safe-math-ops 0.0.1
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 +15 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +40 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# safe_math_operations
|
|
2
|
+
|
|
3
|
+
To install dependencies:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
To run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run index.ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This project was created using `bun init` in bun v1.3.6. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import add from "./libs/add";
|
|
2
|
+
import calcHighestDecimalPoint from "./libs/calcHighestDecimalPoint";
|
|
3
|
+
import divide from "./libs/divide";
|
|
4
|
+
import multiply from "./libs/multiply";
|
|
5
|
+
import substract from "./libs/substract";
|
|
6
|
+
export { add, substract, multiply, divide, calcHighestDecimalPoint };
|
|
7
|
+
declare const _default: {
|
|
8
|
+
add: typeof add;
|
|
9
|
+
substract: typeof substract;
|
|
10
|
+
multiply: typeof multiply;
|
|
11
|
+
divide: typeof divide;
|
|
12
|
+
calcHighestDecimalPoint: typeof calcHighestDecimalPoint;
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/libs/calcHighestDecimalPoint.ts
|
|
2
|
+
function calcHighestDecimalPoint(...numbers) {
|
|
3
|
+
const r = numbers.filter((v) => typeof v === "number").map((v) => (v.toString().split(".")?.[1] || "").length).sort((a, b) => b - a);
|
|
4
|
+
return r?.[0] || 0;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// src/libs/add.ts
|
|
8
|
+
function add(...numbers) {
|
|
9
|
+
const highDecimalPoint = calcHighestDecimalPoint(...numbers);
|
|
10
|
+
return parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a + b).toFixed(highDecimalPoint));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/libs/divide.ts
|
|
14
|
+
function divide(...numbers) {
|
|
15
|
+
const highDecimalPoint = calcHighestDecimalPoint(...numbers);
|
|
16
|
+
return parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a / b).toFixed(highDecimalPoint));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/libs/multiply.ts
|
|
20
|
+
function multiply(...numbers) {
|
|
21
|
+
const highDecimalPoint = calcHighestDecimalPoint(...numbers);
|
|
22
|
+
return parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a * b).toFixed(highDecimalPoint));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/libs/substract.ts
|
|
26
|
+
function substract(...numbers) {
|
|
27
|
+
const highDecimalPoint = calcHighestDecimalPoint(...numbers);
|
|
28
|
+
return parseFloat(numbers.filter((v) => typeof v === "number").reduce((a, b) => a - b).toFixed(highDecimalPoint));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var src_default = { add, substract, multiply, divide, calcHighestDecimalPoint };
|
|
33
|
+
export {
|
|
34
|
+
substract,
|
|
35
|
+
multiply,
|
|
36
|
+
divide,
|
|
37
|
+
src_default as default,
|
|
38
|
+
calcHighestDecimalPoint,
|
|
39
|
+
add
|
|
40
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "safe-math-ops",
|
|
3
|
+
"module": "src/index.ts",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@types/bun": "latest",
|
|
8
|
+
"rimraf": "^6.1.3"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"typescript": "^5"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"safe math",
|
|
15
|
+
"weird decimal prevent",
|
|
16
|
+
"prevent weird decimal"
|
|
17
|
+
],
|
|
18
|
+
"author": "ctev",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "bun build --target=node ./src/index.ts --outfile=dist/index.js && bun run build:declaration",
|
|
21
|
+
"build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json",
|
|
22
|
+
"postbuild": "rimraf tsconfig.types.tsbuildinfo"
|
|
23
|
+
},
|
|
24
|
+
"main": "dist/index.js",
|
|
25
|
+
"types": "dist/index.d.ts",
|
|
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
|
+
"files": [
|
|
28
|
+
"dist/*.js",
|
|
29
|
+
"dist/*.d.ts"
|
|
30
|
+
]
|
|
31
|
+
}
|