This repository was archived by the owner on Jan 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
313 lines (295 loc) · 12.6 KB
/
Copy pathApp.js
File metadata and controls
313 lines (295 loc) · 12.6 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import React, { Component } from 'react';
import math from 'mathjs';
import Casing from './Components/Casing';
import './App.css';
import { btnLabels } from './Components/content/btnLabels';
class App extends Component {
constructor() {
super();
// methods
this.addToOperation = this.addToOperation.bind(this);
this.handleBtnClick = this.handleBtnClick.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.deleteFromOperation = this.deleteFromOperation.bind(this);
this.allClear = this.allClear.bind(this);
this.brackets = this.brackets.bind(this);
this.hitIt = this.hitIt.bind(this);
//helpers
this.replaceOperator = this.replaceOperator.bind(this);
this.playKeySound = this.playKeySound.bind(this);
//state and content
this.buttons = btnLabels;
this.state = {
screenDigit: "0",
currentOperation: "0"
};
}
// LIFECYCLE METHODS
componentDidMount() {
document.addEventListener('keyup', this.handleKeyPress);
}
// COMPONENT METHODS
deleteFromOperation() {
this.setState((prevState) => {
return { currentOperation: prevState.currentOperation.slice(0, prevState.currentOperation.length-1) || "0",
screenDigit: prevState.currentOperation.charAt(prevState.currentOperation.length-2) || "0" }
});
}
allClear() {
this.setState((prevState) => {
return { currentOperation: "0",
screenDigit: "0" }
});
}
addToOperation(btnValue) {
btnValue = btnValue.toLowerCase();
//setState with callback
this.setState((prevState) => {
return { currentOperation: (prevState.currentOperation === "0") ?
prevState.currentOperation = btnValue :
prevState.currentOperation += btnValue,
screenDigit: btnValue.charAt(btnValue.length-1) }
});
}
//helper function to replace an operator with a new operator
replaceOperator(oldOperator, newOperator) {
if (oldOperator === newOperator) {return}
//allow addition of negative after bracket
if (oldOperator === "(" && newOperator === "-") {
this.addToOperation(newOperator);
return}
//prevent replacement of opening bracket for remaining operators
if (oldOperator === "(") return;
else if (oldOperator === "/" && newOperator === "÷") {return}
else if (newOperator === "÷") {
this.deleteFromOperation();
this.addToOperation("/")
}
else {
this.deleteFromOperation();
this.addToOperation(newOperator)
}
return;
}
//function for the "( )" button to figure out if opening or closing bracket needed
brackets(mathString) {
//replace string operators with mathematical operators
mathString = mathString.replace(/x/ig, "*");
let bracket; //use to set screenDigit state and to append to mathString to create new currentOperation
let openParen = mathString.match(/\(/g) || []; //match returns array and then use length to count occurance in string
let closeParen = mathString.match(/\)/g) || []; //match returns array and then use length to count occurance in string
openParen = openParen.length;
closeParen = closeParen.length;
const endOfStr = mathString.length-1; // index of last character
if (openParen !== closeParen && openParen > 0) {
if ( !/[+\-*\/\(]/.test(mathString.charAt(endOfStr)) ) {
bracket = ")";
}
else if ( /\(/.test(mathString.charAt(endOfStr)) ) {
bracket = "(";
}
else {
console.error(new Error("bracket disallowed"));
return;
}
mathString = `${mathString}${bracket}`;
}
else {
// if nothing in current operation insert opening bracket
if (mathString === "0") {
mathString = "(";
bracket = "(";
}
else {
//check if operator at end of string
if (/\./.test(mathString.charAt(endOfStr))) console.error(new Error("Not allowed bracket directly after period"));
if ( /[+\-*\/]/.test(mathString.charAt(endOfStr)) ) {
bracket = "(";
}
else {
console.error(new Error("bracket disallowed"));
return;
}
mathString = `${mathString}${bracket}`;
}
}
//replace math operators with string operators
mathString = mathString.replace(/\*/ig, "x");
this.setState({
currentOperation: mathString ,
screenDigit: bracket
});
}
playKeySound() {
this.refs.keysound.volume = 0.3;
this.refs.keysound.currentTime = 0;
this.refs.keysound.play();
}
//TO DO disallow numeral after closing bracket
handleKeyPress(e) {
// boolean flag for which keys to listen to
const valid = /[\d+=\-*/\.)(]|Backspace|Esc(ape)*|Enter/i.test(e.key) || e.keyCode === 13;
if (!valid) return;
const operator = new RegExp(/[+=\-*x/\(]/i); //opening bracket included to prevent operator directly after (
const started = !/^0$/.test(this.state.currentOperation); //boolean to show if operation begun
const first = /^\-?\(?\d+\.?\d*$/.test(this.state.currentOperation); // boolean flag to check first entry is operand - used for brackets keys
const lastDigit = this.state.currentOperation.substr(-1); // use to run tests on final char of operation
let openParen = this.state.currentOperation.match(/\(/g) || []; //match returns array and then use length to count occurance in string
let closeParen = this.state.currentOperation.match(/\)/g) || []; //match returns array and then use length to count occurance in string
openParen = openParen.length;
closeParen = closeParen.length;
switch (e.key) {
//TO DO only allow closing bracket if open bracket exists and is unclosed - check for number of open
//case "(": if (!/\)/.test(this.state.screenDigit)) this.addToOperation(e.key);
case "(": if (!/\)/.test(lastDigit)) this.addToOperation(e.key);
break;
case ")": if ( !first &&
!operator.test(lastDigit) &&
!/\(/.test(lastDigit) &&
(openParen > closeParen) //prevent multiple closing brackets unless opening bracket exists
)
{
this.addToOperation(e.key);
}
break;
case "Backspace": this.deleteFromOperation();
break;
case "Esc":
case "Escape": this.allClear();
break;
case "Enter":
case "=": this.hitIt(this.state.currentOperation);
break;
case "*": //key is * so needs translation
if (started) {
if (!operator.test(lastDigit)) this.addToOperation("x");
else this.replaceOperator(lastDigit, "x");
}
break;
case "-":
case "+":
case "/": if (!operator.test(lastDigit)) {
if (started || (!started && e.key === "-") ) this.addToOperation(e.key);
}
else {
this.replaceOperator(lastDigit, e.key);
}
break;
case ".": if ( !/\./.test(lastDigit) && //dissallow period after a period
!/\d+\.\d+$/g.test(this.state.currentOperation) //dissallow period after a decimal
) {
if (started) {/\d$/g.test(lastDigit) ?
this.addToOperation(e.key) :
this.addToOperation(`0${e.key}`);
}
else {
this.addToOperation(`0${e.key}`);
}
}
break;
default: // if statement prevents digits directly after closing bracket
// allows digits in all other cases
if ( lastDigit !== ")" ) this.addToOperation(e.key);
}
this.playKeySound();
}
handleBtnClick(buttonText) {
const special = /[+=\-x÷)(\.]|( )|AC|C/i.test(buttonText);
const started = !/^0$/.test(this.state.currentOperation); //boolean to show if operation begun
const lastDigit = this.state.currentOperation.substr(-1); // use to run tests on final char of operation
if (!special) {
// if statement prevents digits directly after closing bracket
// allows digits in all other cases
if (lastDigit !== ")") this.addToOperation(buttonText);
}
else {
const operator = new RegExp(/[+=\-x/\(]/i); //opening bracket included to prevent operator directly after (
switch (buttonText) {
case "C": this.deleteFromOperation();
break;
case "AC": this.allClear();
break;
case "=": this.hitIt(this.state.currentOperation);
break;
case "( )": this.brackets(this.state.currentOperation);
break;
case "÷": if (!operator.test(lastDigit)) this.addToOperation("/");
else this.replaceOperator(lastDigit, "÷");
break;
case "+":
case "-":
case "x": if (!operator.test(lastDigit)) {
this.addToOperation(buttonText);
}
else {
this.replaceOperator(lastDigit, buttonText);
}
break;
case ".": if ( !/\.$/g.test(lastDigit) && //dissallow period after a period
!/\d+\.\d+$/g.test(this.state.currentOperation) //dissallow period after a decimal
) {
if (started) {/\d$/g.test(lastDigit) ?
this.addToOperation(buttonText) :
this.addToOperation(`0${buttonText}`);
}
else {
this.addToOperation(`0${buttonText}`);
}
}
break;
default: this.addToOperation(buttonText);
}
}
this.playKeySound();
}
//let's do the maths, people!
hitIt(mathString) {
// do some sanitisation - mostly as a just in case as the function SHOULD only be called on limited inputs
mathString = mathString.replace(/<\/*script>|[<>]/gi, "")
//replace string operators with mathematical operators
.replace(/x/ig, "*")
// strip 0 before integer i.e. no 5x02, only 5x2 it kills math.eval method used below
.replace(/(0+)(\d+)/g, (match, cg1, cg2) => cg2);
// make sure the last char isn't an operator. Allow closing bracket
let endOfStr = mathString.length-1; // index of last character
while ( /[+\-*\/(\.]/i.test(mathString.charAt(endOfStr)) ) {
mathString = mathString.substring(0, endOfStr);
//prevent solo operator from stripping out state i.e. a single + being stripped and collapsing screen
if (endOfStr === 0) {mathString = "0"};
endOfStr--;
}
// Handle parenthesis matching
let openParen = mathString.match(/\(/g) || []; //match returns array and then use length to count occurance in string
let closeParen = mathString.match(/\)/g) || []; //match returns array and then use length to count occurance in string
openParen = openParen.length;
closeParen = closeParen.length;
if (openParen !== closeParen) {
const err = (openParen > closeParen) ? new Error("an unclosed parenthesis"): new Error("a missing opening bracket") ;
console.error(err);
alert(`Your operation has mismatched parenthseses
You have ${err.message}`);
return err;
}
const result = typeof math.eval(mathString) === 'number' ? math.eval(mathString).toString() : '';
//console.log(`Eval: ${eval(mathString)}`); //check against library result to see if library is wasteful dependancy
// replace mathematical operators with string operators before it goes back into state
mathString = mathString.replace(/\*/ig, "x");
this.setState({ screenDigit: result,
currentOperation: mathString });
return result;
}
render() {
return (
<div className="App">
<div className="flex-wrapper">
<Casing buttons={this.buttons}
screenDigit={this.state.screenDigit}
currentOperation={this.state.currentOperation}
handleBtnClick={this.handleBtnClick} />
</div>
<audio ref="keysound" src="/sounds/keypress.wav"></audio>
</div>
);
}
}
export default App;