forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.ts
More file actions
62 lines (57 loc) · 1.56 KB
/
Copy pathtext.ts
File metadata and controls
62 lines (57 loc) · 1.56 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
export function getTextPositionMessage(text: string, index: number) {
if (!isFinite(index)) {
throw new Error(`Wrong char index ${index}`);
}
let message = '';
let line = 0;
let prevLn: number;
let nextLn = 0;
do {
line++;
prevLn = nextLn;
nextLn = text.indexOf('\n', prevLn + 1);
} while (nextLn >= 0 && nextLn <= index);
const column = index - prevLn;
message += `line ${line}, column ${column}`;
message += '\n';
if (index < text.length) {
message += text.substring(prevLn + 1, nextLn);
} else {
message += text.substring(text.lastIndexOf('\n') + 1);
}
message += '\n';
message += `${new Array(column).join('-')}^`;
return message;
}
export function getTextDiffIndex(a: string, b: string) {
const short = Math.min(a.length, b.length);
for (let i = 0; i < short; i++) {
if (a[i] !== b[i]) {
return i;
}
}
if (a.length !== b.length) {
return short;
}
return -1;
}
export function parseArray(text: string) {
return text.replace(/\r/g, '')
.split('\n')
.map((s) => s.trim())
.filter((s) => s);
}
export function formatArray(arr: string[]) {
return arr.concat('').join('\n');
}
export function getMatches(regex: RegExp, input: string, group = 0) {
const matches: string[] = [];
let m: RegExpMatchArray;
while (m = regex.exec(input)) {
matches.push(m[group]);
}
return matches;
}
export function getStringSize(value: string) {
return value.length * 2;
}