safe-math-ops 0.0.2 → 0.0.4
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/dist/index.js +61 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,31 +1,81 @@
|
|
|
1
|
+
// src/libs/cache.ts
|
|
2
|
+
var cache = {};
|
|
3
|
+
setInterval(() => {
|
|
4
|
+
cache = {};
|
|
5
|
+
}, 15000);
|
|
6
|
+
var cache_default = cache;
|
|
7
|
+
|
|
1
8
|
// src/libs/calcHighestDecimalPoint.ts
|
|
2
|
-
function calcHighestDecimalPoint(
|
|
3
|
-
|
|
4
|
-
|
|
9
|
+
function calcHighestDecimalPoint(numbers, type) {
|
|
10
|
+
let key = 0;
|
|
11
|
+
for (let i of numbers) {
|
|
12
|
+
if (type === "add")
|
|
13
|
+
key += i;
|
|
14
|
+
if (type === "substract")
|
|
15
|
+
key -= i;
|
|
16
|
+
if (type === "divide")
|
|
17
|
+
key /= i;
|
|
18
|
+
if (type === "multiply")
|
|
19
|
+
key *= i;
|
|
20
|
+
}
|
|
21
|
+
key = key * numbers.length + "";
|
|
22
|
+
let result = cache_default[key];
|
|
23
|
+
if (!result) {
|
|
24
|
+
cache_default[key] = numbers.filter((v) => typeof v === "number").map((v) => (v.toString().split(".")?.[1] || "").length).sort((a, b) => b - a)?.[0] || 0;
|
|
25
|
+
result = cache_default[key];
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
result,
|
|
29
|
+
key
|
|
30
|
+
};
|
|
5
31
|
}
|
|
6
32
|
|
|
7
33
|
// src/libs/add.ts
|
|
8
34
|
function add(...numbers) {
|
|
9
|
-
const highDecimalPoint = calcHighestDecimalPoint(
|
|
10
|
-
|
|
35
|
+
const highDecimalPoint = calcHighestDecimalPoint(numbers, "add");
|
|
36
|
+
const key = "a" + highDecimalPoint.key;
|
|
37
|
+
let result = cache_default[key];
|
|
38
|
+
if (!result) {
|
|
39
|
+
cache_default[key] = parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a + b).toFixed(highDecimalPoint.result));
|
|
40
|
+
result = cache_default[key];
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
11
43
|
}
|
|
12
44
|
|
|
13
45
|
// src/libs/divide.ts
|
|
14
46
|
function divide(...numbers) {
|
|
15
|
-
const highDecimalPoint = calcHighestDecimalPoint(
|
|
16
|
-
|
|
47
|
+
const highDecimalPoint = calcHighestDecimalPoint(numbers, "divide");
|
|
48
|
+
const key = "d" + highDecimalPoint.key;
|
|
49
|
+
let result = cache_default[key];
|
|
50
|
+
if (!result) {
|
|
51
|
+
cache_default[key] = parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a / b).toFixed(highDecimalPoint.result));
|
|
52
|
+
result = cache_default[key];
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
17
55
|
}
|
|
18
56
|
|
|
19
57
|
// src/libs/multiply.ts
|
|
20
58
|
function multiply(...numbers) {
|
|
21
|
-
const highDecimalPoint = calcHighestDecimalPoint(
|
|
22
|
-
|
|
59
|
+
const highDecimalPoint = calcHighestDecimalPoint(numbers, "multiply");
|
|
60
|
+
const key = "m" + highDecimalPoint.key;
|
|
61
|
+
let result = cache_default[key];
|
|
62
|
+
if (!result) {
|
|
63
|
+
cache_default[key] = parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a * b).toFixed(highDecimalPoint.result));
|
|
64
|
+
result = cache_default[key];
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
23
67
|
}
|
|
24
68
|
|
|
25
69
|
// src/libs/substract.ts
|
|
26
70
|
function substract(...numbers) {
|
|
27
|
-
const highDecimalPoint = calcHighestDecimalPoint(
|
|
28
|
-
|
|
71
|
+
const highDecimalPoint = calcHighestDecimalPoint(numbers, "substract");
|
|
72
|
+
const key = "s" + highDecimalPoint.key;
|
|
73
|
+
let result = cache_default[key];
|
|
74
|
+
if (!result) {
|
|
75
|
+
cache_default[key] = parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a - b).toFixed(highDecimalPoint.result));
|
|
76
|
+
result = cache_default[key];
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
29
79
|
}
|
|
30
80
|
|
|
31
81
|
// src/index.ts
|