-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkeyExtractorById.js
More file actions
44 lines (38 loc) · 858 Bytes
/
Copy pathkeyExtractorById.js
File metadata and controls
44 lines (38 loc) · 858 Bytes
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
// @flow
type KeyExtractorByIdConfigurationType = {
clear?: Array<string>,
set?: Array<string>,
extractionKey?: string,
idKey?: string,
default: mixed,
};
type KeyExtractorByIdActionType = {
type: string,
payload: Object,
};
const keyExtractorById = (configuration: KeyExtractorByIdConfigurationType) => (
state: {[ID_TYPE]: mixed} = {},
action: KeyExtractorByIdActionType,
): mixed => {
const {
clear,
set,
extractionKey,
idKey = 'id',
} = configuration;
if (clear != null && clear.includes(action.type)) {
return {
...state,
[action.payload[idKey]]: undefined,
};
}
if (set != null && set.includes(action.type)) {
const { payload } = action;
return {
...state,
[payload[idKey]]: payload[extractionKey],
};
}
return state;
};
export default keyExtractorById;