Skip to content

Commit

Permalink
🔨 remove state.nb, rename getNb to getLength
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien JUIF committed Jun 26, 2017
1 parent 8cee5ee commit 82b17c6
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ Selectors are:
| `getBy(<propertyPath>, <value>)(state)` | get data specified by the field you want to filter with (take care, selectors are not memoized) | Example: `getBy('visible', true)(state)` returns all visible todos.
| `getKeys(state)` | returns all store keys (in array) | |
| `getAsArray(state)` | returns all data in array (raw) | |
| `getNb(state)` | returns number of stored instances | |
| `getLength(state)` | returns number of stored instances | |
| `isInitialized(state)` | return true if the store has been initialized (by `add` or by `set` action) | |
| `getState(state)` | returns the global state of your reducer | The global state contains :<ul><li>`data`: key/value store</li><li>`array`: raw data</li><li>`keys`: keys array</li><li>`nb`: store length</li><li>`initialized`: boolean (set to true by `set` and `add` actions)</li></ul>
| `getState(state)` | returns the global state of your reducer | The global state contains :<ul><li>`data`: key/value store</li><li>`array`: raw data</li><li>`keys`: keys array</li><li>`initialized`: boolean (set to true by `set` and `add` actions)</li></ul>

Example, we retrieve the todo with id `1`:
```es6
Expand Down
2 changes: 0 additions & 2 deletions src/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Object {
1,
2,
],
"nb": 2,
}
`;

Expand Down Expand Up @@ -77,7 +76,6 @@ Object {
1,
2,
],
"nb": 2,
}
`;

Expand Down
6 changes: 0 additions & 6 deletions src/__snapshots__/reducer.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Object {
"elm3",
"elm4",
],
"nb": 4,
}
`;

Expand All @@ -77,7 +76,6 @@ Object {
"keys": Array [
"elm4",
],
"nb": 1,
}
`;

Expand Down Expand Up @@ -112,7 +110,6 @@ Object {
"elm2",
"elm3",
],
"nb": 2,
}
`;

Expand All @@ -122,7 +119,6 @@ Object {
"data": Object {},
"initialized": false,
"keys": Array [],
"nb": 0,
}
`;

Expand All @@ -132,7 +128,6 @@ Object {
"data": Object {},
"initialized": false,
"keys": Array [],
"nb": 0,
}
`;

Expand Down Expand Up @@ -167,6 +162,5 @@ Object {
"elm1",
"elm2",
],
"nb": 2,
}
`;
4 changes: 1 addition & 3 deletions src/__snapshots__/selectors.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Array [
]
`;

exports[`selectors should retrieve nb 1`] = `5`;
exports[`selectors should retrieve length 1`] = `5`;

exports[`selectors should retrieve state 1`] = `
Object {
Expand Down Expand Up @@ -251,7 +251,6 @@ Object {
"elm4",
"elm5",
],
"nb": 5,
}
`;

Expand Down Expand Up @@ -331,6 +330,5 @@ Object {
"elm4",
"elm5",
],
"nb": 5,
}
`;
1 change: 0 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const subState = {
2: Todo(2),
},
keys: [1, 20, 2],
nb: 3,
initialized: true,
}

Expand Down
5 changes: 1 addition & 4 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { keyBy, without, uniq, omit } from 'lodash'
import { SET, ADD, DEL, RESET } from './actions'

export const initState = { data: {}, keys: [], array: [], nb: 0, initialized: false }
export const initState = { data: {}, keys: [], array: [], initialized: false }

export default key => prefix =>
(state = initState, { type = 'UNKONWN', payload } = {}) => {
Expand All @@ -11,7 +11,6 @@ export default key => prefix =>
data: keyBy(payload, key),
keys: payload.map(element => element[key]),
array: payload,
nb: payload.length,
initialized: true,
}
case ADD(prefix):
Expand All @@ -20,7 +19,6 @@ export default key => prefix =>
data: { ...state.data, [payload[key]]: payload },
keys: uniq([...state.keys, payload[key]]),
array: [...state.array, payload],
nb: state.keys.length + 1,
initialized: true,
}
case DEL(prefix):
Expand All @@ -29,7 +27,6 @@ export default key => prefix =>
data: omit(state.data, [payload]),
keys: without(state.keys, payload),
array: state.array ? state.array.filter(o => o[key] !== payload) : [],
nb: state.keys.length - 1,
}
case RESET(prefix):
return initState
Expand Down
1 change: 0 additions & 1 deletion src/reducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const state = {
},
keys: ['elm2', 'elm1', 'elm3'],
array: [Element('elm2'), Element('elm1'), Element('elm3')],
nb: 3,
initialized: false,
}

Expand Down
2 changes: 1 addition & 1 deletion src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getState = path => prefix => (state) => {
const getFactory = key => path => prefix => state => getState(path)(prefix)(state)[key]
export const getKeys = getFactory('keys')
export const getAsArray = getFactory('array')
export const getNb = getFactory('nb')
export const getLength = path => prefix => state => getKeys(path)(prefix)(state).length
export const isInitialized = getFactory('initialized')

const getData = getFactory('data')
Expand Down
5 changes: 2 additions & 3 deletions src/selectors.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env jest */
import { getKeys, getNb, getState, get, getBy, getAsArray, isInitialized } from './selectors'
import { getKeys, getLength, getState, get, getBy, getAsArray, isInitialized } from './selectors'

const Element = code => ({ code, some: 'other', infos: code })
const SubElement = code => subCode => ({ ...Element(code), sub: { subCode } })
Expand All @@ -17,7 +17,6 @@ const subState = {
},
keys: ['elm2', 'elm1', 'elm3', 'elm4', 'elm5'],
array: [Element('elm2'), Element('elm1'), Element('elm3'), SubElement('elm4')('subelm4'), SubElement('elm5')('subelm5')],
nb: 5,
initialized: true,
},
}
Expand All @@ -33,7 +32,7 @@ describe('selectors', () => {
it('should retrieve state', () => expect(getState(path)(prefix)(state)).toMatchSnapshot())
it('should retrieve state, without path set', () => expect(getState()(prefix)(subState)).toMatchSnapshot())
it('should retrieve keys', () => expect(getKeys(path)(prefix)(state)).toMatchSnapshot())
it('should retrieve nb', () => expect(getNb(path)(prefix)(state)).toMatchSnapshot())
it('should retrieve length', () => expect(getLength(path)(prefix)(state)).toMatchSnapshot())
it('should retrieve initialized value', () => expect(isInitialized(path)(prefix)(state)).toMatchSnapshot())
it('should retrieve all data, without key', () => expect(get(path)(prefix)()(state)).toMatchSnapshot())
it('should retrieve data by ids, with an array of keys', () => expect(get(path)(prefix)(['elm1', 'elm3'])(state)).toMatchSnapshot())
Expand Down

0 comments on commit 82b17c6

Please sign in to comment.