Codebase list node-leven / ac4dbda
Import upstream version 3.1.0, md5 7188524351215283371e5adc5da6df71 Debian Janitor 4 years ago
13 changed file(s) with 185 addition(s) and 178 deletion(s). Raw diff Collapse all Expand all
66 trim_trailing_whitespace = true
77 insert_final_newline = true
88
9 [{package.json,*.yml}]
9 [*.yml]
1010 indent_style = space
1111 indent_size = 2
0 * text=auto
1 *.js text eol=lf
0 * text=auto eol=lf
00 node_modules
1 yarn.lock
0 package-lock=false
0 sudo: false
10 language: node_js
21 node_js:
2 - '10'
3 - '8'
34 - '6'
4 - '4'
5 - '0.12'
6 - '0.10'
00 /* globals bench suite */
11 '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('.');
1111
1212 function run(fn) {
1313 fn('a', 'b');
2424 fn('levenshtein', 'frankenstein');
2525 fn('distance', 'difference');
2626 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.');
2728 }
2829
29 suite('leven', function () {
30 bench('leven', function () {
30 suite('leven', () => {
31 bench('leven', () => {
3132 run(leven);
3233 });
3334
34 bench('talisman', function () {
35 bench('talisman', () => {
3536 run(talisman);
3637 });
3738
38 bench('levenshtein-edit-distance', function () {
39 bench('levenshtein-edit-distance', () => {
3940 run(levenshteinEditDistance);
4041 });
4142
42 bench('fast-levenshtein', function () {
43 bench('fast-levenshtein', () => {
4344 run(fastLevenshtein);
4445 });
4546
46 bench('levenshtein-component', function () {
47 bench('levenshtein-component', () => {
4748 run(levenshteinComponent);
4849 });
4950
50 bench('levdist', function () {
51 bench('ld', () => {
52 run(ld);
53 });
54
55 bench('levenshtein', () => {
56 run(levenshtein);
57 });
58
59 bench('levdist', () => {
5160 run(levdist);
5261 });
5362
54 bench('ld', function () {
55 run(ld);
56 });
57
58 bench('natural', function () {
63 bench('natural', () => {
5964 run(natural);
6065 });
61
62 bench('levenshtein', function () {
63 run(levenshtein);
64 });
6566 });
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 */
10 'use strict';
2 var arr = [];
3 var charCodeCache = [];
1 const array = [];
2 const charCodeCache = [];
43
5 module.exports = function (a, b) {
6 if (a === b) {
4 const leven = (left, right) => {
5 if (left === right) {
76 return 0;
87 }
98
10 var swap = a;
9 const swap = left;
1110
1211 // Swapping the strings if `a` is longer than `b` so we know which one is the
1312 // 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;
1716 }
1817
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;
2920
3021 // Performing suffix trimming:
3122 // We can linearly drop suffix common to both strings since they
3223 // don't increase distance at all
3324 // 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--;
4128 }
4229
4330 // Performing prefix trimming
4431 // We can linearly drop prefix common to both strings since they
4532 // don't increase distance at all
46 var start = 0;
33 let start = 0;
4734
48 while (start < aLen && (a.charCodeAt(start) === b.charCodeAt(start))) {
35 while (start < leftLength && (left.charCodeAt(start) === right.charCodeAt(start))) {
4936 start++;
5037 }
5138
52 aLen -= start;
53 bLen -= start;
39 leftLength -= start;
40 rightLength -= start;
5441
55 if (aLen === 0) {
56 return bLen;
42 if (leftLength === 0) {
43 return rightLength;
5744 }
5845
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;
6552
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;
6956 }
7057
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;
7562
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;
8068 }
8169 }
8270
83 return ret;
71 return result;
8472 };
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
11
22 Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
33
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:
105
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.
137
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.
00 {
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 }
5456 }
00 # leven [![Build Status](https://travis-ci.org/sindresorhus/leven.svg?branch=master)](https://travis-ci.org/sindresorhus/leven)
11
22 > 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
44
55
66 ## Install
77
88 ```
9 $ npm install --save leven
9 $ npm install leven
1010 ```
1111
1212
2727 ```
2828
2929 ```
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
3939 ```
4040
4141
00 import test from 'ava';
1 import m from './';
1 import leven from '.';
22
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);
1818 });