5 * Extend an Object with another Object's properties.
7 * The source objects are specified as additional arguments.
9 * @param dst Object the object to extend.
11 * @return Object the final object.
13 var _extend = function(dst) {
14 var sources = Array.prototype.slice.call(arguments, 1);
15 for (var i=0; i<sources.length; ++i) {
18 if (src.hasOwnProperty(p)) dst[p] = src[p];
25 * Based on the algorithm at http://en.wikipedia.org/wiki/Levenshtein_distance.
29 * Calculate levenshtein distance of the two strings.
31 * @param str1 String the first string.
32 * @param str2 String the second string.
33 * @return Integer the levenshtein distance (0 and above).
35 get: function(str1, str2) {
37 if (str1 === str2) return 0;
38 if (str1.length === 0) return str2.length;
39 if (str2.length === 0) return str1.length;
42 var prevRow = new Array(str2.length + 1),
43 curCol, nextCol, i, j, tmp;
45 // initialise previous row
46 for (i=0; i<prevRow.length; ++i) {
50 // calculate current row distance from previous row
51 for (i=0; i<str1.length; ++i) {
54 for (j=0; j<str2.length; ++j) {
58 nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
65 tmp = prevRow[j + 1] + 1;
70 // copy current col value into previous (in preparation for next iteration)
74 // copy last col value into previous (in preparation for next iteration)
82 * Asynchronously calculate levenshtein distance of the two strings.
84 * @param str1 String the first string.
85 * @param str2 String the second string.
86 * @param cb Function callback function with signature: function(Error err, int distance)
87 * @param [options] Object additional options.
88 * @param [options.progress] Function progress callback with signature: function(percentComplete)
90 getAsync: function(str1, str2, cb, options) {
91 options = _extend({}, {
96 if (str1 === str2) return cb(null, 0);
97 if (str1.length === 0) return cb(null, str2.length);
98 if (str2.length === 0) return cb(null, str1.length);
101 var prevRow = new Array(str2.length + 1),
104 startTime, currentTime;
106 // initialise previous row
107 for (i=0; i<prevRow.length; ++i) {
115 var __calculate = function() {
117 startTime = new Date().valueOf();
118 currentTime = startTime;
120 // keep going until one second has elapsed
121 while (currentTime - startTime < 1000) {
122 // reached end of current row?
123 if (str2.length <= (++j)) {
124 // copy current into previous (in preparation for next iteration)
125 prevRow[j] = nextCol;
127 // if already done all chars
128 if (str1.length <= (++i)) {
129 return cb(null, nextCol);
131 // else if we have more left to do
142 nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
149 tmp = prevRow[j + 1] + 1;
154 // copy current into previous (in preparation for next iteration)
158 currentTime = new Date().valueOf();
161 // send a progress update?
162 if (null !== options.progress) {
164 options.progress.call(null, (i * 100.0/ str1.length));
166 return cb('Progress callback: ' + err.toString());
171 setTimeout(__calculate(), 0);
180 if (typeof define !== "undefined" && define !== null && define.amd) {
186 else if (typeof module !== "undefined" && module !== null) {
187 module.exports = Levenshtein;
190 else if (typeof self !== "undefined" && typeof self.postMessage === 'function' && typeof self.importScripts === 'function') {
191 self.Levenshtein = Levenshtein;
193 // browser main thread
194 else if (typeof window !== "undefined" && window !== null) {
195 window.Levenshtein = Levenshtein;