[d565449] | 1 | (function() {
|
---|
| 2 | 'use strict';
|
---|
| 3 |
|
---|
| 4 | var collator;
|
---|
| 5 | try {
|
---|
| 6 | collator = (typeof Intl !== "undefined" && typeof Intl.Collator !== "undefined") ? Intl.Collator("generic", { sensitivity: "base" }) : null;
|
---|
| 7 | } catch (err){
|
---|
| 8 | console.log("Collator could not be initialized and wouldn't be used");
|
---|
| 9 | }
|
---|
| 10 | // arrays to re-use
|
---|
| 11 | var prevRow = [],
|
---|
| 12 | str2Char = [];
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Based on the algorithm at http://en.wikipedia.org/wiki/Levenshtein_distance.
|
---|
| 16 | */
|
---|
| 17 | var Levenshtein = {
|
---|
| 18 | /**
|
---|
| 19 | * Calculate levenshtein distance of the two strings.
|
---|
| 20 | *
|
---|
| 21 | * @param str1 String the first string.
|
---|
| 22 | * @param str2 String the second string.
|
---|
| 23 | * @param [options] Additional options.
|
---|
| 24 | * @param [options.useCollator] Use `Intl.Collator` for locale-sensitive string comparison.
|
---|
| 25 | * @return Integer the levenshtein distance (0 and above).
|
---|
| 26 | */
|
---|
| 27 | get: function(str1, str2, options) {
|
---|
| 28 | var useCollator = (options && collator && options.useCollator);
|
---|
| 29 |
|
---|
| 30 | var str1Len = str1.length,
|
---|
| 31 | str2Len = str2.length;
|
---|
| 32 |
|
---|
| 33 | // base cases
|
---|
| 34 | if (str1Len === 0) return str2Len;
|
---|
| 35 | if (str2Len === 0) return str1Len;
|
---|
| 36 |
|
---|
| 37 | // two rows
|
---|
| 38 | var curCol, nextCol, i, j, tmp;
|
---|
| 39 |
|
---|
| 40 | // initialise previous row
|
---|
| 41 | for (i=0; i<str2Len; ++i) {
|
---|
| 42 | prevRow[i] = i;
|
---|
| 43 | str2Char[i] = str2.charCodeAt(i);
|
---|
| 44 | }
|
---|
| 45 | prevRow[str2Len] = str2Len;
|
---|
| 46 |
|
---|
| 47 | var strCmp;
|
---|
| 48 | if (useCollator) {
|
---|
| 49 | // calculate current row distance from previous row using collator
|
---|
| 50 | for (i = 0; i < str1Len; ++i) {
|
---|
| 51 | nextCol = i + 1;
|
---|
| 52 |
|
---|
| 53 | for (j = 0; j < str2Len; ++j) {
|
---|
| 54 | curCol = nextCol;
|
---|
| 55 |
|
---|
| 56 | // substution
|
---|
| 57 | strCmp = 0 === collator.compare(str1.charAt(i), String.fromCharCode(str2Char[j]));
|
---|
| 58 |
|
---|
| 59 | nextCol = prevRow[j] + (strCmp ? 0 : 1);
|
---|
| 60 |
|
---|
| 61 | // insertion
|
---|
| 62 | tmp = curCol + 1;
|
---|
| 63 | if (nextCol > tmp) {
|
---|
| 64 | nextCol = tmp;
|
---|
| 65 | }
|
---|
| 66 | // deletion
|
---|
| 67 | tmp = prevRow[j + 1] + 1;
|
---|
| 68 | if (nextCol > tmp) {
|
---|
| 69 | nextCol = tmp;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | // copy current col value into previous (in preparation for next iteration)
|
---|
| 73 | prevRow[j] = curCol;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // copy last col value into previous (in preparation for next iteration)
|
---|
| 77 | prevRow[j] = nextCol;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | else {
|
---|
| 81 | // calculate current row distance from previous row without collator
|
---|
| 82 | for (i = 0; i < str1Len; ++i) {
|
---|
| 83 | nextCol = i + 1;
|
---|
| 84 |
|
---|
| 85 | for (j = 0; j < str2Len; ++j) {
|
---|
| 86 | curCol = nextCol;
|
---|
| 87 |
|
---|
| 88 | // substution
|
---|
| 89 | strCmp = str1.charCodeAt(i) === str2Char[j];
|
---|
| 90 |
|
---|
| 91 | nextCol = prevRow[j] + (strCmp ? 0 : 1);
|
---|
| 92 |
|
---|
| 93 | // insertion
|
---|
| 94 | tmp = curCol + 1;
|
---|
| 95 | if (nextCol > tmp) {
|
---|
| 96 | nextCol = tmp;
|
---|
| 97 | }
|
---|
| 98 | // deletion
|
---|
| 99 | tmp = prevRow[j + 1] + 1;
|
---|
| 100 | if (nextCol > tmp) {
|
---|
| 101 | nextCol = tmp;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | // copy current col value into previous (in preparation for next iteration)
|
---|
| 105 | prevRow[j] = curCol;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | // copy last col value into previous (in preparation for next iteration)
|
---|
| 109 | prevRow[j] = nextCol;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | return nextCol;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | };
|
---|
| 116 |
|
---|
| 117 | // amd
|
---|
| 118 | if (typeof define !== "undefined" && define !== null && define.amd) {
|
---|
| 119 | define(function() {
|
---|
| 120 | return Levenshtein;
|
---|
| 121 | });
|
---|
| 122 | }
|
---|
| 123 | // commonjs
|
---|
| 124 | else if (typeof module !== "undefined" && module !== null && typeof exports !== "undefined" && module.exports === exports) {
|
---|
| 125 | module.exports = Levenshtein;
|
---|
| 126 | }
|
---|
| 127 | // web worker
|
---|
| 128 | else if (typeof self !== "undefined" && typeof self.postMessage === 'function' && typeof self.importScripts === 'function') {
|
---|
| 129 | self.Levenshtein = Levenshtein;
|
---|
| 130 | }
|
---|
| 131 | // browser main thread
|
---|
| 132 | else if (typeof window !== "undefined" && window !== null) {
|
---|
| 133 | window.Levenshtein = Levenshtein;
|
---|
| 134 | }
|
---|
| 135 | }());
|
---|
| 136 |
|
---|