Skip to content

Commit

Permalink
fix: extend is-equal check for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 20, 2023
1 parent d59c647 commit 406d48b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/utils/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,42 @@ export function isEqual(x: any, y: any): boolean {
return x.toString() === y.toString();
}

if (!isObject(x) || !isObject(y)) {
return false;
}
if (
isObject(x) &&
isObject(y)
) {
const keysX = Reflect.ownKeys(x) as string[];
const keysY = Reflect.ownKeys(y) as string[];
if (keysX.length !== keysY.length) {
return false;
}

const keysX = Reflect.ownKeys(x) as string[];
const keysY = Reflect.ownKeys(y) as string[];
if (keysX.length !== keysY.length) {
return false;
for (let i = 0; i < keysX.length; i++) {
const key = keysX[i];
if (!Reflect.has(y, key) || !isEqual(x[key], y[key])) {
return false;
}
}

return true;
}

for (let i = 0; i < keysX.length; i++) {
const key = keysX[i];
if (!Reflect.has(y, key) || !isEqual(x[key], y[key])) {
if (
Array.isArray(x) &&
Array.isArray(y)
) {
if (x.length !== y.length) {
return false;
}

for (let i = 0; i < x.length; i++) {
if (!isEqual(x[i], y[i])) {
return false;
}
}

return true;
}

return true;
return false;
}
9 changes: 9 additions & 0 deletions test/unit/utils/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ describe('src/utils/array', function () {
arr = [{foo: 'bar'}, {foo: 'baz'}]
expect(distinctArray(arr)).toEqual(arr);

arr = [['foo', 'bar'], ['foo']];
expect(distinctArray(arr)).toEqual([['foo', 'bar'], ['foo']]);

arr = [['foo', 'bar'], ['foo', 'bar']];
expect(distinctArray(arr)).toEqual([['foo', 'bar']]);

arr = [['foo', 'bar'], ['bar', 'foo']];
expect(distinctArray(arr)).toEqual([['foo', 'bar'], ['bar', 'foo']]);

let circ : any = {foo: 'bar'};
circ.bar = circ;

Expand Down

0 comments on commit 406d48b

Please sign in to comment.