Skip to content

Commit

Permalink
faster levenshtein (#4874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ka-weihe committed Aug 10, 2020
1 parent a5b8277 commit 03f494d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions 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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/checkInvalidCLIOptions.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 03f494d

Please sign in to comment.