From 6833b62b67e23e608b119fe31dcd1937ac8ff81d Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Tue, 13 May 2014 17:18:59 +0200 Subject: [PATCH] Better function names. --- string-lerp.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/string-lerp.js b/string-lerp.js index 84cc9b9..0a3d452 100644 --- a/string-lerp.js +++ b/string-lerp.js @@ -3,10 +3,6 @@ var MAX_MATRIX_SIZE = 256 * 256; - function createMatrix(i, j) { - return new Uint32Array(i * j); - } - function levenshteinMatrix(s, t) { /** Calculate the Levenshtein edit distance matrix for two strings @@ -16,7 +12,7 @@ */ var m = s.length + 1; var n = t.length + 1; - var d = createMatrix(m, n); + var d = new Uint32Array(m * n); var i, j; for (i = 1; i < m; ++i) d[n * i] = i; @@ -33,12 +29,12 @@ return d; } - function editPath(d, s, t) { - /** Given a Levenshtein result matrix, trace the shortest edit */ + function editPath(d, t) { + /** Given a Levenshtein matrix and target, create an edit list */ var path = [] - var i = s.length; var j = t.length; var n = j + 1; + var i = d.length / n - 1; while (i || j) { var sub = (i && j) ? d[n * (i - 1) + j - 1] : Infinity; var del = i ? d[n * (i - 1) + j] : Infinity; @@ -58,7 +54,12 @@ return path; } - function applyEdits(edits, s) { + function diff(s, t) { + /** Create a diff between string s and t */ + return editPath(levenshteinMatrix(s, t), t); + } + + function patch(edits, s) { /** Apply the list of edits to s */ var i; for (i = 0; i < edits.length; ++i) { @@ -87,9 +88,9 @@ compute the edits. It is not recommended for strings longer than a few hundred characters. */ - var edits = editPath(levenshteinMatrix(a, b), a, b); + var edits = diff(a, b); var partial = edits.slice(0, Math.round(p * edits.length)); - return applyEdits(partial, a); + return patch(partial, a); } function fastLerp(a, b, p) { @@ -116,10 +117,10 @@ } exports.levenshteinMatrix = levenshteinMatrix; - exports.editPath = editPath; + exports.patch = patch; + exports.diff = diff; exports.fastLerp = fastLerp; exports.slowLerp = slowLerp; - exports.applyEdits = applyEdits; exports.lerp = lerp; })(typeof exports === "undefined" ? (this.stringLerp = {}) : exports); -- 2.30.2