-
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.
New vesion 1.0.0, updated dependencies, added tests
- Loading branch information
Anton Petrov
committed
Sep 7, 2016
1 parent
76e5de0
commit dc9673c
Showing
7 changed files
with
174 additions
and
48 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"stage": 0, | ||
"loose": "all" | ||
} | ||
"presets": ["es2015", "stage-1"] | ||
} |
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 |
---|---|---|
@@ -1,17 +1,4 @@ | ||
{ | ||
"extends": "eslint-config-airbnb", | ||
"env": { | ||
"mocha": true, | ||
"node": true | ||
}, | ||
"globals": { | ||
"expect": true | ||
}, | ||
"rules": { | ||
"padded-blocks": 0, | ||
"no-use-before-define": [2, "nofunc"], | ||
"no-unused-expressions": 0, | ||
"indent": [2, 4], | ||
"comma-dangle": 0 | ||
} | ||
} | ||
"parser": "babel-eslint", | ||
"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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "4" | ||
script: | ||
- npm run eslint | ||
- npm run test |
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
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
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 |
---|---|---|
@@ -1,28 +1,32 @@ | ||
import isFunction from 'lodash/lang/isFunction'; | ||
import mapValues from 'lodash/object/mapValues'; | ||
import isFunction from 'lodash/isFunction' | ||
import mapValues from 'lodash/mapValues' | ||
import get from 'lodash/get' | ||
|
||
function selectEntities(action, name) { | ||
if (action.payload && action.payload.entities && action.payload.entities[name]) { | ||
return action.payload.entities[name]; | ||
} | ||
const entities = get(action, `payload.entities.${name}`) | ||
if (entities) { | ||
return entities; | ||
} | ||
} | ||
|
||
export function entitiesReducer(reducer, entitiesName) { | ||
return (state, action) => { | ||
let newState = state; | ||
const entities = isFunction(entitiesName) ? entitiesName(action) : selectEntities(action, entitiesName); | ||
return (state, action) => { | ||
let newState = state; | ||
const entities = isFunction(entitiesName) ? | ||
entitiesName(action) : selectEntities(action, entitiesName); | ||
|
||
if (entities) { | ||
newState = Object.assign({}, newState, entities); | ||
} | ||
if (entities) { | ||
newState = Object.assign({}, newState, entities); | ||
} | ||
|
||
return reducer(newState, action); | ||
}; | ||
return reducer(newState, action); | ||
}; | ||
} | ||
|
||
export function combineEntitiesReducers(reducers) { | ||
const entitiesReducers = mapValues(reducers, entitiesReducer); | ||
return (state = {}, action) => mapValues(entitiesReducers, | ||
(reducer, key) => reducer(state[key], action) | ||
); | ||
const entitiesReducers = mapValues(reducers, entitiesReducer); | ||
return (state = {}, action) => mapValues( | ||
entitiesReducers, | ||
(reducer, key) => reducer(state[key], action) | ||
); | ||
} |
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,128 @@ | ||
/* eslint-env mocha */ | ||
import { expect } from 'chai'; | ||
import { entitiesReducer, combineEntitiesReducers } from '../src'; | ||
|
||
describe('redux-entities', () => { | ||
const normalizedObject = { | ||
entities: { | ||
contacts: { | ||
1: { | ||
id: 1, | ||
name: 'Anton' | ||
}, | ||
2: { | ||
id: 2, | ||
name: 'Sergey' | ||
} | ||
}, | ||
notes: { | ||
10: { | ||
id: 10, | ||
title: 'Run tests' | ||
}, | ||
20: { | ||
id: 20, | ||
title: 'Run lint' | ||
} | ||
} | ||
} | ||
} | ||
|
||
const fillEntitiesAction = { | ||
type: 'FILL_ENTITIES', | ||
payload: normalizedObject | ||
} | ||
|
||
const fillEntitiesNestedAction = { | ||
type: 'FILL_ENTITIES', | ||
payload: { | ||
nested: normalizedObject | ||
} | ||
} | ||
|
||
const updateContactAction = { | ||
type: 'UPDATE_CONTACT', | ||
payload: { | ||
id: 1, | ||
name: 'Andrey' | ||
} | ||
} | ||
|
||
const contactsReducer = (state = {}) => state; | ||
const notesReducer = (state = {}) => state; | ||
const contactsWithUpdateReducer = (state = {}, action) => { | ||
const { type, payload } = action; | ||
if (type === 'UPDATE_CONTACT') { | ||
const { id, ...rest } = payload | ||
return { | ||
...state, | ||
[id]: { | ||
...state[id], | ||
...rest | ||
} | ||
} | ||
} | ||
return state | ||
} | ||
|
||
describe('entitiesReducer', () => { | ||
it('should return named entities object', () => { | ||
const reducer = entitiesReducer( | ||
contactsReducer, | ||
'contacts' | ||
) | ||
|
||
const state = reducer({}, fillEntitiesAction) | ||
|
||
expect(state).to.deep.equal(normalizedObject.entities.contacts) | ||
}) | ||
|
||
it('can extract entities by custom path', () => { | ||
const reducer = entitiesReducer( | ||
contactsReducer, | ||
(action) => action.payload.nested.entities.contacts | ||
) | ||
|
||
const state = reducer({}, fillEntitiesNestedAction) | ||
|
||
expect(state).to.deep.equal(normalizedObject.entities.contacts) | ||
}) | ||
|
||
it('can update specific entitie', () => { | ||
const reducer = entitiesReducer( | ||
contactsWithUpdateReducer, | ||
'contacts' | ||
) | ||
|
||
const state = reducer({}, fillEntitiesAction) | ||
const updatedState = reducer(state, updateContactAction) | ||
|
||
expect(updatedState[1]).to.deep.equal({ id: 1, name: 'Andrey' }) | ||
}) | ||
}) | ||
|
||
describe('combineEntitiesReducers', () => { | ||
it('should return entities object', () => { | ||
const reducer = combineEntitiesReducers({ | ||
contacts: contactsReducer, | ||
notes: notesReducer, | ||
}) | ||
|
||
const state = reducer({}, fillEntitiesAction) | ||
|
||
expect(state).to.deep.equal(normalizedObject.entities) | ||
}) | ||
|
||
it('can update specific entitie', () => { | ||
const reducer = combineEntitiesReducers({ | ||
contacts: contactsWithUpdateReducer, | ||
notes: notesReducer, | ||
}) | ||
|
||
const state = reducer({}, fillEntitiesAction) | ||
const updatedState = reducer(state, updateContactAction) | ||
|
||
expect(updatedState.contacts[1]).to.deep.equal({ id: 1, name: 'Andrey' }) | ||
}) | ||
}) | ||
}) |