Skip to content

Commit

Permalink
feat: add isEqual (#396)
Browse files Browse the repository at this point in the history
* feat: add isEqual

* add test cace

* add should throw error

* add

* add case

* add case

* add case

* add case

* case

* improve

* circular references now return false

* add warning

* console.error test case

* fix case

* case

* fix case

* fix case

* del comment
  • Loading branch information
yoyo837 committed Dec 19, 2022
1 parent eadf1dc commit da90898
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/isEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import warning from './warning';

/**
* Deeply compares two object literals.
*/
function isEqual(obj1: any, obj2: any): boolean {
// https://github.com/mapbox/mapbox-gl-js/pull/5979/files#diff-fde7145050c47cc3a306856efd5f9c3016e86e859de9afbd02c879be5067e58f
const refSet = new Set<any>();
function deepEqual(a: any, b: any): boolean {
const circular = refSet.has(a);
warning(!circular, 'Warning: There may be circular references');
if (circular) {
return false;
}
if (a === b) {
return true;
}
refSet.add(a);
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) {
return false;
}
}
return true;
}
if (a && b && typeof a === 'object' && typeof b === 'object') {
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) {
return false;
}
return keys.every(key => deepEqual(a[key], b[key]));
}
// other
return false;
}

return deepEqual(obj1, obj2);
}

export default isEqual;
79 changes: 79 additions & 0 deletions src/test/isEqual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import isEqual from '../isEqual';
import warning from '../warning';

describe('isEqual', () => {
let errorSpy: jest.SpyInstance;

beforeAll(() => {
errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
});

afterEach(() => {
errorSpy.mockReset();
});

afterAll(() => {
errorSpy.mockRestore();
});

it('should equal', () => {
const valueIsEqual = isEqual(
{ a: 1, b: 2, c: [1, 2] },
{ a: 1, b: 2, c: [1, 2] },
);
expect(valueIsEqual).toBe(true);
});

const obj = { a: 1, b: 2, c: [1, 2], obj: null };
it('should equal 2', () => {
const valueIsEqual = isEqual(obj, obj);
expect(valueIsEqual).toBe(true);
});

it('should not equal', () => {
const valueIsEqual = isEqual(
{ a: 1, b: 2, c: [2, 3] },
{ a: 1, b: 'x', c: [1, 2] },
);
expect(valueIsEqual).toBe(false);
});

it('should not equal 2', () => {
const valueIsEqual = isEqual(
{ a: 1, c: [2, 3], b: 2 },
{ a: 1, c: [1, 2], b: 'x' },
);
expect(valueIsEqual).toBe(false);
});

it('should not equal 3', () => {
const valueIsEqual = isEqual(
{ a: 1, c: [1, 2], b: 2 },
{ a: 1, c: [1], b: 'x' },
);
expect(valueIsEqual).toBe(false);
});

it('should not equal 4', () => {
const valueIsEqual = isEqual({ a: 1, b: { c: 2 } }, { a: 1, b: 1 });
expect(valueIsEqual).toBe(false);
});

it('should not equal 5', () => {
const valueIsEqual = isEqual(
{ a: 1, b: { c: 2 } },
{ a: 1, b: { c: 2 }, c: 1 },
);
expect(valueIsEqual).toBe(false);
});

it('should not equal 6', () => {
obj.obj = obj;
const obj2 = { a: 1, b: 2, c: [1, 2], obj: null };
warning(false, 'error');
expect(errorSpy).toHaveBeenCalledWith('Warning: error');

const valueIsEqual = isEqual(obj, obj2);
expect(valueIsEqual).toBe(false);
});
});

0 comments on commit da90898

Please sign in to comment.