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 b7582a7
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 15 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);
});
});
64 changes: 49 additions & 15 deletions packages/common/src/is-equal.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,60 @@ export function isEqual(a: unknown, b: unknown): boolean {
}

if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
return areArraysEqual(a, b);
}

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 areArraysEqual<T>(a: T[], b: T[]): boolean {
if (a.length !== b.length) {
return false;
}

return a.every((item: unknown, index: number) => {
if (isNil(item) && isNil(b[index])) {
return true;
}

return a.every((item: unknown, index: number) => {
if (isNil(item) && isNil(b[index])) {
return true;
}
if (isString(item) && isString(b[index])) {
return item === b[index];
}

if (isString(item) && isString(b[index])) {
return item === b[index];
}
if (isNumber(item) && isNumber(b[index])) {
return item === b[index];
}

if (isNumber(item) && isNumber(b[index])) {
return item === b[index];
}
return JSON.stringify(item) === JSON.stringify(b[index]);
});
}

return JSON.stringify(item) === JSON.stringify(b[index]);
});
function areSetsEqual<T>(a: Set<T>, b: Set<T>): boolean {
if (a.size !== b.size) {
return false;
}

return JSON.stringify(a) === JSON.stringify(b);
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 b7582a7

Please sign in to comment.