Skip to content

Commit

Permalink
1.1.0: allow passing a filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
omnidan committed Oct 18, 2015
1 parent af1e9f4 commit 5d4f989
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-ignore",
"version": "1.0.1",
"version": "1.1.0",
"description": "higher-order reducer to ignore redux actions",
"main": "lib/index.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import isFunction from 'lodash/lang/isFunction';
import isFunction from 'lodash/lang/isFunction'

// redux-ignore higher order reducer
export default function ignoreActions (reducer, actions = []) {
let ignorePredicate = isFunction(actions)
? actions
: (action) => actions.indexOf(action.type) >= 0;
: (action) => actions.indexOf(action.type) >= 0

return (state, action) => {
if (!ignorePredicate(action)) {
return reducer(state, action);
return reducer(state, action)
}

return state;
return state
}
}
// /redux-ignore
47 changes: 23 additions & 24 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
import assert from 'assert';
import should from 'should';
import ignoreActions from '../src/index';
import assert from 'assert'
import ignoreActions from '../src/index'

describe('ignoreActions()', () => {
let reducer = (state, action) => {
switch (action.type) {
case 'FOO':
return 'foo-state';
return 'foo-state'
case 'BAR':
return 'bar-state';
return 'bar-state'
default:
return 'default-state';
return 'default-state'
}
};
}

it('should ignore actions with specified types in array', () => {
let ignoringReducer = ignoreActions(reducer, ['BAR']);
let action = { type: 'BAR' };
let ignoringReducer = ignoreActions(reducer, ['BAR'])
let action = { type: 'BAR' }

assert.equal(
ignoringReducer('testing', action),
'testing');
});
'testing')
})

it('should not ignore actions that do not have types in array', () => {
let ignoringReducer = ignoreActions(reducer, ['BAR']);
let action = { type: 'FOO' };
let ignoringReducer = ignoreActions(reducer, ['BAR'])
let action = { type: 'FOO' }

assert.equal(
ignoringReducer('testing', action),
'foo-state');
});
'foo-state')
})

it('should allow all actions when no action types array is specified', () => {
let ignoringReducer = ignoreActions(reducer);
let action = { type: 'BAZ' };
let ignoringReducer = ignoreActions(reducer)
let action = { type: 'BAZ' }

assert.equal(
ignoringReducer('testing', action),
'default-state');
});
'default-state')
})

it('should work with a predicate function for actions', () => {
let ignoringReducer = ignoreActions(reducer, (a) => a.invalid);
let action = { type: 'BAR', invalid: true };
let ignoringReducer = ignoreActions(reducer, (a) => a.invalid)
let action = { type: 'BAR', invalid: true }

assert.equal(
ignoringReducer('testing', action),
'testing');
});
});
'testing')
})
})

0 comments on commit 5d4f989

Please sign in to comment.