Skip to content

Commit

Permalink
add immutability-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Sep 6, 2017
1 parent 8e4017a commit 0d76544
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A redux reducer for standard action that merge data to state",
"main": "lib/index.js",
"dependencies": {
"immutability-helper": "^2.3.1",
"lodash.mergewith": "^4.6.0"
},
"devDependencies": {
Expand Down
22 changes: 20 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import assert from 'assert'
import _mergeWith from 'lodash.mergewith'
import update from 'immutability-helper'

// export `update`
export { update }

// lodash, mergeWith
export const customMerger = (cur, newValue) => {
Expand Down Expand Up @@ -38,12 +43,25 @@ export function mergeState(...args) {
*/

export default function standardReducer(state, action) {
if (!action || !action.type || !action.payload) return state
if (!action || !action.type) return state

// STANDARD_MERGE_STATE
const ok =
action.type.startsWith('STANDARD_MERGE_STATE') || Boolean(action.standard)
if (!ok) return state

return mergeState(state, action.payload)
// use immutability-helper `update`
if (action.update) {
if (typeof action.update !== 'object') {
throw new Error('expect standard action.update = object')
}
state = update(state, action.update)
}

// merge
if (action.payload) {
state = mergeState(state, action.payload)
}

return state
}
2 changes: 1 addition & 1 deletion test/mergeState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mergeState } from '../src/index'
import assert from 'assert'
import { mergeState } from '../src/index'

describe('mergeState', function() {
it('nothing to merge', function() {
Expand Down
70 changes: 70 additions & 0 deletions test/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import assert from 'assert'
import reduceReducers from 'reduce-reducers'
import { combineReducers } from 'redux'
import standardReducer from '../src/index'

/* eslint no-extra-semi: off */

describe('immutability-helper', function() {
let reducer
let state

beforeEach(() => {
reducer = reduceReducers(
standardReducer,
(state = { arr: [] }, action) => state,
combineReducers({
partial1(state = { value: 1, foo: 'bar-1' }, action) {
return state
},

partial2(state = { value: 2, foo: 'bar-2' }, action) {
return state
},
})
)

// init
state = {}
state = reducer(state, { type: 'init' })
state.partial1.value.should.equal(1)
state.partial2.value.should.equal(2)
})

it('it works', function() {
state = reducer(state, {
type: 'UPDATE_PARTIAL_1',
standard: true,
update: {
partial1: {
foo: { $set: 'updated-foo-1' },
},
},
})
state.partial1.foo.should.equal('updated-foo-1')

// init `partial1.sub` to object
state = reducer(state, {
type: 'UPDATE_PARTIAL_1',
standard: true,
update: {
partial1: {
sub: {
$apply: sub => sub || {},
},
},
},
})
assert(state.partial1.sub)
})

it('throws when `action.update` not object', function() {
;(() => {
state = reducer(state, {
type: 'SOME_ACTION_TYPE',
standard: true,
update: 1,
})
}).should.throw(/expect standard action.update = object/)
})
})

0 comments on commit 0d76544

Please sign in to comment.