-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.ts
39 lines (35 loc) · 1.01 KB
/
utils.ts
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
type QuotePairMap = {
single: Record<number, boolean>;
double: Record<number, boolean>;
};
function getQuotePairMap(str?: string | undefined | null): QuotePairMap {
if (!str) str = '';
const quotePairMap: QuotePairMap = { single: {}, double: {} };
const prevQuote = { single: -1, double: -1 };
let prevChar = '';
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (prevChar !== '\\') {
if (char === '"') {
if (prevQuote.double >= 0) {
quotePairMap.double[prevQuote.double] = true;
quotePairMap.double[i] = true;
prevQuote.double = -1;
} else {
prevQuote.double = i;
}
} else if (char === "'") {
if (prevQuote.single >= 0) {
quotePairMap.single[prevQuote.single] = true;
quotePairMap.single[i] = true;
prevQuote.single = -1;
} else {
prevQuote.single = i;
}
}
}
prevChar = char ?? '';
}
return quotePairMap;
}
export { getQuotePairMap };