Import upstream version 3.1.0, md5 7188524351215283371e5adc5da6df71
Debian Janitor
4 years ago
6 | 6 | trim_trailing_whitespace = true |
7 | 7 | insert_final_newline = true |
8 | 8 | |
9 | [{package.json,*.yml}] | |
9 | [*.yml] | |
10 | 10 | indent_style = space |
11 | 11 | indent_size = 2 |
0 | sudo: false | |
1 | 0 | language: node_js |
2 | 1 | node_js: |
2 | - '10' | |
3 | - '8' | |
3 | 4 | - '6' |
4 | - '4' | |
5 | - '0.12' | |
6 | - '0.10' |
0 | 0 | /* globals bench suite */ |
1 | 1 | 'use strict'; |
2 | var levenshteinEditDistance = require('levenshtein-edit-distance'); | |
3 | var fastLevenshtein = require('fast-levenshtein').get; | |
4 | var levenshteinComponent = require('levenshtein-component'); | |
5 | var ld = require('ld').computeDistance; | |
6 | var levdist = require('levdist'); | |
7 | var natural = require('natural').LevenshteinDistance; | |
8 | var levenshtein = require('levenshtein'); | |
9 | var talisman = require('talisman/metrics/distance/levenshtein'); | |
10 | var leven = require('./'); | |
2 | const levenshteinEditDistance = require('levenshtein-edit-distance'); | |
3 | const fastLevenshtein = require('fast-levenshtein').get; | |
4 | const levenshteinComponent = require('levenshtein-component'); | |
5 | const ld = require('ld').computeDistance; | |
6 | const levdist = require('levdist'); | |
7 | const natural = require('natural').LevenshteinDistance; | |
8 | const levenshtein = require('levenshtein'); | |
9 | const talisman = require('talisman/metrics/distance/levenshtein'); | |
10 | const leven = require('.'); | |
11 | 11 | |
12 | 12 | function run(fn) { |
13 | 13 | fn('a', 'b'); |
24 | 24 | fn('levenshtein', 'frankenstein'); |
25 | 25 | fn('distance', 'difference'); |
26 | 26 | fn('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'); |
27 | fn('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.'); | |
27 | 28 | } |
28 | 29 | |
29 | suite('leven', function () { | |
30 | bench('leven', function () { | |
30 | suite('leven', () => { | |
31 | bench('leven', () => { | |
31 | 32 | run(leven); |
32 | 33 | }); |
33 | 34 | |
34 | bench('talisman', function () { | |
35 | bench('talisman', () => { | |
35 | 36 | run(talisman); |
36 | 37 | }); |
37 | 38 | |
38 | bench('levenshtein-edit-distance', function () { | |
39 | bench('levenshtein-edit-distance', () => { | |
39 | 40 | run(levenshteinEditDistance); |
40 | 41 | }); |
41 | 42 | |
42 | bench('fast-levenshtein', function () { | |
43 | bench('fast-levenshtein', () => { | |
43 | 44 | run(fastLevenshtein); |
44 | 45 | }); |
45 | 46 | |
46 | bench('levenshtein-component', function () { | |
47 | bench('levenshtein-component', () => { | |
47 | 48 | run(levenshteinComponent); |
48 | 49 | }); |
49 | 50 | |
50 | bench('levdist', function () { | |
51 | bench('ld', () => { | |
52 | run(ld); | |
53 | }); | |
54 | ||
55 | bench('levenshtein', () => { | |
56 | run(levenshtein); | |
57 | }); | |
58 | ||
59 | bench('levdist', () => { | |
51 | 60 | run(levdist); |
52 | 61 | }); |
53 | 62 | |
54 | bench('ld', function () { | |
55 | run(ld); | |
56 | }); | |
57 | ||
58 | bench('natural', function () { | |
63 | bench('natural', () => { | |
59 | 64 | run(natural); |
60 | 65 | }); |
61 | ||
62 | bench('levenshtein', function () { | |
63 | run(levenshtein); | |
64 | }); | |
65 | 66 | }); |
0 | declare const leven: { | |
1 | /** | |
2 | Measure the difference between two strings. | |
3 | ||
4 | @example | |
5 | ``` | |
6 | import leven = require('leven'); | |
7 | ||
8 | leven('cat', 'cow'); | |
9 | //=> 2 | |
10 | ``` | |
11 | */ | |
12 | (left: string, right: string): number; | |
13 | ||
14 | // TODO: Remove this for the next major release, refactor the whole definition to: | |
15 | // declare function leven(left: string, right: string): number; | |
16 | // export = leven; | |
17 | default: typeof leven; | |
18 | }; | |
19 | ||
20 | export = leven; |
0 | /* eslint-disable no-nested-ternary */ | |
1 | 0 | 'use strict'; |
2 | var arr = []; | |
3 | var charCodeCache = []; | |
1 | const array = []; | |
2 | const charCodeCache = []; | |
4 | 3 | |
5 | module.exports = function (a, b) { | |
6 | if (a === b) { | |
4 | const leven = (left, right) => { | |
5 | if (left === right) { | |
7 | 6 | return 0; |
8 | 7 | } |
9 | 8 | |
10 | var swap = a; | |
9 | const swap = left; | |
11 | 10 | |
12 | 11 | // Swapping the strings if `a` is longer than `b` so we know which one is the |
13 | 12 | // shortest & which one is the longest |
14 | if (a.length > b.length) { | |
15 | a = b; | |
16 | b = swap; | |
13 | if (left.length > right.length) { | |
14 | left = right; | |
15 | right = swap; | |
17 | 16 | } |
18 | 17 | |
19 | var aLen = a.length; | |
20 | var bLen = b.length; | |
21 | ||
22 | if (aLen === 0) { | |
23 | return bLen; | |
24 | } | |
25 | ||
26 | if (bLen === 0) { | |
27 | return aLen; | |
28 | } | |
18 | let leftLength = left.length; | |
19 | let rightLength = right.length; | |
29 | 20 | |
30 | 21 | // Performing suffix trimming: |
31 | 22 | // We can linearly drop suffix common to both strings since they |
32 | 23 | // don't increase distance at all |
33 | 24 | // Note: `~-` is the bitwise way to perform a `- 1` operation |
34 | while (aLen > 0 && (a.charCodeAt(~-aLen) === b.charCodeAt(~-bLen))) { | |
35 | aLen--; | |
36 | bLen--; | |
37 | } | |
38 | ||
39 | if (aLen === 0) { | |
40 | return bLen; | |
25 | while (leftLength > 0 && (left.charCodeAt(~-leftLength) === right.charCodeAt(~-rightLength))) { | |
26 | leftLength--; | |
27 | rightLength--; | |
41 | 28 | } |
42 | 29 | |
43 | 30 | // Performing prefix trimming |
44 | 31 | // We can linearly drop prefix common to both strings since they |
45 | 32 | // don't increase distance at all |
46 | var start = 0; | |
33 | let start = 0; | |
47 | 34 | |
48 | while (start < aLen && (a.charCodeAt(start) === b.charCodeAt(start))) { | |
35 | while (start < leftLength && (left.charCodeAt(start) === right.charCodeAt(start))) { | |
49 | 36 | start++; |
50 | 37 | } |
51 | 38 | |
52 | aLen -= start; | |
53 | bLen -= start; | |
39 | leftLength -= start; | |
40 | rightLength -= start; | |
54 | 41 | |
55 | if (aLen === 0) { | |
56 | return bLen; | |
42 | if (leftLength === 0) { | |
43 | return rightLength; | |
57 | 44 | } |
58 | 45 | |
59 | var bCharCode; | |
60 | var ret; | |
61 | var tmp; | |
62 | var tmp2; | |
63 | var i = 0; | |
64 | var j = 0; | |
46 | let bCharCode; | |
47 | let result; | |
48 | let temp; | |
49 | let temp2; | |
50 | let i = 0; | |
51 | let j = 0; | |
65 | 52 | |
66 | while (i < aLen) { | |
67 | charCodeCache[start + i] = a.charCodeAt(start + i); | |
68 | arr[i] = ++i; | |
53 | while (i < leftLength) { | |
54 | charCodeCache[i] = left.charCodeAt(start + i); | |
55 | array[i] = ++i; | |
69 | 56 | } |
70 | 57 | |
71 | while (j < bLen) { | |
72 | bCharCode = b.charCodeAt(start + j); | |
73 | tmp = j++; | |
74 | ret = j; | |
58 | while (j < rightLength) { | |
59 | bCharCode = right.charCodeAt(start + j); | |
60 | temp = j++; | |
61 | result = j; | |
75 | 62 | |
76 | for (i = 0; i < aLen; i++) { | |
77 | tmp2 = bCharCode === charCodeCache[start + i] ? tmp : tmp + 1; | |
78 | tmp = arr[i]; | |
79 | ret = arr[i] = tmp > ret ? tmp2 > ret ? ret + 1 : tmp2 : tmp2 > tmp ? tmp + 1 : tmp2; | |
63 | for (i = 0; i < leftLength; i++) { | |
64 | temp2 = bCharCode === charCodeCache[i] ? temp : temp + 1; | |
65 | temp = array[i]; | |
66 | // eslint-disable-next-line no-multi-assign | |
67 | result = array[i] = temp > result ? temp2 > result ? result + 1 : temp2 : temp2 > temp ? temp + 1 : temp2; | |
80 | 68 | } |
81 | 69 | } |
82 | 70 | |
83 | return ret; | |
71 | return result; | |
84 | 72 | }; |
73 | ||
74 | module.exports = leven; | |
75 | // TODO: Remove this for the next major release | |
76 | module.exports.default = leven; |
0 | import {expectType} from 'tsd'; | |
1 | import leven = require('.'); | |
2 | ||
3 | expectType<number>(leven('kitten', 'sitting')); |
0 | The MIT License (MIT) | |
0 | MIT License | |
1 | 1 | |
2 | 2 | Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) |
3 | 3 | |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | of this software and associated documentation files (the "Software"), to deal | |
6 | in the Software without restriction, including without limitation the rights | |
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | copies of the Software, and to permit persons to whom the Software is | |
9 | furnished to do so, subject to the following conditions: | |
4 | 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: | |
10 | 5 | |
11 | The above copyright notice and this permission notice shall be included in | |
12 | all copies or substantial portions of the Software. | |
6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
13 | 7 | |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 | THE SOFTWARE. | |
8 | 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. |
0 | 0 | { |
1 | "name": "leven", | |
2 | "version": "2.1.0", | |
3 | "description": "Measure the difference between two strings using the fastest JS implementation of the Levenshtein distance algorithm", | |
4 | "license": "MIT", | |
5 | "repository": "sindresorhus/leven", | |
6 | "author": { | |
7 | "name": "Sindre Sorhus", | |
8 | "email": "sindresorhus@gmail.com", | |
9 | "url": "sindresorhus.com" | |
10 | }, | |
11 | "engines": { | |
12 | "node": ">=0.10.0" | |
13 | }, | |
14 | "scripts": { | |
15 | "test": "xo && ava", | |
16 | "bench": "matcha bench.js" | |
17 | }, | |
18 | "files": [ | |
19 | "index.js" | |
20 | ], | |
21 | "keywords": [ | |
22 | "leven", | |
23 | "levenshtein", | |
24 | "distance", | |
25 | "algorithm", | |
26 | "algo", | |
27 | "string", | |
28 | "difference", | |
29 | "diff", | |
30 | "fast", | |
31 | "fuzzy", | |
32 | "similar", | |
33 | "similarity", | |
34 | "compare", | |
35 | "comparison", | |
36 | "edit", | |
37 | "text", | |
38 | "match", | |
39 | "matching" | |
40 | ], | |
41 | "devDependencies": { | |
42 | "ava": "^0.17.0", | |
43 | "fast-levenshtein": "^2.0.5", | |
44 | "ld": "^0.1.0", | |
45 | "levdist": "^2.0.0", | |
46 | "levenshtein": "^1.0.4", | |
47 | "levenshtein-component": "0.0.1", | |
48 | "levenshtein-edit-distance": "^2.0.0", | |
49 | "matcha": "^0.7.0", | |
50 | "natural": "^0.4.0", | |
51 | "talisman": "^0.18.0", | |
52 | "xo": "^0.16.0" | |
53 | } | |
1 | "name": "leven", | |
2 | "version": "3.1.0", | |
3 | "description": "Measure the difference between two strings using the fastest JS implementation of the Levenshtein distance algorithm", | |
4 | "license": "MIT", | |
5 | "repository": "sindresorhus/leven", | |
6 | "author": { | |
7 | "name": "Sindre Sorhus", | |
8 | "email": "sindresorhus@gmail.com", | |
9 | "url": "sindresorhus.com" | |
10 | }, | |
11 | "engines": { | |
12 | "node": ">=6" | |
13 | }, | |
14 | "scripts": { | |
15 | "test": "xo && ava && tsd", | |
16 | "bench": "matcha bench.js" | |
17 | }, | |
18 | "files": [ | |
19 | "index.js", | |
20 | "index.d.ts" | |
21 | ], | |
22 | "keywords": [ | |
23 | "leven", | |
24 | "levenshtein", | |
25 | "distance", | |
26 | "algorithm", | |
27 | "algo", | |
28 | "string", | |
29 | "difference", | |
30 | "diff", | |
31 | "fast", | |
32 | "fuzzy", | |
33 | "similar", | |
34 | "similarity", | |
35 | "compare", | |
36 | "comparison", | |
37 | "edit", | |
38 | "text", | |
39 | "match", | |
40 | "matching" | |
41 | ], | |
42 | "devDependencies": { | |
43 | "ava": "^1.4.1", | |
44 | "fast-levenshtein": "^2.0.6", | |
45 | "ld": "^0.1.0", | |
46 | "levdist": "^2.2.9", | |
47 | "levenshtein": "^1.0.5", | |
48 | "levenshtein-component": "^0.0.1", | |
49 | "levenshtein-edit-distance": "^2.0.3", | |
50 | "matcha": "^0.7.0", | |
51 | "natural": "^0.6.3", | |
52 | "talisman": "^0.21.0", | |
53 | "tsd": "^0.7.2", | |
54 | "xo": "^0.24.0" | |
55 | } | |
54 | 56 | } |
0 | 0 | # leven [![Build Status](https://travis-ci.org/sindresorhus/leven.svg?branch=master)](https://travis-ci.org/sindresorhus/leven) |
1 | 1 | |
2 | 2 | > Measure the difference between two strings<br> |
3 | > The fastest JS implementation of the [Levenshtein distance](http://en.wikipedia.org/wiki/Levenshtein_distance) algorithm | |
3 | > One of the fastest JS implementations of the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm | |
4 | 4 | |
5 | 5 | |
6 | 6 | ## Install |
7 | 7 | |
8 | 8 | ``` |
9 | $ npm install --save leven | |
9 | $ npm install leven | |
10 | 10 | ``` |
11 | 11 | |
12 | 12 | |
27 | 27 | ``` |
28 | 28 | |
29 | 29 | ``` |
30 | 401,487 op/s » leven | |
31 | 371,707 op/s » talisman | |
32 | 264,191 op/s » levenshtein-edit-distance | |
33 | 152,923 op/s » fast-levenshtein | |
34 | 57,267 op/s » levenshtein-component | |
35 | 19,915 op/s » levdist | |
36 | 21,802 op/s » ld | |
37 | 18,079 op/s » natural | |
38 | 11,761 op/s » levenshtein | |
30 | 165,926 op/s » leven | |
31 | 164,398 op/s » talisman | |
32 | 1,044 op/s » levenshtein-edit-distance | |
33 | 628 op/s » fast-levenshtein | |
34 | 497 op/s » levenshtein-component | |
35 | 195 op/s » ld | |
36 | 190 op/s » levenshtein | |
37 | 168 op/s » levdist | |
38 | 10 op/s » natural | |
39 | 39 | ``` |
40 | 40 | |
41 | 41 |
0 | 0 | import test from 'ava'; |
1 | import m from './'; | |
1 | import leven from '.'; | |
2 | 2 | |
3 | test(t => { | |
4 | t.is(m('a', 'b'), 1); | |
5 | t.is(m('ab', 'ac'), 1); | |
6 | t.is(m('ac', 'bc'), 1); | |
7 | t.is(m('abc', 'axc'), 1); | |
8 | t.is(m('kitten', 'sitting'), 3); | |
9 | t.is(m('xabxcdxxefxgx', '1ab2cd34ef5g6'), 6); | |
10 | t.is(m('cat', 'cow'), 2); | |
11 | t.is(m('xabxcdxxefxgx', 'abcdefg'), 6); | |
12 | t.is(m('javawasneat', 'scalaisgreat'), 7); | |
13 | t.is(m('example', 'samples'), 3); | |
14 | t.is(m('sturgeon', 'urgently'), 6); | |
15 | t.is(m('levenshtein', 'frankenstein'), 6); | |
16 | t.is(m('distance', 'difference'), 5); | |
17 | t.is(m('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'), 2); | |
3 | test('main', t => { | |
4 | t.is(leven('a', 'b'), 1); | |
5 | t.is(leven('ab', 'ac'), 1); | |
6 | t.is(leven('ac', 'bc'), 1); | |
7 | t.is(leven('abc', 'axc'), 1); | |
8 | t.is(leven('kitten', 'sitting'), 3); | |
9 | t.is(leven('xabxcdxxefxgx', '1ab2cd34ef5g6'), 6); | |
10 | t.is(leven('cat', 'cow'), 2); | |
11 | t.is(leven('xabxcdxxefxgx', 'abcdefg'), 6); | |
12 | t.is(leven('javawasneat', 'scalaisgreat'), 7); | |
13 | t.is(leven('example', 'samples'), 3); | |
14 | t.is(leven('sturgeon', 'urgently'), 6); | |
15 | t.is(leven('levenshtein', 'frankenstein'), 6); | |
16 | t.is(leven('distance', 'difference'), 5); | |
17 | t.is(leven('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'), 2); | |
18 | 18 | }); |