diff --git a/lib/utils/did-you-mean.js b/lib/utils/did-you-mean.js index 98133196e3c56..0cfdd035255eb 100644 --- a/lib/utils/did-you-mean.js +++ b/lib/utils/did-you-mean.js @@ -1,10 +1,10 @@ -const leven = require('leven') +const { distance } = require('fastest-levenshtein') const readJson = require('read-package-json-fast') const { cmdList } = require('./cmd-list.js') const didYouMean = async (npm, path, scmd) => { const bestCmd = cmdList - .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd) + .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd) .map(str => ` npm ${str} # ${npm.commands[str].description}`) const pkg = await readJson(`${path}/package.json`) @@ -12,13 +12,13 @@ const didYouMean = async (npm, path, scmd) => { // We would already be suggesting this in `npm x` so omit them here const runScripts = ['stop', 'start', 'test', 'restart'] const bestRun = Object.keys(scripts || {}) - .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4 && + .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && !runScripts.includes(cmd)) .map(str => ` npm run ${str} # run the "${str}" package script`) const { bin } = pkg const bestBin = Object.keys(bin || {}) - .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4) + .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4) .map(str => ` npm exec ${str} # run the "${str}" command from either this or a remote npm package`) const best = [...bestCmd, ...bestRun, ...bestBin] diff --git a/node_modules/fastest-levenshtein/LICENSE.md b/node_modules/fastest-levenshtein/LICENSE.md new file mode 100644 index 0000000000000..0e62da309fa1e --- /dev/null +++ b/node_modules/fastest-levenshtein/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Kasper Unn Weihe + +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. \ No newline at end of file diff --git a/node_modules/fastest-levenshtein/index.d.ts b/node_modules/fastest-levenshtein/index.d.ts new file mode 100644 index 0000000000000..bdc2bb1cc32e0 --- /dev/null +++ b/node_modules/fastest-levenshtein/index.d.ts @@ -0,0 +1,2 @@ +export function distance(a: string, b: string): number; +export function closest(str: string, arr: string[]): string; \ No newline at end of file diff --git a/node_modules/fastest-levenshtein/index.js b/node_modules/fastest-levenshtein/index.js new file mode 100644 index 0000000000000..437bdebfda388 --- /dev/null +++ b/node_modules/fastest-levenshtein/index.js @@ -0,0 +1,147 @@ +"use strict"; +const peq = new Uint32Array(0x10000); +const myers_32 = (a, b) => { + const n = a.length; + const m = b.length; + const lst = 1 << (n - 1); + let pv = -1; + let mv = 0; + let sc = n; + let i = n; + while (i--) { + peq[a.charCodeAt(i)] |= 1 << i; + } + for (i = 0; i < m; i++) { + let eq = peq[b.charCodeAt(i)]; + const xv = eq | mv; + eq |= ((eq & pv) + pv) ^ pv; + mv |= ~(eq | pv); + pv &= eq; + if (mv & lst) { + sc++; + } + if (pv & lst) { + sc--; + } + mv = (mv << 1) | 1; + pv = (pv << 1) | ~(xv | mv); + mv &= xv; + } + i = n; + while (i--) { + peq[a.charCodeAt(i)] = 0; + } + return sc; +}; + +const myers_x = (a, b) => { + const n = a.length; + const m = b.length; + const mhc = []; + const phc = []; + const hsize = Math.ceil(n / 32); + const vsize = Math.ceil(m / 32); + let score = m; + for (let i = 0; i < hsize; i++) { + phc[i] = -1; + mhc[i] = 0; + } + let j = 0; + for (; j < vsize - 1; j++) { + let mv = 0; + let pv = -1; + const start = j * 32; + const end = Math.min(32, m) + start; + for (let k = start; k < end; k++) { + peq[b.charCodeAt(k)] |= 1 << k; + } + score = m; + for (let i = 0; i < n; i++) { + const eq = peq[a.charCodeAt(i)]; + const pb = (phc[(i / 32) | 0] >>> i) & 1; + const mb = (mhc[(i / 32) | 0] >>> i) & 1; + const xv = eq | mv; + const xh = ((((eq | mb) & pv) + pv) ^ pv) | eq | mb; + let ph = mv | ~(xh | pv); + let mh = pv & xh; + if ((ph >>> 31) ^ pb) { + phc[(i / 32) | 0] ^= 1 << i; + } + if ((mh >>> 31) ^ mb) { + mhc[(i / 32) | 0] ^= 1 << i; + } + ph = (ph << 1) | pb; + mh = (mh << 1) | mb; + pv = mh | ~(xv | ph); + mv = ph & xv; + } + for (let k = start; k < end; k++) { + peq[b.charCodeAt(k)] = 0; + } + } + let mv = 0; + let pv = -1; + const start = j * 32; + const end = Math.min(32, m - start) + start; + for (let k = start; k < end; k++) { + peq[b.charCodeAt(k)] |= 1 << k; + } + score = m; + for (let i = 0; i < n; i++) { + const eq = peq[a.charCodeAt(i)]; + const pb = (phc[(i / 32) | 0] >>> i) & 1; + const mb = (mhc[(i / 32) | 0] >>> i) & 1; + const xv = eq | mv; + const xh = ((((eq | mb) & pv) + pv) ^ pv) | eq | mb; + let ph = mv | ~(xh | pv); + let mh = pv & xh; + score += (ph >>> (m - 1)) & 1; + score -= (mh >>> (m - 1)) & 1; + if ((ph >>> 31) ^ pb) { + phc[(i / 32) | 0] ^= 1 << i; + } + if ((mh >>> 31) ^ mb) { + mhc[(i / 32) | 0] ^= 1 << i; + } + ph = (ph << 1) | pb; + mh = (mh << 1) | mb; + pv = mh | ~(xv | ph); + mv = ph & xv; + } + for (let k = start; k < end; k++) { + peq[b.charCodeAt(k)] = 0; + } + return score; +}; + +const distance = (a, b) => { + if (a.length > b.length) { + const tmp = b; + b = a; + a = tmp; + } + if (a.length === 0) { + return b.length; + } + if (a.length <= 32) { + return myers_32(a, b); + } + return myers_x(a, b); +}; + +const closest = (str, arr) => { + let min_distance = Infinity; + let min_index = 0; + for (let i = 0; i < arr.length; i++) { + const dist = distance(str, arr[i]); + if (dist < min_distance) { + min_distance = dist; + min_index = i; + } + } + return arr[min_index]; +}; + +module.exports = { + closest, distance +} diff --git a/node_modules/fastest-levenshtein/package.json b/node_modules/fastest-levenshtein/package.json new file mode 100644 index 0000000000000..4d3ca34c92913 --- /dev/null +++ b/node_modules/fastest-levenshtein/package.json @@ -0,0 +1,63 @@ +{ + "name": "fastest-levenshtein", + "version": "1.0.12", + "description": "Fastest Levenshtein distance implementation in JS.", + "main": "index.js", + "repository": { + "type": "git", + "url": "git+https://github.com/ka-weihe/fastest-levenshtein.git" + }, + "keywords": [ + "levenshtein", + "distance", + "fast", + "fastest", + "edit", + "string", + "similarity", + "algorithm", + "match", + "comparison", + "fuzzy", + "search", + "string", + "matching", + "similar", + "node", + "difference" + ], + "author": "Kasper U. Weihe", + "license": "MIT", + "bugs": { + "url": "https://github.com/ka-weihe/fastest-levenshtein/issues" + }, + "homepage": "https://github.com/ka-weihe/fastest-levenshtein#README", + "scripts": { + "test": "jest", + "test:coverage": "jest --coverage", + "test:coveralls": "jest --coverage --coverageReporters=text-lcov | coveralls" + }, + "devDependencies": { + "benchmark": "^2.1.4", + "coveralls": "^3.1.0", + "eslint": "^7.5.0", + "eslint-config-airbnb": "^18.2.0", + "eslint-config-airbnb-base": "^14.2.0", + "eslint-config-node": "^4.1.0", + "eslint-config-prettier": "^6.11.0", + "eslint-plugin-import": "^2.22.0", + "eslint-plugin-jsx-a11y": "^6.3.1", + "eslint-plugin-node": "^11.1.0", + "eslint-plugin-prettier": "^3.1.4", + "eslint-plugin-react": "^7.20.3", + "eslint-plugin-react-hooks": "^4.0.0", + "fast-levenshtein": "^2.0.6", + "jest": "^26.1.0", + "js-levenshtein": "^1.1.6", + "leven": "^3.1.0", + "natural": "^2.1.5", + "prettier": "^2.0.5", + "talisman": "^1.1.2", + "levenshtein-edit-distance": "^2.0.5" + } +} diff --git a/node_modules/fastest-levenshtein/test.js b/node_modules/fastest-levenshtein/test.js new file mode 100644 index 0000000000000..4b5d6ecae6504 --- /dev/null +++ b/node_modules/fastest-levenshtein/test.js @@ -0,0 +1,64 @@ +const {distance, closest} = require("./index.js"); + +const levenshtein = (a, b) => { + if (a.length === 0) return b.length; + if (b.length === 0) return a.length; + + if (a.length > b.length) { + const tmp = a; + a = b; + b = tmp; + } + + const row = []; + for (let i = 0; i <= a.length; i++) { + row[i] = i; + } + + for (let i = 1; i <= b.length; i++) { + let prev = i; + for (let j = 1; j <= a.length; j++) { + let val; + if (b.charAt(i - 1) === a.charAt(j - 1)) { + val = row[j - 1]; + } else { + val = Math.min(row[j - 1] + 1, prev + 1, row[j] + 1); + } + row[j - 1] = prev; + prev = val; + } + row[a.length] = prev; + } + + return row[a.length]; +}; + +function makeid(length) { + let result = ""; + const characters = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + const charactersLength = characters.length; + for (let i = 0; i < length; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} + +test("test compare", () => { + const errors = 0; + for (let i = 0; i < 1000; i++) { + const rnd_num1 = (Math.random() * 1000) | 0; + const rnd_num2 = (Math.random() * 1000) | 0; + const rnd_string1 = makeid(rnd_num1); + const rnd_string2 = makeid(rnd_num2); + const actual = distance(rnd_string1, rnd_string2); + const expected = levenshtein(rnd_string1, rnd_string2); + expect(actual).toBe(expected); + } +}); + +test("test find", () => { + const actual = closest("fast", ["slow", "faster", "fastest"]); + const expected = "faster"; + expect(actual).toBe(expected); +}); diff --git a/node_modules/leven/index.d.ts b/node_modules/leven/index.d.ts deleted file mode 100644 index 571833ae44b7d..0000000000000 --- a/node_modules/leven/index.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -declare const leven: { - /** - Measure the difference between two strings. - - @example - ``` - import leven = require('leven'); - - leven('cat', 'cow'); - //=> 2 - ``` - */ - (left: string, right: string): number; - - // TODO: Remove this for the next major release, refactor the whole definition to: - // declare function leven(left: string, right: string): number; - // export = leven; - default: typeof leven; -}; - -export = leven; diff --git a/node_modules/leven/index.js b/node_modules/leven/index.js deleted file mode 100644 index 25f5a3d5c8bd4..0000000000000 --- a/node_modules/leven/index.js +++ /dev/null @@ -1,77 +0,0 @@ -'use strict'; -const array = []; -const charCodeCache = []; - -const leven = (left, right) => { - if (left === right) { - return 0; - } - - const swap = left; - - // Swapping the strings if `a` is longer than `b` so we know which one is the - // shortest & which one is the longest - if (left.length > right.length) { - left = right; - right = swap; - } - - let leftLength = left.length; - let rightLength = right.length; - - // Performing suffix trimming: - // We can linearly drop suffix common to both strings since they - // don't increase distance at all - // Note: `~-` is the bitwise way to perform a `- 1` operation - while (leftLength > 0 && (left.charCodeAt(~-leftLength) === right.charCodeAt(~-rightLength))) { - leftLength--; - rightLength--; - } - - // Performing prefix trimming - // We can linearly drop prefix common to both strings since they - // don't increase distance at all - let start = 0; - - while (start < leftLength && (left.charCodeAt(start) === right.charCodeAt(start))) { - start++; - } - - leftLength -= start; - rightLength -= start; - - if (leftLength === 0) { - return rightLength; - } - - let bCharCode; - let result; - let temp; - let temp2; - let i = 0; - let j = 0; - - while (i < leftLength) { - charCodeCache[i] = left.charCodeAt(start + i); - array[i] = ++i; - } - - while (j < rightLength) { - bCharCode = right.charCodeAt(start + j); - temp = j++; - result = j; - - for (i = 0; i < leftLength; i++) { - temp2 = bCharCode === charCodeCache[i] ? temp : temp + 1; - temp = array[i]; - // eslint-disable-next-line no-multi-assign - result = array[i] = temp > result ? temp2 > result ? result + 1 : temp2 : temp2 > temp ? temp + 1 : temp2; - } - } - - return result; -}; - -module.exports = leven; -// TODO: Remove this for the next major release -module.exports.default = leven; diff --git a/node_modules/leven/license b/node_modules/leven/license deleted file mode 100644 index e7af2f77107d7..0000000000000 --- a/node_modules/leven/license +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) Sindre Sorhus (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. diff --git a/node_modules/leven/package.json b/node_modules/leven/package.json deleted file mode 100644 index e1b3d8ee0cb25..0000000000000 --- a/node_modules/leven/package.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "name": "leven", - "version": "3.1.0", - "description": "Measure the difference between two strings using the fastest JS implementation of the Levenshtein distance algorithm", - "license": "MIT", - "repository": "sindresorhus/leven", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "engines": { - "node": ">=6" - }, - "scripts": { - "test": "xo && ava && tsd", - "bench": "matcha bench.js" - }, - "files": [ - "index.js", - "index.d.ts" - ], - "keywords": [ - "leven", - "levenshtein", - "distance", - "algorithm", - "algo", - "string", - "difference", - "diff", - "fast", - "fuzzy", - "similar", - "similarity", - "compare", - "comparison", - "edit", - "text", - "match", - "matching" - ], - "devDependencies": { - "ava": "^1.4.1", - "fast-levenshtein": "^2.0.6", - "ld": "^0.1.0", - "levdist": "^2.2.9", - "levenshtein": "^1.0.5", - "levenshtein-component": "^0.0.1", - "levenshtein-edit-distance": "^2.0.3", - "matcha": "^0.7.0", - "natural": "^0.6.3", - "talisman": "^0.21.0", - "tsd": "^0.7.2", - "xo": "^0.24.0" - } -} diff --git a/node_modules/leven/readme.md b/node_modules/leven/readme.md deleted file mode 100644 index 33625c46f0059..0000000000000 --- a/node_modules/leven/readme.md +++ /dev/null @@ -1,50 +0,0 @@ -# leven [![Build Status](https://travis-ci.org/sindresorhus/leven.svg?branch=master)](https://travis-ci.org/sindresorhus/leven) - -> Measure the difference between two strings
-> One of the fastest JS implementations of the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm - - -## Install - -``` -$ npm install leven -``` - - -## Usage - -```js -const leven = require('leven'); - -leven('cat', 'cow'); -//=> 2 -``` - - -## Benchmark - -``` -$ npm run bench -``` - -``` - 165,926 op/s » leven - 164,398 op/s » talisman - 1,044 op/s » levenshtein-edit-distance - 628 op/s » fast-levenshtein - 497 op/s » levenshtein-component - 195 op/s » ld - 190 op/s » levenshtein - 168 op/s » levdist - 10 op/s » natural -``` - - -## Related - -- [leven-cli](https://github.com/sindresorhus/leven-cli) - CLI for this module - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/package-lock.json b/package-lock.json index 682d82969f9c7..641e6e1accf66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -99,6 +99,7 @@ "cli-columns": "^3.1.2", "cli-table3": "^0.6.0", "columnify": "~1.5.4", + "fastest-levenshtein": "^1.0.12", "glob": "^7.1.7", "graceful-fs": "^4.2.8", "hosted-git-info": "^4.0.2", @@ -106,7 +107,6 @@ "init-package-json": "^2.0.3", "is-cidr": "^4.0.2", "json-parse-even-better-errors": "^2.3.1", - "leven": "^3.1.0", "libnpmaccess": "^4.0.2", "libnpmdiff": "^2.0.4", "libnpmexec": "^2.0.1", @@ -3035,6 +3035,11 @@ "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", "dev": true }, + "node_modules/fastest-levenshtein": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/fastest-levenshtein/-/fastest-levenshtein-1.0.12.tgz", + "integrity": "sha512-On2N+BpYJ15xIC974QNVuYGMOlEVt4s0EOI3wwMqOmK1fdDY+FN/zltPV8vosq4ad4c/gJ1KHScUn/6AWIgiow==" + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -4587,15 +4592,6 @@ "lcov-parse": "bin/cli.js" } }, - "node_modules/leven": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz", - "integrity": "sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==", - "inBundle": true, - "engines": { - "node": ">=6" - } - }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -12710,6 +12706,11 @@ "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", "dev": true }, + "fastest-levenshtein": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/fastest-levenshtein/-/fastest-levenshtein-1.0.12.tgz", + "integrity": "sha512-On2N+BpYJ15xIC974QNVuYGMOlEVt4s0EOI3wwMqOmK1fdDY+FN/zltPV8vosq4ad4c/gJ1KHScUn/6AWIgiow==" + }, "file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -13806,11 +13807,6 @@ "integrity": "sha1-6w1GtUER68VhrLTECO+TY73I9+A=", "dev": true }, - "leven": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz", - "integrity": "sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==" - }, "levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", diff --git a/package.json b/package.json index cee4f44d8cb68..149ee039fefdc 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "cli-columns": "^3.1.2", "cli-table3": "^0.6.0", "columnify": "~1.5.4", + "fastest-levenshtein": "^1.0.12", "glob": "^7.1.7", "graceful-fs": "^4.2.8", "hosted-git-info": "^4.0.2", @@ -76,7 +77,6 @@ "init-package-json": "^2.0.3", "is-cidr": "^4.0.2", "json-parse-even-better-errors": "^2.3.1", - "leven": "^3.1.0", "libnpmaccess": "^4.0.2", "libnpmdiff": "^2.0.4", "libnpmexec": "^2.0.1", @@ -139,6 +139,7 @@ "cli-columns", "cli-table3", "columnify", + "fastest-levenshtein", "glob", "graceful-fs", "hosted-git-info", @@ -146,7 +147,6 @@ "init-package-json", "is-cidr", "json-parse-even-better-errors", - "leven", "libnpmaccess", "libnpmdiff", "libnpmexec",