forked from prettier/angular-estree-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
47 lines (39 loc) · 1.04 KB
/
utils.ts
File metadata and controls
47 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { LocationInformation, RawNGSpan } from './types.ts';
function getCharacterSearchTestFunction(pattern: RegExp | string) {
if (typeof pattern === 'string') {
return (character: string) => character === pattern;
}
return (character: string) => pattern.test(character);
}
export function getCharacterIndex(
text: string,
pattern: RegExp | string,
fromIndex: number,
) {
const test = getCharacterSearchTestFunction(pattern);
for (let index = fromIndex; index < text.length; index++) {
const character = text[index];
if (test(character)) {
return index;
}
}
/* c8 ignore next 4 @preserve */
throw new Error(
`Cannot find character ${pattern} from index ${fromIndex} in ${JSON.stringify(
text,
)}`,
);
}
export function lowercaseFirst(str: string) {
return str.slice(0, 1).toLowerCase() + str.slice(1);
}
export function sourceSpanToLocationInformation(
span: RawNGSpan,
): LocationInformation {
const { start, end } = span;
return {
start,
end,
range: [start, end],
};
}