Skip to content

Commit

Permalink
new filter function, distinctState helper, fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
omnidan committed Sep 24, 2015
1 parent ccc559d commit 480f710
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# redux undo/redo

[![NPM version (>=0.3)](https://img.shields.io/npm/v/redux-undo.svg?style=flat-square)](https://www.npmjs.com/package/redux-undo) [![Dependencies](https://img.shields.io/david/omnidan/redux-undo.svg?style=flat-square)](https://david-dm.org/omnidan/redux-undo)
[![NPM version (>=0.4)](https://img.shields.io/npm/v/redux-undo.svg?style=flat-square)](https://www.npmjs.com/package/redux-undo) [![Dependencies](https://img.shields.io/david/omnidan/redux-undo.svg?style=flat-square)](https://david-dm.org/omnidan/redux-undo)

_simple undo/redo functionality for redux state containers_

**Protip:** You can use the [redux-undo-boilerplate](https://github.com/omnidan/redux-undo-boilerplate) to quickly get started with `redux-undo`.

[![https://i.imgur.com/M2KR4uo.gif](https://i.imgur.com/M2KR4uo.gif)](https://github.com/omnidan/redux-undo-boilerplate)

**Note:** When upgrading from *0.3* to *0.4*, use `.present` instead of `.currentState`.


## Installation

Expand All @@ -22,7 +24,7 @@ takes an existing reducer and a configuration object and enhances your existing
reducer with undo functionality.

**Note:** If you were accessing `state.counter` before, you have to access
`state.counter.currentState` after wrapping your reducer with `undoable`.
`state.counter.present` after wrapping your reducer with `undoable`.

To install, firstly import `redux-undo`:

Expand Down Expand Up @@ -100,23 +102,33 @@ If you don't want to include every action in the undo/redo history, you can
pass a function to `undoable` like this:

```js
undoable(reducer, function filterActions(action) {
return action.type !== SOME_ACTION; // only undo/redo on SOME_ACTION
undoable(reducer, function filterActions(action, currentState, previousState) {
return action.type !== SOME_ACTION; // don't add to history if action isn't SOME_ACTION
})

// or you could do...

undoable(reducer, function filterState(action, currentState, previousState) {
return currentState === previousState; // don't add to history if state stayed the same
})
```

Or you can use the `ifAction` and `excludeAction` helpers, which should be
imported like this:
Or you can use the `distinctState`, `ifAction` and `excludeAction` helpers,
which should be imported like this:

```js
import undoable, { ifAction, excludeAction } from 'redux-undo';
import undoable, { distinctState, ifAction, excludeAction } from 'redux-undo';
```

Now you can use the helper, which is pretty simple:

```js
undoable(reducer, { filter: ifAction(SOME_ACTION) })
undoable(reducer, { filter: excludeAction(SOME_ACTION) })

// or you could do...

undoable(reducer, { filter: distinctState })
```

... they even support Arrays:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-undo",
"version": "0.3.0",
"version": "0.4.0",
"description": "simple undo/redo functionality for redux state containers",
"main": "lib/index.js",
"scripts": {
Expand Down
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function updateState(state, history) {
return {
...state,
history,
currentState: history.present,
present: history.present,
};
}
// /updateState
Expand Down Expand Up @@ -153,14 +153,14 @@ export default function undoable(reducer, rawConfig = {}) {
return res ? updateState(state, res) : state;

default:
res = reducer(state && state.currentState, action);
res = reducer(state && state.present, action);

if (config.filter && typeof config.filter === 'function') {
if (!config.filter(action)) {
if (!config.filter(action, res, state && state.present)) {
debug('filter prevented action, not storing it');
return {
...state,
currentState: res,
present: res,
};
}
}
Expand All @@ -171,7 +171,7 @@ export default function undoable(reducer, rawConfig = {}) {

return {
...state,
currentState: res,
present: res,
history: updatedHistory,
};
}
Expand All @@ -185,6 +185,12 @@ export function parseActions(rawActions = []) {
}
// /parseActions

// distinctState helper
export function distinctState() {
return (action, currentState, previousState) => currentState === previousState;
}
// /distinctState

// ifAction helper
export function ifAction(rawActions) {
const actions = parseActions(rawActions);
Expand Down

0 comments on commit 480f710

Please sign in to comment.