Skip to content

Commit

Permalink
Don't merge diff when it's not spaces even if it's surrounded by word…
Browse files Browse the repository at this point in the history
…-diff (#1032)
  • Loading branch information
FujiHaruka authored Jul 13, 2021
1 parent bac1c3e commit 1c96a57
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion testing/_diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export function diffstr(A: string, B: string) {
).map((result, i, t) => {
if (
(result.type === DiffType.common) && (t[i - 1]) &&
(t[i - 1]?.type === t[i + 1]?.type)
(t[i - 1]?.type === t[i + 1]?.type) && /\s+/.test(result.value)
) {
result.type = t[i - 1].type;
}
Expand Down
81 changes: 81 additions & 0 deletions testing/_diff_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,84 @@ Deno.test({
]);
},
});

Deno.test({
name: `"3.14" vs "2.71" (diffstr)`,
fn(): void {
const diffResult = diffstr("3.14", "2.71");
assertEquals(diffResult, [
{
type: "removed",
value: "3.14\n",
details: [
{
type: "removed",
value: "3",
},
{
type: "common",
value: ".",
},
{
type: "removed",
value: "14",
},
{
type: "common",
value: "\n",
},
],
},
{
type: "added",
value: "2.71\n",
details: [
{
type: "added",
value: "2",
},
{
type: "common",
value: ".",
},
{
type: "added",
value: "71",
},
{
type: "common",
value: "\n",
},
],
},
]);
},
});

Deno.test({
name: `single line "a b" vs "c d" (diffstr)`,
fn(): void {
const diffResult = diffstr("a b", "c d");
assertEquals(diffResult, [
{
type: "removed",
value: "a b\n",
details: [
{ type: "removed", value: "a" },
{ type: "removed", value: " " },
{ type: "removed", value: "b" },
{ type: "common", value: "\n" },
],
},
{
type: "added",
value: "c d\n",
details: [
{ type: "added", value: "c" },
{ type: "added", value: "d" },
{ type: "common", value: "\n" },
],
},
]);
},
});

0 comments on commit 1c96a57

Please sign in to comment.