File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import { getCharacterIndex , getCharacterLastIndex } from './utils.ts' ;
1+ import { getCharacterIndex } from './utils.ts' ;
22
33test ( 'getCharacterIndex' , ( ) => {
44 expect ( getCharacterIndex ( 'foobar' , / o / , 0 ) ) . toBe ( 1 ) ;
55 expect ( getCharacterIndex ( 'foobar' , / o / , 1 ) ) . toBe ( 1 ) ;
66 expect ( getCharacterIndex ( 'foobar' , 'o' , 0 ) ) . toBe ( 1 ) ;
77 expect ( ( ) => getCharacterIndex ( 'foobar' , '_' , 0 ) ) . toThrow ( ) ;
88} ) ;
9-
10- test ( 'getCharacterLastIndex' , ( ) => {
11- expect ( getCharacterLastIndex ( 'foobar' , / o / , 6 ) ) . toBe ( 2 ) ;
12- expect ( getCharacterLastIndex ( 'foobar' , / o / , 2 ) ) . toBe ( 2 ) ;
13- expect ( getCharacterLastIndex ( 'foobar' , 'o' , 6 ) ) . toBe ( 2 ) ;
14- expect ( ( ) => getCharacterLastIndex ( 'foobar' , '_' , 6 ) ) . toThrow ( ) ;
15- } ) ;
Original file line number Diff line number Diff line change @@ -8,29 +8,6 @@ function getCharacterSearchTestFunction(pattern: RegExp | string) {
88 return ( character : string ) => pattern . test ( character ) ;
99}
1010
11- export function getCharacterLastIndex (
12- text : string ,
13- pattern : RegExp | string ,
14- fromIndex : number ,
15- ) {
16- const test = getCharacterSearchTestFunction ( pattern ) ;
17-
18- for ( let index = fromIndex ; index >= 0 ; index -- ) {
19- const character = text [ index ] ;
20-
21- if ( test ( character ) ) {
22- return index ;
23- }
24- }
25-
26- /* c8 ignore next 4 @preserve */
27- throw new Error (
28- `Cannot find front char ${ pattern } from index ${ fromIndex } in ${ JSON . stringify (
29- text ,
30- ) } `,
31- ) ;
32- }
33-
3411export function getCharacterIndex (
3512 text : string ,
3613 pattern : RegExp | string ,
You can’t perform that action at this time.
0 commit comments