leven
Advanced tools
Comparing version 2.1.0 to 3.0.0
91
index.js
@@ -1,31 +0,22 @@ | ||
/* eslint-disable no-nested-ternary */ | ||
'use strict'; | ||
var arr = []; | ||
var charCodeCache = []; | ||
const array = []; | ||
const charCodeCache = []; | ||
module.exports = function (a, b) { | ||
if (a === b) { | ||
const leven = (left, right) => { | ||
if (left === right) { | ||
return 0; | ||
} | ||
var swap = a; | ||
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 (a.length > b.length) { | ||
a = b; | ||
b = swap; | ||
if (left.length > right.length) { | ||
left = right; | ||
right = swap; | ||
} | ||
var aLen = a.length; | ||
var bLen = b.length; | ||
let leftLength = left.length; | ||
let rightLength = right.length; | ||
if (aLen === 0) { | ||
return bLen; | ||
} | ||
if (bLen === 0) { | ||
return aLen; | ||
} | ||
// Performing suffix trimming: | ||
@@ -35,52 +26,52 @@ // We can linearly drop suffix common to both strings since they | ||
// Note: `~-` is the bitwise way to perform a `- 1` operation | ||
while (aLen > 0 && (a.charCodeAt(~-aLen) === b.charCodeAt(~-bLen))) { | ||
aLen--; | ||
bLen--; | ||
while (leftLength > 0 && (left.charCodeAt(~-leftLength) === right.charCodeAt(~-rightLength))) { | ||
leftLength--; | ||
rightLength--; | ||
} | ||
if (aLen === 0) { | ||
return bLen; | ||
} | ||
// Performing prefix trimming | ||
// We can linearly drop prefix common to both strings since they | ||
// don't increase distance at all | ||
var start = 0; | ||
let start = 0; | ||
while (start < aLen && (a.charCodeAt(start) === b.charCodeAt(start))) { | ||
while (start < leftLength && (left.charCodeAt(start) === right.charCodeAt(start))) { | ||
start++; | ||
} | ||
aLen -= start; | ||
bLen -= start; | ||
leftLength -= start; | ||
rightLength -= start; | ||
if (aLen === 0) { | ||
return bLen; | ||
if (leftLength === 0) { | ||
return rightLength; | ||
} | ||
var bCharCode; | ||
var ret; | ||
var tmp; | ||
var tmp2; | ||
var i = 0; | ||
var j = 0; | ||
let bCharCode; | ||
let result; | ||
let temp; | ||
let temp2; | ||
let i = 0; | ||
let j = 0; | ||
while (i < aLen) { | ||
charCodeCache[start + i] = a.charCodeAt(start + i); | ||
arr[i] = ++i; | ||
while (i < leftLength) { | ||
charCodeCache[i] = left.charCodeAt(start + i); | ||
array[i] = ++i; | ||
} | ||
while (j < bLen) { | ||
bCharCode = b.charCodeAt(start + j); | ||
tmp = j++; | ||
ret = j; | ||
while (j < rightLength) { | ||
bCharCode = right.charCodeAt(start + j); | ||
temp = j++; | ||
result = j; | ||
for (i = 0; i < aLen; i++) { | ||
tmp2 = bCharCode === charCodeCache[start + i] ? tmp : tmp + 1; | ||
tmp = arr[i]; | ||
ret = arr[i] = tmp > ret ? tmp2 > ret ? ret + 1 : tmp2 : tmp2 > tmp ? tmp + 1 : tmp2; | ||
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 ret; | ||
return result; | ||
}; | ||
module.exports = leven; | ||
module.exports.default = leven; |
108
package.json
{ | ||
"name": "leven", | ||
"version": "2.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": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava", | ||
"bench": "matcha bench.js" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"leven", | ||
"levenshtein", | ||
"distance", | ||
"algorithm", | ||
"algo", | ||
"string", | ||
"difference", | ||
"diff", | ||
"fast", | ||
"fuzzy", | ||
"similar", | ||
"similarity", | ||
"compare", | ||
"comparison", | ||
"edit", | ||
"text", | ||
"match", | ||
"matching" | ||
], | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"fast-levenshtein": "^2.0.5", | ||
"ld": "^0.1.0", | ||
"levdist": "^2.0.0", | ||
"levenshtein": "^1.0.4", | ||
"levenshtein-component": "0.0.1", | ||
"levenshtein-edit-distance": "^2.0.0", | ||
"matcha": "^0.7.0", | ||
"natural": "^0.4.0", | ||
"talisman": "^0.18.0", | ||
"xo": "^0.16.0" | ||
} | ||
"name": "leven", | ||
"version": "3.0.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-check", | ||
"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.3.1", | ||
"fast-levenshtein": "^2.0.5", | ||
"ld": "^0.1.0", | ||
"levdist": "^2.0.0", | ||
"levenshtein": "^1.0.4", | ||
"levenshtein-component": "0.0.1", | ||
"levenshtein-edit-distance": "^2.0.0", | ||
"matcha": "^0.7.0", | ||
"natural": "^0.6.3", | ||
"talisman": "^0.21.0", | ||
"tsd-check": "^0.3.0", | ||
"xo": "^0.24.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<br> | ||
> The fastest JS implementation of the [Levenshtein distance](http://en.wikipedia.org/wiki/Levenshtein_distance) algorithm | ||
> One of the fastest JS implementations of the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm | ||
@@ -10,3 +10,3 @@ | ||
``` | ||
$ npm install --save leven | ||
$ npm install leven | ||
``` | ||
@@ -32,11 +32,11 @@ | ||
``` | ||
401,487 op/s » leven | ||
371,707 op/s » talisman | ||
264,191 op/s » levenshtein-edit-distance | ||
152,923 op/s » fast-levenshtein | ||
57,267 op/s » levenshtein-component | ||
19,915 op/s » levdist | ||
21,802 op/s » ld | ||
18,079 op/s » natural | ||
11,761 op/s » levenshtein | ||
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 | ||
``` | ||
@@ -43,0 +43,0 @@ |
Sorry, the diff of this file is not supported yet
5011
5
12
65