-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcounter.js
More file actions
48 lines (38 loc) · 894 Bytes
/
Copy pathcounter.js
File metadata and controls
48 lines (38 loc) · 894 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
45
46
47
48
// @flow
type CounterConfigurationType = {
incremented?: Array<string>,
decremented?: Array<string>,
reset?: Array<string>,
};
type CounterActionType = {
type: string,
payload: {
step: number,
},
};
const counter = (configuration: CounterConfigurationType) => (
state: number = 0,
action: CounterActionType,
): number => {
const {
incremented,
decremented,
reset,
} = configuration;
const { payload } = action;
if (incremented != null && incremented.includes(action.type)) {
if (typeof payload !== 'undefined') {
return state + (payload.step || 1);
}
}
if (decremented != null && decremented.includes(action.type)) {
if (typeof payload !== 'undefined') {
return state - (payload.step || 1);
}
}
if (reset != null && reset.includes(action.type)) {
return 0;
}
return state;
};
export default counter;