1 var makeString = require('./helper/makeString');
4 * Based on the implementation here: https://github.com/hiddentao/fast-levenshtein
6 module.exports = function levenshtein(str1, str2) {
8 str1 = makeString(str1);
9 str2 = makeString(str2);
12 if (str1 === str2) return 0;
13 if (!str1 || !str2) return Math.max(str1.length, str2.length);
16 var prevRow = new Array(str2.length + 1);
18 // initialise previous row
19 for (var i = 0; i < prevRow.length; ++i) {
23 // calculate current row distance from previous row
24 for (i = 0; i < str1.length; ++i) {
27 for (var j = 0; j < str2.length; ++j) {
31 nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
38 tmp = prevRow[j + 1] + 1;
43 // copy current col value into previous (in preparation for next iteration)
47 // copy last col value into previous (in preparation for next iteration)