Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(graphin):change didupdate logic
- Loading branch information
1 parent
2931dc2
commit a1975c7
Showing
5 changed files
with
135 additions
and
23 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
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
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
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,81 @@ | ||
import deepEqual from '../deepEqual'; | ||
|
||
describe('compare ', () => { | ||
it('compare', () => { | ||
expect(deepEqual('a', 'a')).toEqual(true); | ||
expect(deepEqual(0, 0)).toEqual(true); | ||
expect(deepEqual(1, 1)).toEqual(true); | ||
expect(deepEqual(NaN, NaN)).toEqual(true); | ||
expect(deepEqual(undefined, undefined)).toEqual(true); | ||
expect(deepEqual({}, {})).toEqual(true); | ||
expect(deepEqual([], [])).toEqual(true); | ||
expect( | ||
deepEqual( | ||
() => { | ||
console.log('hello'); | ||
}, | ||
() => { | ||
console.log('hello'); | ||
}, | ||
), | ||
).toEqual(true); | ||
expect( | ||
deepEqual( | ||
{ | ||
name: 'pomelo', | ||
}, | ||
{ | ||
name: 'pomelo', | ||
}, | ||
), | ||
).toEqual(true); | ||
}); | ||
it('compare complex object', () => { | ||
const a = { | ||
name: 'pomelo', | ||
do: () => { | ||
console.log('do.'); | ||
}, | ||
circle: { | ||
a: [ | ||
'a', | ||
{ | ||
name: 'a', | ||
}, | ||
], | ||
}, | ||
}; | ||
const b = { | ||
name: 'pomelo', | ||
do: () => { | ||
console.log('do.'); | ||
}, | ||
circle: { | ||
a: [ | ||
'a', | ||
{ | ||
name: 'a', | ||
}, | ||
], | ||
}, | ||
}; | ||
const c = { | ||
name: 'pomelo', | ||
do: () => { | ||
console.log('do'); | ||
}, | ||
circle: { | ||
a: [ | ||
'a', | ||
{ | ||
name: 'a', | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
expect(deepEqual(a, b)).toEqual(true); | ||
expect(deepEqual(a, c)).toEqual(false); | ||
expect(deepEqual(a.circle, c.circle)).toEqual(true); | ||
}); | ||
}); |
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,38 @@ | ||
const deepEqual = (x: any, y: any): boolean => { | ||
/** 1. NaN */ | ||
if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') { | ||
return true; | ||
} | ||
/** 2. primitives and function reference */ | ||
if (x === y) { | ||
return true; | ||
} | ||
/** 3.Function,Date,RegExp,String,Number */ | ||
if ( | ||
(typeof x === 'function' && typeof y === 'function') || | ||
(x instanceof Date && y instanceof Date) || | ||
(x instanceof RegExp && y instanceof RegExp) || | ||
(x instanceof String && y instanceof String) || | ||
(x instanceof Number && y instanceof Number) | ||
) { | ||
return x.toString() === y.toString(); | ||
} | ||
/** 4.plain object */ | ||
if (!(x instanceof Object && y instanceof Object)) { | ||
return false; | ||
} | ||
const isEqualArray = []; | ||
const xKey = Object.keys(x); | ||
const yKey = Object.keys(y); | ||
if (xKey.length !== yKey.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < xKey.length; i++) { | ||
const key = xKey[i]; | ||
const isEqual = deepEqual(x[key], y[key]); | ||
isEqualArray.push(isEqual); | ||
} | ||
return isEqualArray.every(c => c); | ||
}; | ||
|
||
export default deepEqual; |