Skip to content

Commit

Permalink
refactor: embed escape-string-regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
macjuul committed Jan 18, 2024
1 parent 1605a6b commit 72d3eec
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,6 @@
"dependencies": {
"csv-stringify": "^6.4.5",
"debounce": "^2.0.0",
"escape-string-regexp": "^5.0.0",
"minimatch": "^9.0.3"
}
}
6 changes: 3 additions & 3 deletions src/anchorEngine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import debounce from "debounce";
import * as escape from "escape-string-regexp";
import * as fs from "node:fs";
import * as path from "node:path";

Expand Down Expand Up @@ -48,6 +47,7 @@ import { flattenAnchors } from "./util/flattener";
import { registerDefaults } from "./util/defaultTags";
import { setupCompletionProvider } from "./util/completionProvider";
import { parseCustomAnchors } from "./util/customTags";
import escapeStringRegexp from "./util/escape";

/* -- Constants -- */

Expand Down Expand Up @@ -498,7 +498,7 @@ export class AnchorEngine {

// Create a selection of tags
const tags = tagList
.map((tag) => escape(tag))
.map((tag) => escapeStringRegexp(tag))
.sort((left, right) => right.length - left.length)
.join("|");

Expand All @@ -509,7 +509,7 @@ export class AnchorEngine {

// Create a selection of separators
const separators = (config.tags.separators as string[])
.map((seperator) => escape(seperator).replaceAll(' ', " +"))
.map((seperator) => escapeStringRegexp(seperator).replaceAll(' ', " +"))
.sort((left, right) => right.length - left.length)
.join("|");

Expand Down
1 change: 0 additions & 1 deletion src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
declare module "escape-string-regexp";
declare module "minimatch";
declare module "*.json";
26 changes: 26 additions & 0 deletions src/util/escape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* escape-string-regexp
*
* MIT License
*
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* NOTE: This dependency is embedded due to bundling limitations
*/
export default function escapeStringRegexp(string: string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}

// Escape characters with special meaning either inside or outside character sets.
// 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.
return string
.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&')
.replaceAll('-', '\\x2d');
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,6 @@ escape-string-regexp@^4.0.0:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==

escape-string-regexp@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz#4683126b500b61762f2dbebace1806e8be31b1c8"
integrity sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==

eslint-plugin-unicorn@^50.0.1:
version "50.0.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-unicorn/-/eslint-plugin-unicorn-50.0.1.tgz#e539cdb02dfd893c603536264c4ed9505b70e3bf"
Expand Down

0 comments on commit 72d3eec

Please sign in to comment.