Skip to content

Commit 902cb27

Browse files
committed
Apply review suggestion
1 parent 33561b4 commit 902cb27

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
let funcToExport: (string: string) => string;
2+
13
/**
24
* Escape characters with special meaning either inside or outside character sets.
35
*
46
* Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
57
*
68
* Taken from https://github.com/sindresorhus/escape-string-regexp/
79
*/
8-
export function escapeStringRegexp(string: string) {
9-
// Use native implementation if available (Node.js 24+)
10-
// A benchmark showed that it is up to 50% faster than the polyfill
11-
// @ts-expect-error Outdated Node.js types
12-
if (typeof RegExp?.escape === "function") {
13-
// @ts-expect-error Outdated Node.js types
14-
return RegExp.escape(string);
15-
}
16-
10+
function escapeStringRegexpFallback(string: string) {
1711
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
1812
}
13+
14+
// Use native implementation if available (Node.js 24+)
15+
// A benchmark showed that it is up to 50% faster than the polyfill
16+
// @ts-expect-error Outdated Node.js types
17+
if (typeof RegExp?.escape === "function") {
18+
// @ts-expect-error Outdated Node.js types
19+
funcToExport = RegExp.escape;
20+
} else {
21+
funcToExport = escapeStringRegexpFallback;
22+
}
23+
24+
export { funcToExport as escapeStringRegexp };

0 commit comments

Comments
 (0)