From: Joe Wreschnig Date: Tue, 13 May 2014 19:28:48 +0000 (+0200) Subject: Reverse edit strings so they change LTR, like fastLerp. X-Git-Tag: 1.0.0~25 X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=bdb8d784b8c1f08a2be6b67a148cb00619b512ec;p=string-lerp.git Reverse edit strings so they change LTR, like fastLerp. --- 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)); }}); }});