-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |