Skip to content

Commit

Permalink
fix: diff now handles complicated change sequences properly
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Nov 10, 2022
1 parent b4d3d9b commit baa3c01
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
54 changes: 38 additions & 16 deletions src/diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe("diff", () =>
});
});

describe("diffText", () =>
describe.only("diffText", () =>
{
it.each([
[ "", "" ],
Expand All @@ -298,21 +298,43 @@ describe("diffText", () =>
expect(diffText(a, b)).toStrictEqual(diff);
});

it("Adjusts indices to account for previous changes.", () =>
it.only.each([
[
"hello",
"goodbye",
[
[ "add", 0, "g" ],
[ "add", 1, "o" ],
[ "add", 2, "o" ],
[ "add", 3, "d" ],
[ "add", 4, "b" ],
[ "add", 5, "y" ],
[ "delete", 6, undefined ],
[ "delete", 7, undefined ],
[ "delete", 7, undefined ],
[ "delete", 7, undefined ]
]
],
[
"hello, world!",
"goodbye, world.",
[
[ "add", 0, "g" ],
[ "add", 1, "o" ],
[ "add", 2, "o" ],
[ "add", 3, "d" ],
[ "add", 4, "b" ],
[ "add", 5, "y" ],
[ "delete", 6, undefined ],
[ "delete", 7, undefined ],
[ "delete", 7, undefined ],
[ "delete", 7, undefined ],
[ "add", 14, "." ],
[ "delete", 15, undefined ]
]
]
])("Adjusts indices to account for previous changes.", (a, b, diff) =>
{
const a = "hello", b = "goodbye";

expect(diffText(a, b)).toStrictEqual([
[ "add", 0, "g" ],
[ "add", 1, "o" ],
[ "add", 2, "o" ],
[ "add", 3, "d" ],
[ "add", 4, "b" ],
[ "add", 5, "y" ],
[ "delete", 6, undefined ],
[ "delete", 8, undefined ],
[ "delete", 9, undefined ],
[ "delete", 10, undefined ]
]);
expect(diffText(a, b)).toStrictEqual(diff);
});
});
19 changes: 9 additions & 10 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const diffText = (a: string, b: string): any =>
};

/**
* An implementation of Wu et al. O(NP) text diff. (See docs/text-diff)
* An adaptation of Wu et al. O(NP) text diff. (See docs/text-diff)
*
* Credit to [this JavaScript implementation](https://github.com/cubicdaiya/onp/blob/master/javascript/onp.js).
*
Expand Down Expand Up @@ -243,8 +243,7 @@ const _diffText = (a: string, b: string, isReversed: boolean): any =>
}

const changeList: [ "add" | "delete", number, string | undefined ][] = [];
let x = 0, y = 0;
let start = 0;
let x = 0, y = 0, index = -1;

for (let i = editPath.length - 1; i >= 0; i--)
{
Expand All @@ -256,19 +255,19 @@ const _diffText = (a: string, b: string, isReversed: boolean): any =>
{
changeList[changeList.length] = [
"delete",
(y - 1) + start,
index,
undefined
];
}
else
{
changeList[changeList.length] = [
"add",
y - 1,
index,
b[y - 1]
];

start++;
index++;
}

y++;
Expand All @@ -279,17 +278,17 @@ const _diffText = (a: string, b: string, isReversed: boolean): any =>
{
changeList[changeList.length] = [
"add",
x - 1,
index,
a[x - 1]
];

start++;
index++;
}
else
{
changeList[changeList.length] = [
"delete",
(x - 1) + start,
index,
undefined
];
}
Expand All @@ -298,7 +297,7 @@ const _diffText = (a: string, b: string, isReversed: boolean): any =>
}
else
{
x++; y++;
x++; y++; index++;
}
}
}
Expand Down

0 comments on commit baa3c01

Please sign in to comment.