-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.js
149 lines (129 loc) · 3.23 KB
/
modules.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
141
142
143
144
145
146
147
148
149
// types
const PRESS_NUM = "PRESS_NUM";
const ENTER = "ENTER";
const CALCULATION = "CALCULATION";
const CLEAR = "CLEAR";
const SWAP = "SWAP";
const TOGGLE_NEGATIVE = "TOGGLE_NEGATIVE";
// actions
export const pressNum = (num) => ({
type: PRESS_NUM,
payload: num,
});
export const pressEnter = () => ({
type: ENTER,
});
export const processCalculation = (operation) => ({
type: CALCULATION,
payload: operation,
});
export const pressClear = () => ({
type: CLEAR,
});
export const pressSwap = () => ({
type: SWAP,
});
export const processToggle = (idx) => ({
type: TOGGLE_NEGATIVE,
payload: idx,
});
const getCalculatedValue = (x, y, operationType) => {
if (operationType === "pow") {
return y ** x;
} else if (operationType === "+") {
return y + x;
} else if (operationType === "-") {
return y - x;
} else if (operationType === "/") {
return y / x;
} else if (operationType === "X") {
return y * x;
}
return 0;
};
const toggleNegative = (num) => {
if (num.startsWith("-")) {
return num.slice(1);
}
return `-${num}`;
};
// Check Points (dots) and leading zeros
const checkMultiplePointsAndZeros = (num) => {
return (
num
.replace(/[^0-9.-]/g, "")
.replace(".", "x")
.replace(/\./g, "")
.replace("x", ".")
// .replace(/(?!^)-/g, "")
.replace(/^0+(\d)/gm, "$1")
);
};
const initialState = { stack: [], inputState: "replace" };
// inputState would be append or replace or push
// ...state.stack.slice(1) means that
// a = [1, 3];
// b = [2];
// c = [...a, ...b];
// d = [1, 3, 2];
export const reducer = (state = initialState, { type, payload }) => {
// console.log(state.stack, type, payload);
switch (type) {
case CALCULATION:
const x = !isNaN(parseFloat(state.stack[0]))
? parseFloat(state.stack[0])
: 0;
const y = !isNaN(parseFloat(state.stack[1]))
? parseFloat(state.stack[1])
: 0;
return {
stack: [
`${getCalculatedValue(x, y, payload)}`,
...state.stack.slice(2),
],
inputState: "push",
};
case ENTER:
return {
stack: [state.stack[0] || "0", ...state.stack],
inputState: "replace",
};
case CLEAR:
return initialState;
case SWAP:
return {
stack: [state.stack[1], state.stack[0], ...state.stack.slice(2)],
inputState: "push",
};
case TOGGLE_NEGATIVE:
return {
stack: state.stack.map((num, idx) =>
payload === idx ? toggleNegative(num) : num
),
inputState: state.inputState,
};
case PRESS_NUM:
if (state.inputState === "append") {
const num = (state.stack[0] || "0") + payload;
return {
...state,
stack: [checkMultiplePointsAndZeros(num), ...state.stack.slice(1)],
inputState: "append",
};
} else if (state.inputState === "replace") {
return {
...state,
stack: [payload, ...state.stack.slice(1)],
inputState: "append",
};
} else if (state.inputState === "push") {
return {
stack: [payload, ...state.stack],
inputState: "append",
};
}
break;
default:
return state;
}
};