From bdb8d784b8c1f08a2be6b67a148cb00619b512ec Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Tue, 13 May 2014 21:28:48 +0200 Subject: [PATCH] Reverse edit strings so they change LTR, like fastLerp. --- string-lerp.js | 8 +++++++- tests/string-lerp.js | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/string-lerp.js b/string-lerp.js index 5e8201c..2096a07 100644 --- a/string-lerp.js +++ b/string-lerp.js @@ -79,6 +79,10 @@ return s; } + function reverse (s) { + return s.split("").reverse().join(""); + } + function diffLerp(a, b, p) { /** Interpolate between two strings based on edit distance @@ -88,9 +92,11 @@ compute the edits. It is not recommended for strings longer than a few hundred characters. */ + a = reverse(a); + b = reverse(b); var edits = diff(a, b); var partial = edits.slice(0, Math.round(p * edits.length)); - return patch(partial, a); + return reverse(patch(partial, a)); } var NUMBERS = /(-?\d+(?:\.\d+)?)/g; diff --git a/tests/string-lerp.js b/tests/string-lerp.js index 2e7f01f..8ef0bc7 100644 --- a/tests/string-lerp.js +++ b/tests/string-lerp.js @@ -73,9 +73,9 @@ JS.Test.describe('diff lerp', function () { with (this) { }}); it("edits strings", function () { with (this) { - assertEqual("Do you like green eggsSam-I-am.", lerp(A, B, 0.3)); - assertEqual("Do you like green Sam-I-am.", lerp(A, B, 0.5)); - assertEqual("do not like them, Sam-I-am.", lerp(A, B, 0.9)); + assertEqual("I do not like treen eggs and ham?", lerp(A, B, 0.3)); + assertEqual("I do not like them, Sggs and ham?", lerp(A, B, 0.5)); + assertEqual("I do not like them, Sam-Iham?", lerp(A, B, 0.9)); }}); }}); -- 2.30.2