- var edits = diff(a, b);
- var partial = edits.slice(0, Math.round(p * edits.length));
- return 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.
+ if (a.match && a.match(MULTI) || b.match && b.match(MULTI)) {
+ var ca = a.match(GLYPH) || [];
+ var cb = b.match(GLYPH) || [];
+ return diffLerp(ca, cb, p).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(b, a);
+ var partial = edits.slice(0, Math.round((1 - p) * edits.length));
+ return patch(partial, b);