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
*/
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;
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;
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) {
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) {
}
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);