Created
May 14, 2016 21:18
-
-
Save JDMcKinstry/af414d0c4a020a81839ac3b797638f89 to your computer and use it in GitHub Desktop.
compare a string to given param to see if its similar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* String.prototype.similarTo */ | |
(function() { // compare a string to given param to see if its similar | |
// pass 2nd param true to have it check in reverse as well | |
function a(a, c) { | |
if (0 == a.length) return c.length; | |
if (0 == c.length) return a.length; | |
for (var d = [], e = 0; e <= c.length; e++) d[e] = [e]; | |
for (var f = 0; f <= a.length; f++) d[0][f] = f; | |
for (e = 1; e <= c.length; e++) | |
for (f = 1; f <= a.length; f++) c.charAt(e - 1) == a.charAt(f - 1) ? d[e][f] = d[e - 1][f - 1] : d[e][f] = Math.min(d[e - 1][f - 1] + 1, Math.min(d[e][f - 1] + 1, d[e - 1][f] + 1)); | |
return d[c.length][a.length] | |
} | |
function g(b, c) { | |
var d = this.toString(), | |
e = a(d, b); | |
if (!c) var f = b.reverse(), | |
d = a(d, f), | |
e = e < d ? e : d; | |
return e; | |
} | |
Object.defineProperty && !String.prototype.hasOwnProperty("similarTo") && | |
Object.defineProperty(String.prototype, "similarTo", { | |
value: g | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment