entities-reducer
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -7,2 +7,8 @@ Object.defineProperty(exports, "__esModule", { | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _fluxStandardAction = require('flux-standard-action'); | ||
var _redux = require('redux'); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
@@ -18,24 +24,34 @@ | ||
var defaultDataResolver = function defaultDataResolver(action) { | ||
if ((0, _fluxStandardAction.isFSA)(action)) { | ||
var payload = action.payload; | ||
if (payload && !payload.error && payload.entities) { | ||
return payload.entities; | ||
} | ||
return null; | ||
} | ||
return action.entities; | ||
}; | ||
exports.default = function (reducers) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { dataResolver: defaultDataResolver }, | ||
dataResolver = _ref.dataResolver; | ||
return function () { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var action = arguments[1]; | ||
var payload = action.payload; | ||
var newState = state; | ||
if (payload && !payload.error && payload.entities) { | ||
(function () { | ||
var entities = payload.entities; | ||
newState = Object.keys(entities).reduce(function (acc, key) { | ||
return updateEntities(acc, key, entities); | ||
}, newState); | ||
})(); | ||
var entities = dataResolver(action); | ||
if (entities && (typeof entities === 'undefined' ? 'undefined' : _typeof(entities)) === 'object') { | ||
newState = Object.keys(entities).reduce(function (acc, key) { | ||
return updateEntities(acc, key, entities); | ||
}, newState); | ||
} | ||
newState = Object.keys(reducers).reduce(function (acc, key) { | ||
var reducerReturn = reducers[key](acc[key], action); | ||
return Object.assign({}, acc, _defineProperty({}, key, reducerReturn)); | ||
}, newState); | ||
if (reducers && (typeof reducers === 'undefined' ? 'undefined' : _typeof(reducers)) === 'object' && Object.keys(reducers).length > 0) { | ||
return (0, _redux.combineReducers)(reducers)(newState, action); | ||
} | ||
return newState; | ||
}; | ||
}; |
@@ -11,3 +11,3 @@ { | ||
}, | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Redux normalized entity reducer", | ||
@@ -49,3 +49,10 @@ "main": "dist/index.js", | ||
} | ||
}, | ||
"peerDependencies": { | ||
"redux": "^3.3.0" | ||
}, | ||
"dependencies": { | ||
"flux-standard-action": "^1.1.0", | ||
"redux": "3.0.3" | ||
} | ||
} |
@@ -1,5 +0,3 @@ | ||
# Entities reducer | ||
# Entities Reducer | ||
Redux high order reducer for normalized `flux-standard-action`s | ||
[![Build Status][build-badge]][build] | ||
@@ -13,7 +11,23 @@ [![Code Coverage][coverage-badge]][coverage] | ||
This package is a high-order reducer that updates state using entities from [normalizr](https://github.com/paularmstrong/normalizr). | ||
By default, it expects the action to have a property of `entities` or to follow the [`flux-standard-action` spec](https://github.com/acdlite/flux-standard-action). | ||
However, it is possible to pass a data resolver ([see Parameters](#parameter-data-resolver)) if your case doesn't match either of those. | ||
Allows for updates to entities in state. Accepts custom reducers to further control. | ||
## Why | ||
**Why does this package exist?** | ||
## Usage | ||
One of the best things to store in redux is data from server requests. Additionally, working with items stored in redux is best done when the data is normalized. | ||
To simplify the storing of the data, this package will handle updating state with fresh normalized data. | ||
Entities Reducer is a high-order reducer, which means it will accept more reducers for further customizations. | ||
The custom reducers are passed directly to `combineRecuders` from redux and should be passed into `entities-reducers` with the same format. | ||
## Installation | ||
``` | ||
npm install --save entities-reducer | ||
--- or --- | ||
yarn add entities-reducer | ||
``` | ||
Then add to your root reducer: | ||
```javascript | ||
@@ -32,3 +46,26 @@ import { combineReducers } from 'redux'; | ||
## Parameters | ||
```javascript | ||
entitiesReducer(reducers, { dataResolver }) | ||
``` | ||
### (#parameters-reducers) Reducers | ||
Reducers are passed directly into `combineReducers` from redux, after the entities have been updated in state. It is called with the updated state and immediately returned. | ||
### (#parameters-data-resolver) dataResolver | ||
The data resolver is a lookup function that is passed the action and returns the entities object to use while updating. | ||
If the data resolver returns a falsy value the `entities-reducer` will skip process and move directly to handling the custom reducers. | ||
Below is a customer dataResolver example, or you can checkout the [default resolver](src/index.js). | ||
**Example** | ||
```javascript | ||
const customDataResolver = (action) => { | ||
if (action.error) { | ||
return false; | ||
} | ||
return action.data.extra.normalized; | ||
} | ||
``` | ||
[build]: https://travis-ci.org/kwelch/entities-reducer | ||
@@ -35,0 +72,0 @@ [build-badge]: https://img.shields.io/travis/kwelch/entities-reducer.svg?style=flat-square |
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
7944
42
79
3
+ Addedflux-standard-action@^1.1.0
+ Addedredux@3.0.3
+ Addedflux-standard-action@1.2.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedredux@3.0.3(transitive)