-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathreducers.js
43 lines (37 loc) · 1.02 KB
/
reducers.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
import { assign } from './functions'
function getId(x) {
return x.id || x
}
/* istanbul ignore next */
function shallowEqual(a, b) {
if (typeof a !== 'object' || typeof b !== 'object') return a === b
if (a === b) return true
if (!a || !b) return false
for (const k in a) {
if (a.hasOwnProperty(k) && (!b.hasOwnProperty(k) || a[k] !== b[k])) {
return false
}
}
for (const k in b) {
if (b.hasOwnProperty(k) && !a.hasOwnProperty(k)) {
return false
}
}
return true
}
export function combine(...restReducers) {
const reducers = assign.apply(null, [{}].concat(restReducers))
return function combinedReducer(state, payload) {
const newState = reducers.hasOwnProperty(payload.action)
? reducers[payload.action](state, payload.data)
: state
if (shallowEqual(state, newState)) this.preventDefault()
return newState
}
}
export function reduceWith(actions, reduce) {
return actions.reduce((total, action) => {
total[getId(action)] = reduce
return total
}, {})
}