summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/lib/createReducer.js
blob: 2ebecfbfb2cac057d7726feef4ab890a71780dce (plain)
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

/**
 * Helper to create a reducer that extend the store.
 *
 * @param  {String} property
 * @param  {Function(state, action): state} reduce
 * @return {Function(state, action): state}
 */
function createReducer(name, reduce) {
    return (state, action) => {
        const value = state[name];
        const newValue = reduce(value, action);

        if (newValue === value) {
            return state;
        }

        const newState = {
            ...state,
            [name]: newValue
        };

        return newState;
    };
}

module.exports = createReducer;