forked from redux-utilities/redux-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflattenWhenNode.js
More file actions
45 lines (38 loc) · 1.25 KB
/
Copy pathflattenWhenNode.js
File metadata and controls
45 lines (38 loc) · 1.25 KB
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
import isMap from 'lodash/isMap';
import { DEFAULT_NAMESPACE, ACTION_TYPE_DELIMITER } from '../constants';
import ownKeys from './ownKeys';
function get(key, x) {
return isMap(x) ? x.get(key) : x[key];
}
export default predicate =>
function flatten(
map,
{ namespace = DEFAULT_NAMESPACE, prefix } = {},
partialFlatMap = {},
partialFlatActionType = ''
) {
function connectNamespace(type) {
if (!partialFlatActionType) return type;
const types = type.toString().split(ACTION_TYPE_DELIMITER);
const partials = partialFlatActionType.split(ACTION_TYPE_DELIMITER);
return []
.concat(...partials.map(p => types.map(t => `${p}${namespace}${t}`)))
.join(ACTION_TYPE_DELIMITER);
}
function connectPrefix(type) {
if (partialFlatActionType || !prefix) {
return type;
}
return `${prefix}${namespace}${type}`;
}
ownKeys(map).forEach(type => {
const nextNamespace = connectPrefix(connectNamespace(type));
const mapValue = get(type, map);
if (predicate(mapValue)) {
flatten(mapValue, { namespace, prefix }, partialFlatMap, nextNamespace);
} else {
partialFlatMap[nextNamespace] = mapValue;
}
});
return partialFlatMap;
};