-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (99 loc) · 2.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import _ from 'lodash'
const debug = false
const log = (...args) => {
if (debug) {
console.log('shallowEqual:: ', new Date(), ' - ', ...args)
}
}
const globalExcludes = [
'size',
'_root',
'__ownerID',
'__hash',
'__altered',
'descriptors',
'navigation',
'emitters',
'handlers',
]
const shallowEqualJS = (props, nextProps, keys) => {
const shouldUpdate = false
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
let oldValue = props[key]
const newValue = nextProps[key]
if (typeof newValue === 'function') {
continue
}
const isImmutable = !!(newValue && newValue.get && newValue.getIn)
if (isImmutable) {
oldValue = oldValue || new Map()
if (!_.isEqual(newValue, oldValue)) {
log('UPDATED:: ', key, 'TYPEOF:: immutable')
return true
}
}
if (typeof newValue !== 'undefined' && oldValue === 'undefined') {
log('UPDATED:: ', key, 'TYPEOF:: prev was undefined')
return true
}
if (typeof newValue === 'object') {
if (!_.isEqual(newValue, oldValue)) {
log('UPDATED:: ', key, 'TYPEOF:: object')
return true
}
}
if (typeof newValue === 'string'
|| typeof newValue === 'number'
|| typeof newValue === 'boolean'
) {
if (newValue !== oldValue) {
log('UPDATED:: ', key, 'TYPEOF:: string|number|boolean', ' prev::', oldValue, ' new::', newValue)
return true
}
}
if (newValue !== oldValue) {
log('UPDATED:: ', key, 'TYPEOF:: just not equals')
return true
}
}
return shouldUpdate
}
const shallowCompare = (props, nextProps) => {
const oldProps = _.omit(props, globalExcludes)
const newProps = _.omit(nextProps, globalExcludes)
const keys = Object.keys(props)
return shallowEqualJS(oldProps, newProps, keys)
}
const shallowCompareWithState = (data, nextProps, nextState) => {
const { props = {}, state = {} } = data
const stateKeys = Object.keys(state)
for (let i = 0; i < stateKeys.length; i++) {
const key = stateKeys[i]
if (state[key] !== nextState[key]) {
return true
}
}
return shallowCompare(props, nextProps)
}
const shallowCompareOnly = keys => (props, nextProps) => {
const oldProps = {}
const newProps = {}
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
oldProps[key] = props[key]
newProps[key] = nextProps[key]
}
return shallowCompare(oldProps, newProps)
}
const shallowCompareExclude = keys => (props, nextProps) => {
const oldProps = _.omit(props, keys)
const newProps = _.omit(nextProps, keys)
return shallowCompare(oldProps, newProps)
}
export {
shallowCompare,
shallowCompareWithState,
shallowCompareOnly,
shallowCompareExclude,
}