PHP's levenshtein in JavaScript

Here’s what our current JavaScript equivalent to PHP's levenshtein looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module.exports = function levenshtein (s1, s2, costIns, costRep, costDel) {
// discuss at: http://locutus.io/php/levenshtein/
// original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// revised by: Andrea Giammarchi (http://webreflection.blogspot.com)
// reimplemented by: Brett Zamir (http://brett-zamir.me)
// reimplemented by: Alexander M Beedie
// reimplemented by: Rafał Kukawski (http://blog.kukawski.pl)
// example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld')
// returns 1: 3
// example 2: levenshtein("carrrot", "carrots")
// returns 2: 2
// example 3: levenshtein("carrrot", "carrots", 2, 3, 4)
// returns 3: 6
// var LEVENSHTEIN_MAX_LENGTH = 255 // PHP limits the function to max 255 character-long strings
costIns = costIns == null ? 1 : +costIns
costRep = costRep == null ? 1 : +costRep
costDel = costDel == null ? 1 : +costDel
if (s1 === s2) {
return 0
}
var l1 = s1.length
var l2 = s2.length
if (l1 === 0) {
return l2 * costIns
}
if (l2 === 0) {
return l1 * costDel
}
// Enable the 3 lines below to set the same limits on string length as PHP does
// if (l1 > LEVENSHTEIN_MAX_LENGTH || l2 > LEVENSHTEIN_MAX_LENGTH) {
// return -1;
// }
var split = false
try {
split = !('0')[0]
} catch (e) {
// Earlier IE may not support access by string index
split = true
}
if (split) {
s1 = s1.split('')
s2 = s2.split('')
}
var p1 = new Array(l2 + 1)
var p2 = new Array(l2 + 1)
var i1, i2, c0, c1, c2, tmp
for (i2 = 0; i2 <= l2; i2++) {
p1[i2] = i2 * costIns
}
for (i1 = 0; i1 < l1; i1++) {
p2[0] = p1[0] + costDel
for (i2 = 0; i2 < l2; i2++) {
c0 = p1[i2] + ((s1[i1] === s2[i2]) ? 0 : costRep)
c1 = p1[i2 + 1] + costDel
if (c1 < c0) {
c0 = c1
}
c2 = p2[i2] + costIns
if (c2 < c0) {
c0 = c2
}
p2[i2 + 1] = c0
}
tmp = p1
p1 = p2
p2 = tmp
}
c0 = p1[l2]
return c0
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/strings/levenshtein'). You could also require the strings module in full so that you could access strings.levenshtein instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld')3
2levenshtein("carrrot", "carrots")2
3levenshtein("carrrot", "carrots", 2, 3, 4)6

« More PHP strings functions