Comparing version 0.1.0 to 0.2.0
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). | ||
## [Unreleased] | ||
## [0.1.0] - 2019-07-12 | ||
- Initial release | ||
* The changelog is recorded as part of the project [Releases](https://github.com/tgallacher/redux-msr/releases). |
{ | ||
"name": "redux-msr", | ||
"version": "0.1.0", | ||
"description": "Merge sub reducers wihtout splitting the Redux store", | ||
"main": "dist/index.js", | ||
"version": "0.2.0", | ||
"description": "Merge resource sub reducers wihtout slicing the Redux state", | ||
"main": "dist/bundle.js", | ||
"scripts": { | ||
"build": "parcel build index.js", | ||
"test": "jest", | ||
"build": "webpack --config ./webpack.prod.js", | ||
"build:dev": "webpack --config ./webpack.dev.js", | ||
"test": "jest --config ./.jest.config.js", | ||
"test:dev": "npm run-script test -- --watch", | ||
"flow": "flow check", | ||
"flow:install": "flow-typed install", | ||
"lint": "semistandard --verbose | snazzy", | ||
"lint": "eslint --config .eslintrc src/**/*.js", | ||
"lint:fix": "semistandard --fix", | ||
@@ -21,42 +22,40 @@ "preversion": "npm run lint && npm test", | ||
], | ||
"author": "Tom Gallacher <thomas.gallacher@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/tgallacher/redux-msr.git" | ||
}, | ||
"author": "Tom Gallacher <tgallacher@users.noreply.github.com>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"ajv": "^6.5.2", | ||
"babel-core": "^6.26.3", | ||
"babel-eslint": "^8.2.5", | ||
"babel-jest": "^23.2.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-preset-env": "^1.7.0", | ||
"eslint": "^5.0.1", | ||
"eslint-plugin-flowtype": "^2.49.3", | ||
"flow-bin": "^0.75.0", | ||
"flow-typed": "^2.4.0", | ||
"jest": "^23.3.0", | ||
"jest-extended": "^0.7.2", | ||
"parcel-bundler": "^1.9.5", | ||
"regenerator-runtime": "^0.12.0", | ||
"semistandard": "^12.0.1", | ||
"snazzy": "^7.1.1" | ||
"@babel/core": "^7.1.2", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/polyfill": "^7.0.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"@babel/preset-flow": "^7.0.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-eslint": "^8.2.6", | ||
"babel-jest": "^23.6.0", | ||
"babel-loader": "^8.0.4", | ||
"clean-webpack-plugin": "^0.1.19", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jsx-a11y": "^6.1.1", | ||
"eslint-plugin-promise": "^4.0.0", | ||
"eslint-plugin-react": "^7.11.1", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"flow-bin": "^0.80.0", | ||
"eslint": "^5.8.0", | ||
"eslint-plugin-flowtype": "^2.50.3", | ||
"flow-typed": "^2.5.1", | ||
"jest": "^23.6.0", | ||
"jest-extended": "^0.11.0", | ||
"snazzy": "^7.1.1", | ||
"webpack": "^4.23.1", | ||
"webpack-cli": "^3.1.2", | ||
"webpack-merge": "^4.1.4" | ||
}, | ||
"dependencies": {}, | ||
"files": [ | ||
"dist/" | ||
], | ||
"jest": { | ||
"setupTestFrameworkScriptFile": "jest-extended" | ||
"peerDependencies": { | ||
"redux": "^4.0.0" | ||
}, | ||
"semistandard": { | ||
"parser": "babel-eslint", | ||
"ignore": [ | ||
"flow-typed", | ||
"flowtypes.js" | ||
], | ||
"plugins": [ | ||
"flowtype" | ||
], | ||
"extends": [ | ||
"plugin:flowtype/recommended" | ||
] | ||
} | ||
"dependencies": {} | ||
} |
146
README.md
@@ -1,19 +0,34 @@ | ||
# Redux MSR (Merge Sub Reducers) | ||
# Redux MSR | ||
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/Flet/semistandard) | ||
Merge sub-reducers (MSR) while sharing the same Redux store (or sub-state component), enabling scalable reducer code management and reuse. | ||
Merge sub-reducers while sharing the same Redux store (or slice), enabling scalable reducer code management and reuse. | ||
**Table of Contents** | ||
<!-- TOC depthFrom:2 depthTo:3 --> | ||
- [The Problem](#the-problem) | ||
- [The Solution](#the-solution) | ||
- [Installation](#installation) | ||
- [API](#api) | ||
- [`combineSubReducers<T>(defaultState: T, reducerConfig: ReducerConfig) => Reducer<T>`](#combinesubreducerstdefaultstate-t-reducerconfig-reducerconfig--reducert) | ||
- [`returnPrevState<T>(prevState: T) => T`](#returnprevstatetprevstate-t--t) | ||
- [Examples](#examples) | ||
- [reducerConfig: Object type](#reducerconfig-object-type) | ||
- [reducerConfig: Array type](#reducerconfig-array-type) | ||
<!-- /TOC --> | ||
## The Problem | ||
How best to manage file sizes, reducer layout, code reuse, etc. as reducer complexity and/or size increases. Even when using Redux's [combineReducers()](https://redux.js.org/api-reference/combinereducers) API method to split and organise your (global) store, resource reducers can still grow and become difficult to maintain. This problem can also be further amplified when adopting the [ducks](https://github.com/erikras/ducks-modular-redux) practice. | ||
Redux's API provides [combineReducers()](https://redux.js.org/api-reference/combinereducers) to help manage and split large or complicated store state into smaller chunks that can then be better split/organised within a project. However, even parts of these broken states -- or non-split stores -- handle multiple action types, and/or can include relatively long/complex reducer logic. Code management or readability can quickly become difficult, and we often have to split our reducers into smaller reducers, but then end up with unnecessary code to load or organise such reducers. | ||
This package aims to help provide a simple API to join and reuse reducers, so that code manageement and complexity become easier to manage and scale. | ||
## The Solution | ||
> Assume an app which at some point includes counter-based functionality (detail omitted for brevity) | ||
> Note: The below example is using [ducks](https://github.com/erikras/ducks-modular-redux) for organising action creators, constants and the root resource-reducer. | ||
> Note: The below example is also using the ducks practice for organising action creators, constants and the root resource-reducer. | ||
Taking the following simplified example: | ||
We can take the following extremely simplified example: | ||
```js | ||
@@ -55,3 +70,3 @@ const INCREMENT = 'COUNTER.INCREMENT.VALUE'; | ||
And extract/refactor into something like: | ||
With `redux-msr`, this can be simplified to: | ||
@@ -82,19 +97,5 @@ ```js | ||
## Table of Contents | ||
<!-- TOC --> | ||
- [Redux MSR (Merge Sub Reducers)](#redux-msr-merge-sub-reducers) | ||
- [The Problem](#the-problem) | ||
- [The Solution](#the-solution) | ||
- [Table of Contents](#table-of-contents) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Examples](#examples) | ||
<!-- /TOC --> | ||
## Installation | ||
This module is distributed via [npm]() and should be included as part of your package's dependencies: | ||
This module is distributed via [npm](https://www.npmjs.com) and should be included as part of your package's dependencies: | ||
@@ -105,13 +106,18 @@ ```shell | ||
## Usage | ||
## API | ||
The main method exported by this module has the following type signature: | ||
### `combineSubReducers<T>(defaultState: T, reducerConfig: ReducerConfig) => Reducer<T>` | ||
`combineSubReducers(defaultState: any, reducerConfig: object) => ReducerFn` | ||
**defaultState** | ||
where, | ||
The default state to use when the Redux store is initialised | ||
* `defaultState` - The default state to use when the Redux store is initialised | ||
* `reducerConfig` - An object whose key-value pairs correspond to the action type and corresponding value to return for that action type, respectively. If the return value is a function, then this will be called with the standard Redux reducer parameters, namely `<T>(previousState: T, action: object) => T`. | ||
**reducerConfig** | ||
This can either be an `Array` or an `Object`. | ||
When an `Array`, the array should be an array of Redux reducer functions (type: [Reducer](https://github.com/reduxjs/redux/blob/master/index.d.ts)). Each reducer in the array will be triggered for all `action.type`'s and will be each passed the previous state in sequential order. The result returned by each reducer will be passed to the subsequent in the array, with the final state returned by the array being passed back to the Redux store. | ||
When an `Object`, the object key-value pairs should correspond to the action type and corresponding value to return for that action type, respectively. If the return value is a function, then this will be called with the standard Redux reducer parameters, namely `Reducer` ([see Redux typing](https://github.com/reduxjs/redux/blob/master/index.d.ts)). | ||
The reducer returned by this module will return the previous state unless a specific action type is noted, and a subsequent value or function is supplied in order to change what the new (immutable) state should be. If you need to overwrite the `default` response of the resulting reducer, i.e. if no action-types match the currently dispatched action, then simply add the required value/function to the `default` key within the `reducerConfig` object. For example, | ||
@@ -125,11 +131,19 @@ | ||
### `returnPrevState<T>(prevState: T) => T` | ||
A simply utility that can be used to force a simple *short circuit* and return the previous state. This can be particularly useful when the `reducerConfig` is the `Object` variant, and for a given `action.type` you would like to return the previous state, e.g. during a dispatched error. | ||
## Examples | ||
Return basic primitive types for a given action type: | ||
These are just a small set of examples of what is possible. This package can be used on its own for a single Redux store, or one that has been subdivided into smaller store *slices* using Redux's [combineReducers()](https://redux.js.org/api-reference/combinereducers) API, as mentioned above. | ||
### reducerConfig: Object type | ||
#### 1. Return basic primitive types for a given action type: | ||
```js | ||
// reducer.js | ||
// State is a Number | ||
import combineSubReducers from 'redux-msr'; | ||
// State is a Number | ||
const defaultState = 0; | ||
@@ -145,20 +159,43 @@ | ||
// reducer.js | ||
// State is a String | ||
import combineSubReducers from 'redux-msr'; | ||
// State is a String | ||
const defaultState = ''; | ||
export default combinSubReducers(defaultState, { | ||
'EXAMPLE.ACTION.TYPE.1': 'monday', | ||
'EXAMPLE.ACTION.TYPE.2': 'tuesday', | ||
'EXAMPLE.ACTION.TYPE.1': 'foo', | ||
'EXAMPLE.ACTION.TYPE.2': 'bar', | ||
}); | ||
``` | ||
Use a refactored sub-reducer: | ||
#### 2. Use sub-reducer(s): | ||
```js | ||
// reducer.js | ||
import combineSubReducers from 'redux-msr'; | ||
import handleType1Reducer from './reducers/reducer1'; | ||
import handleType2Reducer from './reducers/reducer2'; | ||
// State is a Number | ||
const defaultState = 0; | ||
export default combinSubReducers(defaultState, { | ||
'EXAMPLE.ACTION.TYPE.1': handleType1Reducer, | ||
'EXAMPLE.ACTION.TYPE.2': handleType2Reducer, | ||
}); | ||
``` | ||
Here, both `handleType1Reducer` and `handleType2Reducer` have the `Reducer` type signature (see [here](https://github.com/reduxjs/redux/blob/master/index.d.ts)). | ||
#### 3. A combination | ||
```js | ||
// reducer.js | ||
import combineSubReducers from 'redux-msr'; | ||
import handleType1Reducer from './reducers/reducer1'; | ||
import handleType2Reducer from './reducers/reducer2'; | ||
// State is a Number | ||
const defaultState = 0; | ||
@@ -169,13 +206,40 @@ | ||
'EXAMPLE.ACTION.TYPE.2': handleType2Reducer, | ||
'EXAMPLE.ACTION.TYPE.3': 10, | ||
}); | ||
``` | ||
Where `handleType1Reducer` and `handleType2Reducer` have the form: | ||
### reducerConfig: Array type | ||
```js | ||
// reducers/handleType1Reducer.js, or | ||
// reducers/handleType2Reducer.js | ||
export default (prevState, action) => { | ||
// single action type reducer code | ||
// reducer.js | ||
import combineSubReducers from 'redux-msr'; | ||
import reducerA from '/some/other/part/of/codebase'; | ||
// Assume our state is a simple number | ||
const defaultState = 0; | ||
// Mocked reducer | ||
const reducerB = (prevState: number = defaultState, action: Action) => { | ||
switch(action.type){ | ||
case 'foo': | ||
return 100; | ||
default: return prevState; | ||
} | ||
}; | ||
/** | ||
* Remember: the order of the reducers in the array matter! | ||
* | ||
* State is passed to the first reducer, here `reducerA`; the state returned | ||
* from reducerA is then passed to `reducerB`; the state returned from reducerB | ||
* is then sent back to the Redux store. | ||
*/ | ||
const mainReducer = comineSubReducers(defaultState, [ | ||
reducerA, | ||
reducerB, | ||
]); | ||
export default mainReducer; | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
25505
9
43
2
240
0
0
1
26