Skip to content

Commit

Permalink
repair stringsDiffToOps broken case
Browse files Browse the repository at this point in the history
  • Loading branch information
cudr committed Jul 31, 2019
1 parent 13fd876 commit a90141f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/compare/stringDiffToOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ const stringDiffToOps = (
type,
totalOffset,
null,
i === listOps.length - 1 ? split + d : d + split
i === listOps.length - 1 && listOffset === originList.length
? split + d
: d + split
)
);

Expand Down
46 changes: 27 additions & 19 deletions test/discretionShift.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import discretionShift from "../src/compare/discretionShift";

type Item = string[][] | string[];

const pair = (pair: Item, match: any): [Item, any] => [pair, match];

const listPairs = [
pair([[""], [""]], [0, 0]),
pair([["", "xyz"], ["bar"]], undefined),
pair([["", "bar"], [" "]], undefined),
pair([["bar"], ["bar"]], [0, 0]),
pair([["", "bar"], ["bar"]], [1, 0]),
pair([["", "", "bar"], [" ", "bar"]], [2, 1]),
pair([["xyz", "abc", "def", "xyz"], ["foo", "bar", "foo", "abc"]], [1, 3])
];

const stringPairs = [
pair(["ghifoo", "xyzbarfoo"], [3, 6]),
pair(["foo", "xyzbarfoo"], [0, 6]),
pair(["", " x"], [1, 0]),
pair([" g", " x"], undefined)
];

describe("discretion shift", () => {
it("find list shift pair", () => {
expect(discretionShift([""], [""])).toStrictEqual([0, 0]);
expect(discretionShift(["", "xyz"], ["bar"])).toBe(undefined);
expect(discretionShift(["", "bar"], [" "])).toBe(undefined);
expect(discretionShift(["bar"], ["bar"])).toStrictEqual([0, 0]);
expect(discretionShift(["", "bar"], ["bar"])).toStrictEqual([1, 0]);
expect(discretionShift(["", "", "bar"], [" ", "bar"])).toStrictEqual([
2,
1
]);
expect(
discretionShift(
["xyz", "abc", "def", "xyz"],
["foo", "bar", "foo", "abc"]
)
).toStrictEqual([1, 3]);
listPairs.forEach(([left, right]) =>
expect(discretionShift(left[0], left[1])).toStrictEqual(right)
);
});

it("find string shift pair", () => {
expect(discretionShift("ghifoo", "xyzbarfoo")).toStrictEqual([3, 6]);
expect(discretionShift("foo", "xyzbarfoo")).toStrictEqual([0, 6]);
expect(discretionShift("", " x")).toStrictEqual([1, 0]);
expect(discretionShift(" g", " x")).toBe(undefined);
stringPairs.forEach(([left, right]) =>
expect(discretionShift(left[0], left[1])).toStrictEqual(right)
);
});
});
2 changes: 1 addition & 1 deletion test/text_source.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const origin =
"early Yet far hath!!! fought with great nature, That you with wisest apple - think on him, Together me remembrance bananas of ourselves! later";
"early Yet far hath!!! fought with great nature, That you with wisest apple - think on himTogether me remembrance bananas of ourselves! later";

export const modifyed =
"Yet so far hath discretion fought with nature That we with wisest sorrow — think on him, Together with remembrance of ourselves.";
Expand Down

0 comments on commit a90141f

Please sign in to comment.