From 03f494d59e3f2500d17e3b72e11b258d650fd426 Mon Sep 17 00:00:00 2001 From: ka-weihe Date: Mon, 10 Aug 2020 16:25:05 +0200 Subject: [PATCH] faster levenshtein (#4874) --- lib/reportUnknownRuleNames.js | 4 ++-- lib/utils/checkInvalidCLIOptions.js | 4 ++-- package-lock.json | 13 +++++++++++++ package.json | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/reportUnknownRuleNames.js b/lib/reportUnknownRuleNames.js index e06f0f6722..6656bf5092 100644 --- a/lib/reportUnknownRuleNames.js +++ b/lib/reportUnknownRuleNames.js @@ -1,6 +1,6 @@ 'use strict'; -const leven = require('leven'); +const levenshtein = require('fastest-levenshtein'); const rules = require('./rules'); const MAX_LEVENSHTEIN_DISTANCE = 6; @@ -18,7 +18,7 @@ function extractSuggestions(ruleName) { } Object.keys(rules).forEach((existRuleName) => { - const distance = leven(existRuleName, ruleName); + const distance = levenshtein.distance(existRuleName, ruleName); if (distance <= MAX_LEVENSHTEIN_DISTANCE) { suggestions[distance - 1].push(existRuleName); diff --git a/lib/utils/checkInvalidCLIOptions.js b/lib/utils/checkInvalidCLIOptions.js index 9334ecb9e5..9bb937669b 100644 --- a/lib/utils/checkInvalidCLIOptions.js +++ b/lib/utils/checkInvalidCLIOptions.js @@ -3,7 +3,7 @@ const _ = require('lodash'); const chalk = require('chalk'); const EOL = require('os').EOL; -const leven = require('leven'); +const levenshtein = require('fastest-levenshtein'); /** * @param {{ [key: string]: { alias?: string } }} allowedOptions @@ -35,7 +35,7 @@ const suggest = (all, invalid) => { const maxThreshold = 10; for (let threshold = 1; threshold <= maxThreshold; threshold++) { - const suggestion = all.find((option) => leven(option, invalid) <= threshold); + const suggestion = all.find((option) => levenshtein.distance(option, invalid) <= threshold); if (suggestion) { return suggestion; diff --git a/package-lock.json b/package-lock.json index e3cd564a1e..6bcd99a021 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1662,6 +1662,14 @@ "@types/node": "*" } }, + "@types/leven": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@types/leven/-/leven-2.1.2.tgz", + "integrity": "sha512-/DZnNl+6YyiHG+t1EbNMBlMbrU8r1f7RWRi0kAYZYxko0hzA13IMagg9qtTL4a3SwFx4eYubZMcvYAijuKbS5A==", + "requires": { + "leven": "*" + } + }, "@types/lodash": { "version": "4.14.157", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.157.tgz", @@ -3950,6 +3958,11 @@ "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", "dev": true }, + "fastest-levenshtein": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/fastest-levenshtein/-/fastest-levenshtein-1.0.9.tgz", + "integrity": "sha512-8DIQrqkmGTMfjcwiebj28Y1l6eCoLCU+TppdZa5zp5ZyNO12oUhtEEeCZjojZZarD8+peRg3CxL4Zm5BrKr9Fw==" + }, "fastq": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.8.0.tgz", diff --git a/package.json b/package.json index 707f2ebb41..e4a591eb9d 100644 --- a/package.json +++ b/package.json @@ -118,6 +118,7 @@ "cosmiconfig": "^6.0.0", "debug": "^4.1.1", "execall": "^2.0.0", + "fastest-levenshtein": "^1.0.9", "file-entry-cache": "^5.0.1", "get-stdin": "^8.0.0", "global-modules": "^2.0.0", @@ -128,7 +129,6 @@ "import-lazy": "^4.0.0", "imurmurhash": "^0.1.4", "known-css-properties": "^0.19.0", - "leven": "^3.1.0", "lodash": "^4.17.19", "log-symbols": "^4.0.0", "mathml-tag-names": "^2.1.3",