Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-utility",
"version": "4.7.0",
"version": "4.7.1",
"license": "LGPL-3.0",
"author": "shiy2008@gmail.com",
"description": "Web front-end toolkit based on TypeScript",
Expand Down
11 changes: 5 additions & 6 deletions source/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,13 @@ export enum DiffStatus {
}

export function diffKeys<T extends IndexKey>(oldList: T[], newList: T[]) {
const oldSet = new Set(oldList),
newSet = new Set(newList);
const map = {} as Record<T, DiffStatus>;

for (const item of oldList) map[item] = DiffStatus.Old;

for (const item of newList) {
map[item] ||= 0;
map[item] += DiffStatus.New;
}
for (const item of oldSet.difference(newSet)) map[item] = DiffStatus.Old;
for (const item of oldSet.intersection(newSet)) map[item] = DiffStatus.Same;
for (const item of newSet.difference(oldSet)) map[item] = DiffStatus.New;
Comment thread
TechQuery marked this conversation as resolved.

return {
map,
Expand Down
11 changes: 11 additions & 0 deletions test/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ describe('Data', () => {
});
});

it('should ignore duplicate keys when comparing key arrays', () => {
expect(diffKeys(['a', 'a', 'b'], ['b', 'b', 'c', 'c'])).toEqual({
map: { a: DiffStatus.Old, b: DiffStatus.Same, c: DiffStatus.New },
group: {
[DiffStatus.Old]: [['a', DiffStatus.Old]],
[DiffStatus.Same]: [['b', DiffStatus.Same]],
[DiffStatus.New]: [['c', DiffStatus.New]]
}
});
});

it('should detect an Object whether is Array-like or not', () => {
expect(likeArray(NaN)).toBe(false);
expect(likeArray('a')).toBe(true);
Expand Down