Reverse edit strings so they change LTR, like fastLerp.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Tue, 13 May 2014 19:28:48 +0000 (21:28 +0200)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Tue, 13 May 2014 19:28:48 +0000 (21:28 +0200)
string-lerp.js
tests/string-lerp.js

index 5e8201c..2096a07 100644 (file)
         return s;
     }
 
+    function reverse (s) {
+        return s.split("").reverse().join("");
+    }
+
     function diffLerp(a, b, p) {
         /** Interpolate between two strings based on edit distance
 
             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;
index 2e7f01f..8ef0bc7 100644 (file)
@@ -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));
     }});
 }});