Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
omnidan committed Oct 18, 2015
0 parents commit fa31a01
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"stage": 0,
"loose": "all"
}
3 changes: 3 additions & 0 deletions .eslintignore
@@ -0,0 +1,3 @@
lib
**/node_modules
**/webpack.config.js
9 changes: 9 additions & 0 deletions .eslintrc
@@ -0,0 +1,9 @@
{
"extends": "standard",
"parser": "babel-eslint",
"env": {
"browser": true,
"mocha": true,
"node": true
}
}
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
node_modules
*.log
.DS_Store
lib
coverage
5 changes: 5 additions & 0 deletions .npmignore
@@ -0,0 +1,5 @@
.DS_Store
*.log
src
test
coverage
6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "4.0.0"
script:
- npm run lint
- npm test
21 changes: 21 additions & 0 deletions LICENSE.md
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Daniel Bugl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
@@ -0,0 +1,57 @@
# redux-recycle

_higher-order reducer to reset the redux state on certain actions_


## Installation

```
npm install --save redux-recycle
```


## API

```js
recycleState(reducer, [ARRAY_OF_ACTIONS], initialState)
```


## Ignoring actions

`redux-recycle` is a reducer enhancer (higher-order reducer), it provides the
`recycleState` function, which takes an existing reducer and an array of
actions that will reset the state. Optionally, you can also pass an initial
state to reset to. (defaults to calling your reducer with
`@@redux-recycle/INIT` and an `undefined` state, which will have the same effect
as the initial redux action)

Firstly, import `redux-recycle`:

```js
// Redux utility functions
import { combineReducers } from 'redux';
// Redux Undo store enhancer
import recycleState from 'redux-recycle';
```

Then, add `recycleState` to your reducer(s) like this:

```js
combineReducers({
counter: recycleState(counter, [INCREMENT_COUNTER], 0)
})
```

Now, once you click the increment button, the state will be reset to `0`.


## What is this magic? How does it work?

Have a read of the [Implementing Undo History recipe](https://rackt.github.io/redux/docs/recipes/ImplementingUndoHistory.html)
in the Redux documents, which explains in detail how higher-order reducers work.


## License

MIT, see `LICENSE.md` for more information.
49 changes: 49 additions & 0 deletions package.json
@@ -0,0 +1,49 @@
{
"name": "redux-recycle",
"version": "1.0.0",
"description": "higher-order reducer to reset the redux state on certain actions",
"main": "lib/index.js",
"scripts": {
"clean": "rimraf lib",
"build": "babel src --out-dir lib",
"lint": "eslint src test",
"test": "NODE_ENV=test mocha --compilers js:babel/register --recursive",
"test:watch": "NODE_ENV=test mocha --compilers js:babel/register --recursive --watch",
"test:cov": "babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive",
"prepublish": "npm run lint && npm run test && npm run clean && npm run build"
},
"repository": {
"type": "git",
"url": "https://github.com/omnidan/redux-recycle.git"
},
"keywords": [
"redux",
"undo",
"redo",
"flux",
"time travel"
],
"author": "Daniel Bugl <me@omnidan.net> (https://github.com/omnidan)",
"license": "MIT",
"bugs": {
"url": "https://github.com/omnidan/redux-recycle/issues"
},
"homepage": "https://github.com/omnidan/redux-recycle",
"devDependencies": {
"babel": "^5.8.23",
"babel-core": "^5.8.25",
"babel-eslint": "^4.1.3",
"babel-loader": "^5.3.2",
"eslint": "^1.7",
"eslint-config-standard": "^4.4.0",
"eslint-plugin-react": "^3.6.0",
"eslint-plugin-standard": "^1.3.1",
"expect": "^1.12.2",
"isparta": "^3.1.0",
"mocha": "^2.3.3",
"rimraf": "^2.4.3"
},
"dependencies": {
"redux": "^3.0.2"
}
}
13 changes: 13 additions & 0 deletions src/index.js
@@ -0,0 +1,13 @@
// redux-recycle higher order reducer
export default function recycleState (reducer, actions = [], initialState) {
return (state, action) => {
if (actions.indexOf(action.type) >= 0) {
if (initialState === undefined) {
return reducer(undefined, '@@redux-recycle/INIT')
}
return initialState
}
return reducer(state, action)
}
}
// /redux-recycle
Empty file added test/index.spec.js
Empty file.

0 comments on commit fa31a01

Please sign in to comment.