aprog 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/package.json +1 -1
- package/src/math.js +9 -13
package/README.md
CHANGED
|
@@ -128,10 +128,16 @@ console.log(message);
|
|
|
128
128
|
|
|
129
129
|
```shell
|
|
130
130
|
|
|
131
|
-
onInput: (event) =>
|
|
131
|
+
onInput: (event) => {
|
|
132
|
+
const res = mathInput(event.target);
|
|
133
|
+
console.log(res);
|
|
134
|
+
}
|
|
132
135
|
|
|
133
136
|
```
|
|
134
|
-
Результат: 4
|
|
137
|
+
Результат: 4 *(повертається з функції та встановлює значення у input)*
|
|
138
|
+
```shell
|
|
139
|
+
<input value="4">
|
|
140
|
+
```
|
|
135
141
|
|
|
136
142
|
########## ########## ########## ########## ##########
|
|
137
143
|
|
package/package.json
CHANGED
package/src/math.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
export const mathInput = (target) => {
|
|
2
2
|
const enter = target.value.includes("=");
|
|
3
|
-
|
|
4
|
-
const parts = target.value.slice(0, -1).split(/([\+\-\*\/])/);
|
|
5
|
-
|
|
6
|
-
const num1 = Number(parts[0]);
|
|
7
|
-
const operator = parts[1];
|
|
8
|
-
const num2 = Number(parts[2]);
|
|
3
|
+
let result = target.value;
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
default: result = target.value;
|
|
5
|
+
if (enter) {
|
|
6
|
+
const expression = target.value.slice(0, -1);
|
|
7
|
+
try {
|
|
8
|
+
result = new Function(`return ${expression}`)();
|
|
9
|
+
} catch (error) {
|
|
10
|
+
result = target.value;
|
|
17
11
|
}
|
|
18
12
|
|
|
19
13
|
target.value = result;
|
|
20
14
|
}
|
|
15
|
+
|
|
16
|
+
return result;
|
|
21
17
|
}
|