-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (64 loc) · 2.48 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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.duxedo = {}));
}(this, function (exports) { 'use strict';
// Assign each item in the array given, as the key and value in an
// immutable object.
var generateActions = (list = []) =>
Object.freeze(Object.assign(...list.map(key => ({ [key]: key }))));
// convert Action strings such as 'ACTION_DEFINED' to camelcase which will
// be used as the Action Creator function name.
const camelCaseAction = action =>
action
.toLowerCase()
.trim()
.split(/[.\-_\s]/g)
.reduce((str, word) => str + word[0].toUpperCase() + word.slice(1));
// Create a default FSA compliant actionCreator function that accepts
// an Object payload, a meta object, and an error.
const defaultAction = type => (
payload = {},
meta = {},
error = false,
) => ({ type, payload, meta, error });
// Assign each item in the array given, as the camelCased key for an action
// creator with the same type.
var generateActionCreators = (list = []) =>
Object.freeze(
Object.assign(
...list.map(key => ({ [camelCaseAction(key)]: defaultAction(key) })),
),
);
// Return a reducer function, using the command pattern,
// that can be passed to redux
var generateReducer = ({ definition = {}, defaultState = {} }) => {
const obj = {
...definition,
__run(previousState = defaultState, action = {}) {
// return the previous state if this action doesn't exist
if (!this[action.type]) {
return previousState;
}
// run the function, to update state
return this[action.type](previousState, action);
},
};
return function reducer(previousState = defaultState, action) {
return obj.__run(previousState, action);
};
};
var index = ({ definition = {}, defaultState = {}, options = {} }) => {
const keys = Object.keys(definition);
return {
constants: generateActions(keys),
actions: generateActionCreators(keys),
reducer: generateReducer({ definition, defaultState }),
};
};
exports.default = index;
exports.generateActions = generateActions;
exports.generateActionCreators = generateActionCreators;
exports.generateReducer = generateReducer;
Object.defineProperty(exports, '__esModule', { value: true });
}));