- */
- a = reverse(a);
- b = reverse(b);
- var edits = diff(a, b);
- var partial = edits.slice(0, Math.round(p * edits.length));
- return reverse(patch(partial, a));
+ */
+
+ // If given strings with astral codepoints or combining
+ // characters, split them into arrays of "glyphs" first,
+ // do the edit on the list of "glyphs", and rejoin them.
+ //
+ // This split is not perfect for all languages, but at least
+ // it won't create invalid surrogate pairs or orphaned
+ // combining characters.
+ //
+ // TODO: The way this is called is a hack.
+ if (source.match && (source.match(MULTI) || target.match(MULTI))) {
+ var sourceGlyphs = source.match(GLYPH) || [];
+ var targetGlyphs = target.match(GLYPH) || [];
+ return diffLerp(sourceGlyphs, targetGlyphs, amount).join("");
+ }
+
+ // The edit path works from the string end, forwards, because
+ // that's how Levenshtein edits work. To match LTR reading
+ // direction (and the behavior of fastLerp), swap the strings
+ // and invert the parameter when editing.
+ var edits = diff(target, source, 2, 2, 3);
+ var partial = edits.slice(0, Math.round((1 - amount) * edits.length));
+ return patch(partial, target);