safe-math-ops 0.0.5 → 0.0.6

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 CHANGED
@@ -3,6 +3,18 @@
3
3
  The goal of this package was to prevent weird decimals from appearing.
4
4
  e.g. 9.99 - 8 = 1.9900000000000002 instead of 1.99
5
5
 
6
+ Example:
7
+
8
+ ```ts
9
+ import safeMath from "safe-math-ops";
10
+
11
+ // Expected output: 1.99
12
+ console.log(safeMath.substract([9.99, 8]));
13
+
14
+ // Expected output: 1.9900000000000002
15
+ console.log(9.99 - 8);
16
+ ```
17
+
6
18
  To install dependencies:
7
19
 
8
20
  ```bash
package/dist/index.d.ts CHANGED
@@ -1,14 +1,19 @@
1
1
  import add from "./libs/add";
2
- import calcHighestDecimalPoint from "./libs/calcHighestDecimalPoint";
2
+ import calcHighestDecimalPoint, { calcDecimals } from "./libs/calcHighestDecimalPoint";
3
3
  import divide from "./libs/divide";
4
+ import getPow from "./libs/getPow";
4
5
  import multiply from "./libs/multiply";
6
+ import replaceByInt from "./libs/replaceByInt";
5
7
  import substract from "./libs/substract";
6
- export { add, substract, multiply, divide, calcHighestDecimalPoint };
8
+ export { add, substract, multiply, divide, calcHighestDecimalPoint, calcDecimals, getPow, replaceByInt, };
7
9
  declare const _default: {
8
10
  add: typeof add;
9
11
  substract: typeof substract;
10
12
  multiply: typeof multiply;
11
13
  divide: typeof divide;
12
14
  calcHighestDecimalPoint: typeof calcHighestDecimalPoint;
15
+ calcDecimals: typeof calcDecimals;
16
+ getPow: typeof getPow;
17
+ replaceByInt: typeof replaceByInt;
13
18
  };
14
19
  export default _default;
package/dist/index.js CHANGED
@@ -1 +1,89 @@
1
- var s={};setInterval(()=>{s={}},15000);var i=s;function c(l,o){let t=0;for(let e of l){if(o==="add")t+=e;if(o==="substract")t-=e;if(o==="divide")t/=e;if(o==="multiply")t*=e}t=t*l.length+"";let r=i[t];if(!r)i[t]=l.filter((e)=>typeof e==="number").map((e)=>(e.toString().split(".")?.[1]||"").length).sort((e,a)=>a-e)?.[0]||0,r=i[t];return{result:r,key:t}}function m(...l){let o=c(l,"add"),t="a"+o.key,r=i[t];if(!r)i[t]=parseFloat(l.filter((e)=>typeof e==="number").reduce((e,a)=>e+a).toFixed(o.result)),r=i[t];return r}function f(...l){let o=c(l,"divide"),t="d"+o.key,r=i[t];if(!r)i[t]=parseFloat(l.filter((e)=>typeof e==="number").reduce((e,a)=>e/a).toFixed(o.result)),r=i[t];return r}function n(...l){let o=c(l,"multiply"),t="m"+o.key,r=i[t];if(!r)i[t]=parseFloat(l.filter((e)=>typeof e==="number").reduce((e,a)=>e*a).toFixed(o.result)),r=i[t];return r}function u(...l){let o=c(l,"substract"),t="s"+o.key,r=i[t];if(!r)i[t]=parseFloat(l.filter((e)=>typeof e==="number").reduce((e,a)=>e-a).toFixed(o.result)),r=i[t];return r}var B={add:m,substract:u,multiply:n,divide:f,calcHighestDecimalPoint:c};export{u as substract,n as multiply,f as divide,B as default,c as calcHighestDecimalPoint,m as add};
1
+ // src/libs/calcHighestDecimalPoint.ts
2
+ function calcDecimals(number) {
3
+ const int = Math.trunc(number);
4
+ const len = (int + "").length + 1;
5
+ return number % 1 !== 0 ? (number + "").slice(len).length : 0;
6
+ }
7
+ function calcHighestDecimalPoint(numbers) {
8
+ let highest = 0;
9
+ for (let i = 0;i < numbers.length; i++) {
10
+ if ((numbers?.[i] || 0) % 1 === 0)
11
+ continue;
12
+ let v = calcDecimals(numbers?.[i] || 0);
13
+ if (v > highest)
14
+ highest = v;
15
+ }
16
+ return highest;
17
+ }
18
+
19
+ // src/libs/getPow.ts
20
+ function getPow(amt) {
21
+ return amt === 0 ? 1 : 10 ** amt;
22
+ }
23
+
24
+ // src/libs/add.ts
25
+ function add(numbers) {
26
+ const highDecimalPoint = calcHighestDecimalPoint(numbers);
27
+ const multiplier = getPow(highDecimalPoint);
28
+ return numbers.reduce((a, b) => {
29
+ return a * multiplier + b * multiplier;
30
+ }) / multiplier;
31
+ }
32
+
33
+ // src/libs/divide.ts
34
+ function divide(numbers) {
35
+ const highDecimalPoint = calcHighestDecimalPoint(numbers);
36
+ const multiplier = getPow(highDecimalPoint);
37
+ return numbers.reduce((a, b) => {
38
+ return a * multiplier / (b * multiplier);
39
+ }) / multiplier;
40
+ }
41
+
42
+ // src/libs/multiply.ts
43
+ function multiply(numbers) {
44
+ const highDecimalPoint = calcHighestDecimalPoint(numbers);
45
+ const multiplier = getPow(highDecimalPoint);
46
+ return numbers.reduce((a, b) => {
47
+ return a * multiplier * (b * multiplier);
48
+ }) / multiplier;
49
+ }
50
+
51
+ // src/libs/replaceByInt.ts
52
+ function replaceByInt(numbers, multiplier) {
53
+ for (let i = 0;i < numbers.length; i++) {
54
+ numbers[i] *= multiplier;
55
+ }
56
+ return numbers;
57
+ }
58
+
59
+ // src/libs/substract.ts
60
+ function substract(numbers) {
61
+ const highDecimalPoint = calcHighestDecimalPoint(numbers);
62
+ const multiplier = getPow(highDecimalPoint);
63
+ return numbers.reduce((a, b) => {
64
+ return a * multiplier - b * multiplier;
65
+ }) / multiplier;
66
+ }
67
+
68
+ // src/index.ts
69
+ var src_default = {
70
+ add,
71
+ substract,
72
+ multiply,
73
+ divide,
74
+ calcHighestDecimalPoint,
75
+ calcDecimals,
76
+ getPow,
77
+ replaceByInt
78
+ };
79
+ export {
80
+ substract,
81
+ replaceByInt,
82
+ multiply,
83
+ getPow,
84
+ divide,
85
+ src_default as default,
86
+ calcHighestDecimalPoint,
87
+ calcDecimals,
88
+ add
89
+ };
@@ -0,0 +1 @@
1
+ export default function add(numbers: number[]): number;
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ [key: string]: any;
3
+ };
4
+ export default _default;
@@ -0,0 +1,2 @@
1
+ export declare function calcDecimals(number: number): number;
2
+ export default function calcHighestDecimalPoint(numbers: number[]): number;
@@ -0,0 +1 @@
1
+ export default function divide(numbers: number[]): number;
@@ -0,0 +1 @@
1
+ export default function getMultiplier(amt: number): number;
@@ -0,0 +1 @@
1
+ export default function getPow(amt: number): number;
@@ -0,0 +1 @@
1
+ export default function multiply(numbers: number[]): number;
@@ -0,0 +1 @@
1
+ export default function replaceByInt(numbers: number[], multiplier: number): number[];
@@ -0,0 +1 @@
1
+ export default function substract(numbers: number[]): number;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "safe-math-ops",
3
3
  "module": "src/index.ts",
4
4
  "type": "module",
5
- "version": "0.0.5",
5
+ "version": "0.0.6",
6
6
  "devDependencies": {
7
7
  "@types/bun": "latest",
8
8
  "rimraf": "^6.1.3"
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "author": "ctev",
19
19
  "scripts": {
20
- "build": "bun build --minify --target=node ./src/index.ts --outfile=dist/index.js && bun run build:declaration",
20
+ "build": "bun build --target=node ./src/index.ts --outfile=dist/index.js && bun run build:declaration",
21
21
  "build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json",
22
22
  "postbuild": "rimraf tsconfig.types.tsbuildinfo"
23
23
  },
@@ -26,6 +26,7 @@
26
26
  "description": "The goal of this package was to prevent weird decimals from appearing when doing operations (e.g. 9.99 - 8 = 1.9900000000000002 instead of 1.99)",
27
27
  "files": [
28
28
  "dist/*.js",
29
- "dist/*.d.ts"
29
+ "dist/*.d.ts",
30
+ "dist/**/*.d.ts"
30
31
  ]
31
32
  }