import DiffMatchPatch from "diff-match-patch"; const dmp = new DiffMatchPatch(); export function createDiff(oldText: string, newText: string): string { const patches = dmp.patch_make(oldText, newText); return dmp.patch_toText(patches); } export function applyDiff(text: string, patchText: string): string { const patches = dmp.patch_fromText(patchText); const [result, applied] = dmp.patch_apply(patches, text); const failed = applied.filter((ok) => !ok); if (failed.length > 0) { throw new Error(`Failed to apply ${failed.length} patch(es)`); } return result; }