File tree Expand file tree Collapse file tree 1 file changed +24
-11
lines changed Expand file tree Collapse file tree 1 file changed +24
-11
lines changed Original file line number Diff line number Diff line change 1
1
const hasOwn = Object . prototype . hasOwnProperty
2
2
3
- export default function shallowEqual ( a , b ) {
4
- if ( a === b ) return true
3
+ function is ( x , y ) {
4
+ if ( x === y ) {
5
+ return x !== 0 || y !== 0 || 1 / x === 1 / y
6
+ } else {
7
+ return x !== x && y !== y
8
+ }
9
+ }
10
+
11
+ export default function shallowEqual ( objA , objB ) {
12
+ if ( is ( objA , objB ) ) return true
5
13
6
- let countA = 0
7
- let countB = 0
8
-
9
- for ( let key in a ) {
10
- if ( hasOwn . call ( a , key ) && a [ key ] !== b [ key ] ) return false
11
- countA ++
14
+ if ( typeof objA !== 'object' || objA === null ||
15
+ typeof objB !== 'object' || objB === null ) {
16
+ return false
12
17
}
13
18
14
- for ( let key in b ) {
15
- if ( hasOwn . call ( b , key ) ) countB ++
19
+ const keysA = Object . keys ( objA )
20
+ const keysB = Object . keys ( objB )
21
+
22
+ if ( keysA . length !== keysB . length ) return false
23
+
24
+ for ( let i = 0 ; i < keysA . length ; i ++ ) {
25
+ if ( ! hasOwn . call ( objB , keysA [ i ] ) ||
26
+ ! is ( objA [ keysA [ i ] ] , objB [ keysA [ i ] ] ) ) {
27
+ return false
28
+ }
16
29
}
17
30
18
- return countA === countB
31
+ return true
19
32
}
You can’t perform that action at this time.
0 commit comments