From cc855832c30a1622c6d5295b4a3c31cf67670188 Mon Sep 17 00:00:00 2001 From: magicdawn Date: Sun, 20 Aug 2017 21:56:21 +0800 Subject: [PATCH] init --- .babelrc | 3 +++ .eslintrc.yml | 43 +++++++++++++++++++++++++++++++++++++ .gitignore | 13 ++++++++++++ .travis.yml | 7 ++++++ CHANGELOG.md | 4 ++++ LICENSE | 21 ++++++++++++++++++ README.md | 24 +++++++++++++++++++++ package.json | 43 +++++++++++++++++++++++++++++++++++++ src/index.js | 38 +++++++++++++++++++++++++++++++++ test/.eslintrc.yml | 2 ++ test/mocha.opts | 4 ++++ test/simple.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 255 insertions(+) create mode 100644 .babelrc create mode 100644 .eslintrc.yml create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 package.json create mode 100644 src/index.js create mode 100644 test/.eslintrc.yml create mode 100644 test/mocha.opts create mode 100644 test/simple.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..002b4aa --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["env"] +} diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..044bc7e --- /dev/null +++ b/.eslintrc.yml @@ -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' \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b27c51 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +logs +*.log +.DS_Store + +npm-debug.log* +node_modules + +.nyc_output +coverage +coverage.* + +# build file +lib \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7f33dc5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - 7 +script: "npm run test-cover" +after_script: "npm run report-cover" +notifications: + email: false \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..0da2285 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +# CHANGELOG + +## v0.0.1 2017-08-20 +- first release \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2177f1c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Magicdawn(magicdawn@qq.com) + +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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..be91b7b --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..9614249 --- /dev/null +++ b/package.json @@ -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://git@github.com/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" +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..12aee53 --- /dev/null +++ b/src/index.js @@ -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) +} diff --git a/test/.eslintrc.yml b/test/.eslintrc.yml new file mode 100644 index 0000000..4f4212f --- /dev/null +++ b/test/.eslintrc.yml @@ -0,0 +1,2 @@ +parserOptions: + ecmaVersion: 8 \ No newline at end of file diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..14022d9 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,4 @@ +-R spec +--require should +--require babel-polyfill +--compilers js:babel-register \ No newline at end of file diff --git a/test/simple.js b/test/simple.js new file mode 100644 index 0000000..85e7e1f --- /dev/null +++ b/test/simple.js @@ -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 + }) +})