Skip to content

Commit

Permalink
chore: updated isEqual function to support sets and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed May 3, 2024
1 parent 3a01fad commit e4fecdb
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
80 changes: 80 additions & 0 deletions packages/common/src/is-equal.function.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,84 @@ describe('is-equal.function.ts', () => {
const b: unknown[] = Array.from(Array(15).keys()).map((index: number) => ({ id: index, name: index }));
expect(isEqual(a, b)).toBe(false);
});

it('should be true if sets of strings are equal', () => {
const a: Set<string> = new Set<string>(['1', '2', '3']);
const b: Set<string> = new Set<string>(['1', '2', '3']);
expect(isEqual(a, b)).toBe(true);
});

it('should be false if sets of strings are not equal', () => {
const a: Set<string> = new Set<string>(['1', '2', '3']);
const b: Set<string> = new Set<string>(['1', '2', '3', '4']);
expect(isEqual(a, b)).toBe(false);
});

it('should be true if sets of numbers are equal', () => {
const a: Set<number> = new Set<number>([1, 2, 3]);
const b: Set<number> = new Set<number>([1, 2, 3]);
expect(isEqual(a, b)).toBe(true);
});

it('should be false if sets of numbers are not equal', () => {
const a: Set<number> = new Set<number>([1, 2, 3]);
const b: Set<number> = new Set<number>([1, 2, 3, 4]);
expect(isEqual(a, b)).toBe(false);
});

it('should be true if maps of strings are equal', () => {
const a: Map<string, number> = new Map<string, number>([
['1', 1],
['2', 2],
['3', 3]
]);
const b: Map<string, number> = new Map<string, number>([
['1', 1],
['2', 2],
['3', 3]
]);
expect(isEqual(a, b)).toBe(true);
});

it('should be false if maps of strings are not equal', () => {
const a: Map<string, number> = new Map<string, number>([
['1', 1],
['2', 2],
['3', 3]
]);
const b: Map<string, number> = new Map<string, number>([
['1', 1],
['2', 2],
['3', 4]
]);
expect(isEqual(a, b)).toBe(false);
});

it('should be true if maps of objects are equal', () => {
const a: Map<string, unknown> = new Map<string, unknown>([
['1', { id: 1 }],
['2', { id: 2 }],
['3', { id: 3 }]
]);
const b: Map<string, unknown> = new Map<string, unknown>([
['1', { id: 1 }],
['2', { id: 2 }],
['3', { id: 3 }]
]);
expect(isEqual(a, b)).toBe(true);
});

it('should be false if maps of objects are not equal', () => {
const a: Map<string, unknown> = new Map<string, unknown>([
['1', { id: 1 }],
['2', { id: 2 }],
['3', { id: 3 }]
]);
const b: Map<string, unknown> = new Map<string, unknown>([
['1', { id: 1 }],
['2', { id: 2 }],
['3', { id: 4 }]
]);
expect(isEqual(a, b)).toBe(false);
});
});
30 changes: 30 additions & 0 deletions packages/common/src/is-equal.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,35 @@ export function isEqual(a: unknown, b: unknown): boolean {
});
}

if (a instanceof Set && b instanceof Set) {
return areSetsEqual(a, b);
}

if (a instanceof Map && b instanceof Map) {
return areMapsEqual(a, b);
}

return JSON.stringify(a) === JSON.stringify(b);
}

function areSetsEqual<T>(a: Set<T>, b: Set<T>): boolean {
if (a.size !== b.size) {
return false;
}

return [...a].every((item: T) => b.has(item));
}

function areMapsEqual<T, K>(a: Map<T, K>, b: Map<T, K>): boolean {
if (a.size !== b.size) {
return false;
}

for (const [key, value] of a) {
if (!b.has(key) || !isEqual(value, b.get(key))) {
return false;
}
}

return true;
}

0 comments on commit e4fecdb

Please sign in to comment.