Skip to content

Commit

Permalink
feat(keyedCollection.js): add clear action
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Mar 6, 2017
1 parent d3fc79a commit a3f493d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ store.dispatch(userActions.batch([
]))
```

There is a `clear` action as well that will clear the collection.

13 changes: 10 additions & 3 deletions src/keyedCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import {fromJS, Map} from 'immutable'
import defaults from 'lodash.defaults'
import createReducer from './createReducer'

export const CLEAR = 'CLEAR'
export const INSERT = 'INSERT'
export const UPDATE = 'UPDATE'
export const REMOVE = 'REMOVE'
export const BATCH = 'BATCH'

export function actions(actionTypePrefix = '') {
return {
clear() {
return {
type: actionTypePrefix + CLEAR,
}
},
insert(key, fields) {
return {
type: actionTypePrefix + INSERT,
Expand Down Expand Up @@ -41,9 +47,9 @@ export function actions(actionTypePrefix = '') {

// this looks confusing...it just maintains backwards compatibility with old actions export
// by assigning unprefixed actions to the actions function
const {insert, update, remove, batch} = actions()
export {insert, update, remove, batch}
Object.assign(actions, {insert, update, remove, batch})
const {clear, insert, update, remove, batch} = actions()
export {clear, insert, update, remove, batch}
Object.assign(actions, {clear, insert, update, remove, batch})

const defaultOptions = {
actionTypePrefix: '',
Expand All @@ -60,6 +66,7 @@ const defaultOptions = {
export function reducer(options) {
const {createReducer, createDocument, merge, initialState, enhance, actionTypePrefix} = defaults({}, options, defaultOptions)
let reducer = createReducer(initialState, {
[actionTypePrefix + CLEAR]: collection => collection.clear(),
[actionTypePrefix + INSERT]: (collection, {payload, meta: {key}}) => collection.update(key, doc => doc || createDocument(payload)),
[actionTypePrefix + UPDATE]: (collection, {payload, meta: {key}}) => collection.update(key, doc => merge(doc, payload)),
[actionTypePrefix + REMOVE]: (collection, {meta: {key}}) => collection.delete(key),
Expand Down
7 changes: 6 additions & 1 deletion test/keyedCollectionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ chai.use(chaiImmutable)

describe('keyedCollectionReducer', () => {
function tests(actions, reducer) {
const {insert, update, remove, batch} = actions
const {clear, insert, update, remove, batch} = actions
describe('clear', () => {
it('clears collection', () => {
expect(reducer(Map({hello: 'world'}), clear())).to.equal(fromJS({}))
})
})
describe('insert', () => {
it('inserts if no document exists', () => {
expect(reducer(undefined, insert('a', {hello: 'world'}))).to.equal(fromJS({a: {hello: 'world'}}))
Expand Down

0 comments on commit a3f493d

Please sign in to comment.