-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cc85583
Showing
12 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# root | ||
root: true | ||
|
||
# code env | ||
env: | ||
node: true | ||
mocha: true | ||
es6: true | ||
|
||
# rules | ||
rules: | ||
# basic | ||
semi: [error, never] | ||
quotes: [error, single, { allowTemplateLiterals: true }] | ||
eqeqeq: error | ||
camelcase: error | ||
radix: error | ||
yoda: error | ||
no-var: error | ||
no-const-assign: error | ||
strict: [error, global] | ||
comma-dangle: [error, only-multiline] | ||
|
||
# indent | ||
indent: [error, 2, { SwitchCase: 1 }] | ||
|
||
# brace | ||
curly: [error, multi-line] | ||
brace-style: error | ||
|
||
# disable default | ||
no-unused-vars: off | ||
no-console: off | ||
no-unreachable: off | ||
require-yield: off | ||
no-constant-condition: off | ||
|
||
parserOptions: | ||
ecmaVersion: 6 | ||
sourceType: module | ||
|
||
extends: | ||
- 'eslint:recommended' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
logs | ||
*.log | ||
.DS_Store | ||
|
||
npm-debug.log* | ||
node_modules | ||
|
||
.nyc_output | ||
coverage | ||
coverage.* | ||
|
||
# build file | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- 7 | ||
script: "npm run test-cover" | ||
after_script: "npm run report-cover" | ||
notifications: | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# CHANGELOG | ||
|
||
## v0.0.1 2017-08-20 | ||
- first release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Magicdawn([email protected]) | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# redux-standard-reducer | ||
> A reducer that handles standard action | ||
[](https://travis-ci.org/magicdawn/reduce-reducers) | ||
[](https://codecov.io/gh/magicdawn/reduce-reducers) | ||
[](https://www.npmjs.com/package/redux-standard-reducer) | ||
[](https://www.npmjs.com/package/redux-standard-reducer) | ||
[](http://magicdawn.mit-license.org) | ||
|
||
## Install | ||
```sh | ||
$ npm i redux-standard-reducer --save | ||
``` | ||
|
||
## API | ||
```js | ||
const reduxStandardReducer = require('redux-standard-reducer'); | ||
``` | ||
|
||
## Changelog | ||
[CHANGELOG.md](CHANGELOG.md) | ||
|
||
## License | ||
the MIT License http://magicdawn.mit-license.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "redux-standard-reducer", | ||
"version": "0.0.1", | ||
"description": "A reducer that handles standard action", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"lodash.mergewith": "^4.6.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-polyfill": "^6.26.0", | ||
"babel-preset-env": "^1.6.0", | ||
"babel-register": "^6.26.0", | ||
"codecov": "^2.2.0", | ||
"eslint": "^4.5.0", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"mocha": "^2.5.3", | ||
"reduce-reducers": "^0.1.2", | ||
"redux": "^3.7.2", | ||
"should": "^11.2.1" | ||
}, | ||
"scripts": { | ||
"build": "babel src -d lib", | ||
"test": "mocha", | ||
"test-cover": "istanbul cover _mocha -- -u exports", | ||
"report-cover": "codecov" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/magicdawn/redux-standard-reducer.git" | ||
}, | ||
"keywords": [ | ||
"redux", | ||
"reducer", | ||
"standard" | ||
], | ||
"author": "magicdawn", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/magicdawn/redux-standard-reducer/issues" | ||
}, | ||
"homepage": "https://github.com/magicdawn/redux-standard-reducer#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import _mergeWith from 'lodash.mergewith' | ||
|
||
// lodash, mergeWith | ||
export const customMerger = (cur, newValue) => { | ||
if (Array.isArray(newValue)) { | ||
return newValue | ||
} | ||
} | ||
|
||
export function mergeState(...args) { | ||
return _mergeWith({}, ...args, customMerger) | ||
} | ||
|
||
/** | ||
* handle | ||
* { | ||
* type: STANDARD_MERGE_STATE_XXX | ||
* payload: { to be merged } | ||
* } | ||
* | ||
* or | ||
* { | ||
* type: XXX, | ||
* standard: true, | ||
* payload: {xxx} | ||
* } | ||
*/ | ||
|
||
export default function standardReducer(state, action) { | ||
if (!action || !action.type || !action.payload) 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parserOptions: | ||
ecmaVersion: 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-R spec | ||
--require should | ||
--require babel-polyfill | ||
--compilers js:babel-register |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import standardReducer from '../src/index' | ||
import { combineReducers } from 'redux' | ||
import reduceReducers from 'reduce-reducers' | ||
|
||
describe('Simple', function() { | ||
it('It works', function() { | ||
const reducer = reduceReducers( | ||
standardReducer, | ||
combineReducers({ | ||
partial1(state = { value: 1, foo: 'bar-1' }, action) { | ||
return state | ||
}, | ||
|
||
partial2(state = { value: 2, foo: 'bar-2' }, action) { | ||
return state | ||
}, | ||
}) | ||
) | ||
|
||
let state = {} | ||
|
||
// init | ||
state = reducer(state, { type: 'init' }) | ||
state.partial1.value.should.equal(1) | ||
state.partial2.value.should.equal(2) | ||
|
||
// using type = STANDARD_MERGE_STATE_ | ||
state = reducer(state, { | ||
type: 'STANDARD_MERGE_STATE_MODIFY_PARTIAL1', | ||
payload: { | ||
partial1: { | ||
value: 'modifyed-1', | ||
}, | ||
}, | ||
}) | ||
// console.log(state) | ||
state.partial1.value.should.equal('modifyed-1') | ||
state.partial1.foo.should.equal('bar-1') // unchanged | ||
|
||
// using action.standard | ||
state = reducer(state, { | ||
type: 'MODIFY_PARTIAL_2', | ||
standard: true, | ||
payload: { | ||
partial2: { | ||
value: 'modifyed-2', | ||
}, | ||
}, | ||
}) | ||
state.partial2.value.should.equal('modifyed-2') | ||
state.partial2.foo.should.equal('bar-2') // unchanged | ||
}) | ||
}) |