Skip to content

Commit

Permalink
Fix issue with strings bookended with the same characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmacarthur committed May 16, 2022
1 parent 65999df commit db6633c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ const result = striff(str1, str2);
}
```

### Strings w/ Duplicate Characters
### Strings w/ Duplicate, Consecutive Characters

Handling strings with duplicate, consecutive characters removed is a little weird. For strings whose characters were changed at the _end_, the indices will be grouped together at the end of the string.
For strings whose characters were changed at the _end_, the indices will be grouped together at the end of the string.

#### Input

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "striff",
"description": "Real simple string diffing.",
"author": "Alex MacArthur (https://macarthur.me)",
"version": "1.0.3",
"version": "1.0.4",
"main": "dist/index.umd.js",
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
Expand Down
64 changes: 64 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,67 @@ describe("characters are changed and added", () => {
]);
});
});

describe("multiple subsequent characters are in middle", () => {
it("simple example - added", () => {
const { added, removed } = striff("yxy", "yxxxy");

expect(added).toHaveLength(2);
expect(removed).toHaveLength(0);
expect(added).toEqual([
{
value: "x",
index: 2,
},
{
value: "x",
index: 3,
},
]);
});

it("simple example - removed", () => {
const { added, removed } = striff("yxxxy", "yxy");

expect(removed).toHaveLength(2);
expect(added).toHaveLength(0);
expect(removed).toEqual([
{
value: "x",
index: 1,
},
{
value: "x",
index: 2,
},
]);
});

it("line breaks are used", () => {
const str1 = `
xxx
22
111
`;
const str2 = `
xxx
2222
111
`;

const { added, removed } = striff(str1, str2);

expect(added).toHaveLength(2);
expect(removed).toHaveLength(0);
expect(added).toEqual([
{
value: "2",
index: 7,
},
{
value: "2",
index: 8,
}
]);
});
});
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ let getDiff = (partsArr: Character[][], arr1: Character[], arr2: Character[]) =>
let originalItem = arr1[partItem.index];

// The first string character has already been matched
// with a second string character. Something's up.
// if (
// originalItem.pointsTo &&
// originalItem.pointsTo !== arr2[characterIndex].ref
// ) {
// console.log('This one\'s already been matched to something, bud:', partItem.value);
// }
// with a second string character. Skip it!
if (
originalItem.pointsTo &&
originalItem.pointsTo !== arr2[characterIndex].ref
) {
return;
}

// Create a two-way binding between the characters:
// Point the first string character to the second's ref.
Expand All @@ -51,7 +51,7 @@ let getDiff = (partsArr: Character[][], arr1: Character[], arr2: Character[]) =>
// already matched the string and had their items set to the
// `matched` store. We need to find those indicies and unset them.
for (let i = result.index; i < result.index + pastMatchLength; i++) {
matched.delete(arr1[i].ref);
matched.delete(arr1[i]?.ref);
}

// Throw each of the matched characters into storage.
Expand Down Expand Up @@ -81,7 +81,6 @@ let getDiff = (partsArr: Character[][], arr1: Character[], arr2: Character[]) =>

let striff = (str1: string, str2: string): DiffResult => {
let strArr1 = toCharacters(str1);

let strArr2 = toCharacters(str2).map((char, index) => {
let str1Char = strArr1[index];

Expand Down

0 comments on commit db6633c

Please sign in to comment.