-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
140 lines (110 loc) · 3.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const invert = method => arg => arg[method]();
const updateFuncs = {
PREPEND: (arr, val) => [val, ...arr],
APPEND: (arr, val) => [...arr, val],
INSERT: (arr, val, index)=> [...arr.slice(0, index), val, ...arr.slice(index,)],
REPLACE: (arr, val, index) => [...arr.slice(0, index), val, ...arr.slice(index + 1,)],
REMOVE: (arr, _val, index) => [...arr.slice(0, index), ...arr.slice(index + 1,)],
TOGGLE: bool => !bool,
INCREMENT: val => val + 1,
DECREMENT: val => val - 1
}
const attrReducer = (cb, at) => (state, action) => {
const { id, index, value } = action.payload;
const resource = state[id];
const attr = resource[at];
return {
...state,
[id]: {
...resource,
[at]: cb(attr, value, index),
}
}
};
/**
* A utility which takes a resource schema and returns an object containing
* reducer functions.
*
*
* import juice from 'juice';
*
* const userSchema = {
* name: String,
* age: Number,
* likeCount: { type: Number, func: 'INC', default: 0 },
*
* };
*
* const { byId, all } = juice(userSchema, 'user');
*
* const userReducer = combineReducers({ byId, all });
*
* juice
* @param {object} schema model schema
* @param {string} rawName model name
*/
module.exports = (schema, rawName) => {
if(!rawName) throw new Error('Please provide a name for your model');
const name = rawName.toUpperCase();
// get default values if any
const initial = Object.keys(schema)
.filter(at => schema[at].default !== undefined)
.reduce((acc, at) => ({ ...acc, [at]: schema[at].default }), {})
const funcRepo = Object.keys(schema).reduce((acc, attr) => {
// scope by resource name and attribute name
const scope = [name, attr].map(invert('toUpperCase')).join('_');
let config = schema[attr];
if(Array.isArray(config)) {
config = config[0];
}
const { func } = config;
if(func) {
if(Array.isArray(func)) {
// more than one function provided
func.forEach((f) => {
if (updateFuncs[f]) {
const functionToReduce = updateFuncs[f];
acc[`${scope}_${f}`] = attrReducer(functionToReduce, attr);
} else if (typeof f === 'object') {
// custom function
acc[`${scope}_${f.name}`] = attrReducer(f.func, attr);
} else {
throw new Error(`${f} is not a known function`)
}
})
} else {
// single function provided
if (updateFuncs[func]) {
const functionToReduce = updateFuncs[func];
acc[`${scope}_${func}`] = attrReducer(functionToReduce, attr);
} else if (typeof func === 'object') {
// custom function
acc[`${scope}_${func.name}`] = attrReducer(func.func, attr);
} else {
throw new Error(`${func} is not a known function`)
}
}
} else {
// only a type is provided - provide update reduction
acc[`${scope}_UPDATE`] = attrReducer((_attr, val) => val, attr)
}
acc[`${name}_ADD`] = (state, action) => ({
...state,
[action.payload.id]: { ...initial, ...action.payload }
})
acc[`${name}_REMOVE`] = (state, action) => {
delete state[action.payload.id];
return { ...state };
}
return acc;
}, {});
const byId = (state={}, action) => funcRepo[action.type] ? funcRepo[action.type](state, action) : state;
const all = (state=[], action) => {
switch(action.type) {
case `${name}_ADD`: return [...state, action.payload.id];
case `${name}_REMOVE`: return state.filter(id => id !== action.payload.id)
default: return state;
}
}
return { byId, all }
};