Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Aug 20, 2017
0 parents commit cc85583
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
43 changes: 43 additions & 0 deletions .eslintrc.yml
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'
13 changes: 13 additions & 0 deletions .gitignore
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
7 changes: 7 additions & 0 deletions .travis.yml
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CHANGELOG

## v0.0.1 2017-08-20
- first release
21 changes: 21 additions & 0 deletions LICENSE
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.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# redux-standard-reducer
> A reducer that handles standard action
[![Build Status](https://img.shields.io/travis/magicdawn/reduce-reducers.svg?style=flat-square)](https://travis-ci.org/magicdawn/reduce-reducers)
[![Coverage Status](https://img.shields.io/codecov/c/github/magicdawn/reduce-reducers.svg?style=flat-square)](https://codecov.io/gh/magicdawn/reduce-reducers)
[![npm version](https://img.shields.io/npm/v/redux-standard-reducer.svg?style=flat-square)](https://www.npmjs.com/package/redux-standard-reducer)
[![npm downloads](https://img.shields.io/npm/dm/redux-standard-reducer.svg?style=flat-square)](https://www.npmjs.com/package/redux-standard-reducer)
[![npm license](https://img.shields.io/npm/l/redux-standard-reducer.svg?style=flat-square)](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
43 changes: 43 additions & 0 deletions package.json
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"
}
38 changes: 38 additions & 0 deletions src/index.js
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)
}
2 changes: 2 additions & 0 deletions test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parserOptions:
ecmaVersion: 8
4 changes: 4 additions & 0 deletions test/mocha.opts
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
53 changes: 53 additions & 0 deletions test/simple.js
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
})
})

0 comments on commit cc85583

Please sign in to comment.