Skip to content

Commit 94887c4

Browse files
authored
1.0.0-alpha.5
Fix broken behavior introduced by array store
2 parents 1417a87 + 41dedfa commit 94887c4

File tree

7 files changed

+54
-40
lines changed

7 files changed

+54
-40
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Redeuce Changelog
22

3+
## 1.0.0-alpha.5 (2019-05-02)
4+
5+
- Fix a major bug introduced in alpha.4 (breaking action creator and reducers arity)
6+
- Full Documentation
7+
38
## 1.0.0-alpha.4 (2019-04-24)
49

510
- New arrayStore

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redeuce",
3-
"version": "1.0.0-alpha.4",
3+
"version": "1.0.0-alpha.5",
44
"main": "dist/redeuce.min.js",
55
"engines": {
66
"node": ">=8.9",

src/generators/actions.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ export const COLLECTION = 'COLLECTION';
33
export const ARRAY = 'ARRAY';
44
export const VERBS = {
55
[SIMPLE]: {
6-
SET: 'set',
6+
SET: { name: 'set' },
77
},
88
[COLLECTION]: {
9-
SET: 'set',
10-
UPDATE: 'update',
11-
DELETE: 'delete',
12-
MERGE: 'merge',
13-
MERGEDEEP: 'mergeDeep',
14-
DELETEALL: 'deleteAll',
15-
CLEAR: 'clear',
9+
SET: { name: 'set' },
10+
UPDATE: { name: 'update' },
11+
DELETE: { name: 'delete' },
12+
MERGE: { name: 'merge' },
13+
MERGEDEEP: { name: 'mergeDeep' },
14+
DELETEALL: { name: 'deleteAll' },
15+
CLEAR: { name: 'clear' },
1616
},
1717
[ARRAY]: {
18-
SET: 'set',
19-
SETIN: 'setIn',
20-
DELETE: 'delete',
21-
INSERT: 'insert',
22-
CLEAR: 'clear',
23-
PUSH: 'push',
24-
POP: 'pop',
25-
UNSHIFT: 'unshift',
26-
SHIFT: 'shift',
18+
SET: { name: 'set', arity: true },
19+
SETIN: { name: 'setIn', arity: true },
20+
DELETE: { name: 'delete' },
21+
INSERT: { name: 'insert', arity: true },
22+
CLEAR: { name: 'clear' },
23+
PUSH: { name: 'push', arity: true },
24+
POP: { name: 'pop', arity: true },
25+
UNSHIFT: { name: 'unshift', arity: true },
26+
SHIFT: { name: 'shift', arity: true },
2727
},
2828
};
2929

@@ -38,14 +38,17 @@ export const generateActionTypes = (storeType, entityId) =>
3838
{},
3939
);
4040

41-
const generateActionCreator = type => (...payload) => ({ type, payload });
41+
const generateActionCreator = (type, arity) => (...payload) =>
42+
arity ? { type, payload } : { type, payload: payload[0] };
43+
4244
export const generateActionCreators = (storeType, entityId) => {
4345
const actionTypes = generateActionTypes(storeType, entityId);
44-
return Object.keys(VERBS[storeType]).reduce(
45-
(actionCreators, verb) => ({
46+
return Object.keys(VERBS[storeType]).reduce((actionCreators, verb) => {
47+
const { name, arity } = VERBS[storeType][verb];
48+
49+
return {
4650
...actionCreators,
47-
[VERBS[storeType][verb]]: generateActionCreator(actionTypes[verb]),
48-
}),
49-
{},
50-
);
51+
[name]: generateActionCreator(actionTypes[verb], arity),
52+
};
53+
}, {});
5154
};

src/generators/reducers.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const buildCollectionReducers = (actionTypes, keyName) => {
2222

2323
// Reducer to SET a list of objects in the store
2424
const setReducer = (state, payload) => {
25-
const filteredPayload = filterByKeyname(asArray(payload[0]), keyName);
25+
const filteredPayload = filterByKeyname(asArray(payload), keyName);
2626
const setIndexes = getKeyNameValues(filteredPayload, keyName);
2727

2828
return [
@@ -35,7 +35,7 @@ export const buildCollectionReducers = (actionTypes, keyName) => {
3535

3636
// Reducer to UPDATE a list of objects in the store
3737
const updateReducer = (state, payload) => {
38-
const filteredPayload = filterByKeyname(asArray(payload[0]), keyName);
38+
const filteredPayload = filterByKeyname(asArray(payload), keyName);
3939
const updateIndexes = getKeyNameValues(filteredPayload, keyName);
4040
const existingIndexes = getKeyNameValues(state, keyName);
4141

@@ -55,7 +55,7 @@ export const buildCollectionReducers = (actionTypes, keyName) => {
5555

5656
// Reducer to DELETE a list of objects in the store
5757
const deleteReducer = (state, payload) => {
58-
const filteredPayload = filterByKeyname(asArray(payload[0]), keyName);
58+
const filteredPayload = filterByKeyname(asArray(payload), keyName);
5959
const setIndexes = getKeyNameValues(filteredPayload, keyName);
6060

6161
return [
@@ -81,7 +81,7 @@ export const buildSimpleReducer = actionTypes => {
8181
const { SET } = actionTypes;
8282

8383
// Reducer to SET a list of objects in the store
84-
const setReducer = (_, payload) => payload[0];
84+
const setReducer = (_, payload) => payload;
8585

8686
return {
8787
[SET]: setReducer,
@@ -94,7 +94,7 @@ export const buildArrayReducers = (actionTypes, keyName) => {
9494
const popReducer = state => state.slice(0, state.length - 1);
9595
const unshiftReducer = (state, payload) => [...payload, ...state];
9696
const shiftReducer = state => state.slice(1, state.length);
97-
const deleteReducer = (state, payload) => state.filter((_, i) => i !== payload[0]);
97+
const deleteReducer = (state, payload) => state.filter((_, i) => i !== payload);
9898
const insertReducer = (state, payload) => {
9999
let a = [...state];
100100
a.splice(payload[1], 0, payload[0]);

tests/arrayStore.test.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('Array Store', () => {
9797
test('delete', () => {
9898
expect(actions.delete(2)).toEqual({
9999
type: `REDEUCE:ARRAY@@${entityName}@@DELETE`,
100-
payload: [2],
100+
payload: 2,
101101
});
102102
});
103103

@@ -111,7 +111,6 @@ describe('Array Store', () => {
111111
test('clear', () => {
112112
expect(actions.clear()).toEqual({
113113
type: `REDEUCE:ARRAY@@${entityName}@@CLEAR`,
114-
payload: [],
115114
});
116115
});
117116

@@ -121,6 +120,14 @@ describe('Array Store', () => {
121120
type: `REDEUCE:ARRAY@@${entityName}@@${verb.toUpperCase()}`,
122121
payload: ['hello'],
123122
});
123+
expect(actions[verb]('hello', 'world')).toEqual({
124+
type: `REDEUCE:ARRAY@@${entityName}@@${verb.toUpperCase()}`,
125+
payload: ['hello', 'world'],
126+
});
127+
expect(actions[verb]('hello', 'cruel', 'world')).toEqual({
128+
type: `REDEUCE:ARRAY@@${entityName}@@${verb.toUpperCase()}`,
129+
payload: ['hello', 'cruel', 'world'],
130+
});
124131
});
125132
});
126133
});

tests/collectionStore.test.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -73,44 +73,43 @@ describe('Collection Store', () => {
7373
test('single entity set action creator', () => {
7474
expect(actions.set('hello')).toEqual({
7575
type: `REDEUCE:COLLECTION@@${entityName}@@SET`,
76-
payload: ['hello'],
76+
payload: 'hello',
7777
});
7878
});
7979
test('single update action creator', () => {
8080
expect(actions.update('hello')).toEqual({
8181
type: `REDEUCE:COLLECTION@@${entityName}@@UPDATE`,
82-
payload: ['hello'],
82+
payload: 'hello',
8383
});
8484
});
8585
test('single delete action creator', () => {
8686
expect(actions.delete('hello')).toEqual({
8787
type: `REDEUCE:COLLECTION@@${entityName}@@DELETE`,
88-
payload: ['hello'],
88+
payload: 'hello',
8989
});
9090
});
9191

9292
test('bulk entities set action creator', () => {
9393
expect(actions.merge('hello')).toEqual({
9494
type: `REDEUCE:COLLECTION@@${entityName}@@MERGE`,
95-
payload: ['hello'],
95+
payload: 'hello',
9696
});
9797
});
9898
test('bulk update action creator', () => {
9999
expect(actions.mergeDeep('hello')).toEqual({
100100
type: `REDEUCE:COLLECTION@@${entityName}@@MERGEDEEP`,
101-
payload: ['hello'],
101+
payload: 'hello',
102102
});
103103
});
104104
test('bulk delete action creator', () => {
105105
expect(actions.deleteAll('hello')).toEqual({
106106
type: `REDEUCE:COLLECTION@@${entityName}@@DELETEALL`,
107-
payload: ['hello'],
107+
payload: 'hello',
108108
});
109109
});
110110
test('clear action creator', () => {
111111
expect(actions.clear()).toEqual({
112112
type: `REDEUCE:COLLECTION@@${entityName}@@CLEAR`,
113-
payload: [],
114113
});
115114
});
116115
});

tests/simpleStore.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('Simple Store', () => {
6868
test('single entity set action creator', () => {
6969
expect(actions.set('hello')).toEqual({
7070
type: `REDEUCE:SIMPLE@@${entityName}@@SET`,
71-
payload: ['hello'],
71+
payload: 'hello',
7272
});
7373
});
7474
});

0 commit comments

Comments
 (0)