@amritk/helpers 0.23.2 → 0.23.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/identifier-mentions.js +26 -11
- package/package.json +1 -1
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
const escapeForWordMatch = (name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2
2
|
const BEFORE_REGEX = /* @__PURE__ */ new Set(["(", ",", "=", ":", "[", "!", "&", "|", "?", "{", "}", ";", "\n"]);
|
|
3
3
|
const stripDataText = (source) => {
|
|
4
|
-
const
|
|
4
|
+
const parts = [];
|
|
5
|
+
let copiedTo = 0;
|
|
5
6
|
let previous = "\n";
|
|
6
7
|
let i = 0;
|
|
7
8
|
const blank = (from, to) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
out[n] = " ";
|
|
9
|
+
parts.push(source.slice(copiedTo, from), source.slice(from, to).replace(/[^\n]/g, " "));
|
|
10
|
+
copiedTo = to;
|
|
11
11
|
};
|
|
12
12
|
while (i < source.length) {
|
|
13
|
+
const code = source.charCodeAt(i);
|
|
14
|
+
if (code !== 47 && code !== 34 && code !== 39 && code !== 96) {
|
|
15
|
+
if (code === 10)
|
|
16
|
+
previous = "\n";
|
|
17
|
+
else if (!isWhitespace(code, source, i))
|
|
18
|
+
previous = source[i];
|
|
19
|
+
i++;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
13
22
|
const char = source[i];
|
|
14
23
|
const next = source[i + 1];
|
|
15
24
|
if (char === "/" && next === "/") {
|
|
@@ -43,7 +52,7 @@ const stripDataText = (source) => {
|
|
|
43
52
|
previous = char;
|
|
44
53
|
continue;
|
|
45
54
|
}
|
|
46
|
-
if (
|
|
55
|
+
if (BEFORE_REGEX.has(previous)) {
|
|
47
56
|
let n = i + 1;
|
|
48
57
|
let inClass = false;
|
|
49
58
|
while (n < source.length) {
|
|
@@ -71,17 +80,23 @@ const stripDataText = (source) => {
|
|
|
71
80
|
previous = "/";
|
|
72
81
|
continue;
|
|
73
82
|
}
|
|
74
|
-
|
|
75
|
-
previous = char;
|
|
76
|
-
else if (char === "\n")
|
|
77
|
-
previous = "\n";
|
|
83
|
+
previous = char;
|
|
78
84
|
i++;
|
|
79
85
|
}
|
|
80
|
-
|
|
86
|
+
parts.push(source.slice(copiedTo));
|
|
87
|
+
return parts.join("");
|
|
81
88
|
};
|
|
89
|
+
const isWhitespace = (code, source, index) => code < 128 ? code === 32 || code >= 9 && code <= 13 : /\s/.test(source[index]);
|
|
90
|
+
const WORD_NAME = /^\w+$/;
|
|
82
91
|
const identifierMentions = (source) => {
|
|
83
92
|
const code = stripDataText(source);
|
|
84
|
-
|
|
93
|
+
let words;
|
|
94
|
+
return (name) => {
|
|
95
|
+
if (!WORD_NAME.test(name))
|
|
96
|
+
return new RegExp(`\\b${escapeForWordMatch(name)}\\b`).test(code);
|
|
97
|
+
words ??= new Set(code.match(/\w+/g));
|
|
98
|
+
return words.has(name);
|
|
99
|
+
};
|
|
85
100
|
};
|
|
86
101
|
export {
|
|
87
102
|
identifierMentions
|