@xylabs/decimal-precision 4.5.9 → 4.6.0
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/neutral/index.mjs +9 -5
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/fromFixedPoint.d.ts +2 -0
- package/dist/types/fromFixedPoint.d.ts.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/toDecimalPrecision.d.ts +2 -0
- package/dist/types/toDecimalPrecision.d.ts.map +1 -0
- package/dist/types/toFixedPoint.d.ts +2 -0
- package/dist/types/toFixedPoint.d.ts.map +1 -0
- package/package.json +7 -6
- package/src/fromFixedPoint.ts +4 -0
- package/src/index.ts +3 -30
- package/src/toDecimalPrecision.ts +8 -0
- package/src/toFixedPoint.ts +16 -0
- package/dist/neutral/index.d.ts +0 -4
- package/dist/neutral/index.d.ts.map +0 -1
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/fromFixedPoint.ts
|
|
2
|
+
var fromFixedPoint = (value, places = 18) => {
|
|
3
|
+
if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`);
|
|
4
|
+
return value / 10n ** BigInt(places);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/toDecimalPrecision.ts
|
|
2
8
|
var toDecimalPrecision = (value, digits) => {
|
|
3
9
|
let fixed = 0;
|
|
4
10
|
const result = Number.parseFloat(value.toPrecision(digits));
|
|
@@ -7,6 +13,8 @@ var toDecimalPrecision = (value, digits) => {
|
|
|
7
13
|
}
|
|
8
14
|
return result.toFixed(fixed);
|
|
9
15
|
};
|
|
16
|
+
|
|
17
|
+
// src/toFixedPoint.ts
|
|
10
18
|
var toFixedPoint = (value, places = 18) => {
|
|
11
19
|
if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`);
|
|
12
20
|
if (typeof value === "string") {
|
|
@@ -23,10 +31,6 @@ var toFixedPoint = (value, places = 18) => {
|
|
|
23
31
|
}
|
|
24
32
|
return value * 10n ** BigInt(places);
|
|
25
33
|
};
|
|
26
|
-
var fromFixedPoint = (value, places = 18) => {
|
|
27
|
-
if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`);
|
|
28
|
-
return value / 10n ** BigInt(places);
|
|
29
|
-
};
|
|
30
34
|
export {
|
|
31
35
|
fromFixedPoint,
|
|
32
36
|
toDecimalPrecision,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/fromFixedPoint.ts","../../src/toDecimalPrecision.ts","../../src/toFixedPoint.ts"],"sourcesContent":["export const fromFixedPoint = (value: bigint, places = 18): bigint => {\n if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)\n return value / (10n ** BigInt(places))\n}\n","export const toDecimalPrecision = (value: number, digits: number) => {\n let fixed = 0\n const result = Number.parseFloat(value.toPrecision(digits))\n while (Number.parseFloat(result.toFixed(fixed)) !== result && fixed < 20) {\n fixed++\n }\n return result.toFixed(fixed)\n}\n","export const toFixedPoint = (value: bigint | string, places = 18): bigint => {\n if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)\n if (typeof value === 'string') {\n const parts = value.split('.')\n if (parts.length > 2) {\n throw new Error('Too many decimals in value')\n }\n if (parts.length === 1) {\n return BigInt(value) * (10n ** BigInt(places))\n }\n const [whole, fraction] = parts\n const trimmed = fraction.slice(0, places)\n return BigInt(whole + trimmed.padEnd(Number(places), '0'))\n }\n return value * (10n ** BigInt(places))\n}\n"],"mappings":";AAAO,IAAM,iBAAiB,CAAC,OAAe,SAAS,OAAe;AACpE,MAAI,CAAC,OAAO,UAAU,MAAM,EAAG,OAAM,IAAI,MAAM,WAAW,MAAM,sBAAsB;AACtF,SAAO,QAAS,OAAO,OAAO,MAAM;AACtC;;;ACHO,IAAM,qBAAqB,CAAC,OAAe,WAAmB;AACnE,MAAI,QAAQ;AACZ,QAAM,SAAS,OAAO,WAAW,MAAM,YAAY,MAAM,CAAC;AAC1D,SAAO,OAAO,WAAW,OAAO,QAAQ,KAAK,CAAC,MAAM,UAAU,QAAQ,IAAI;AACxE;AAAA,EACF;AACA,SAAO,OAAO,QAAQ,KAAK;AAC7B;;;ACPO,IAAM,eAAe,CAAC,OAAwB,SAAS,OAAe;AAC3E,MAAI,CAAC,OAAO,UAAU,MAAM,EAAG,OAAM,IAAI,MAAM,WAAW,MAAM,sBAAsB;AACtF,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,QAAQ,MAAM,MAAM,GAAG;AAC7B,QAAI,MAAM,SAAS,GAAG;AACpB,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAC9C;AACA,QAAI,MAAM,WAAW,GAAG;AACtB,aAAO,OAAO,KAAK,IAAK,OAAO,OAAO,MAAM;AAAA,IAC9C;AACA,UAAM,CAAC,OAAO,QAAQ,IAAI;AAC1B,UAAM,UAAU,SAAS,MAAM,GAAG,MAAM;AACxC,WAAO,OAAO,QAAQ,QAAQ,OAAO,OAAO,MAAM,GAAG,GAAG,CAAC;AAAA,EAC3D;AACA,SAAO,QAAS,OAAO,OAAO,MAAM;AACtC;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fromFixedPoint.d.ts","sourceRoot":"","sources":["../../src/fromFixedPoint.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,EAAE,eAAW,KAAG,MAG3D,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toDecimalPrecision.d.ts","sourceRoot":"","sources":["../../src/toDecimalPrecision.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,EAAE,QAAQ,MAAM,WAO/D,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toFixedPoint.d.ts","sourceRoot":"","sources":["../../src/toFixedPoint.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,GAAG,MAAM,EAAE,eAAW,KAAG,MAelE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/decimal-precision",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"decimal",
|
|
@@ -29,17 +29,18 @@
|
|
|
29
29
|
"type": "module",
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
|
-
"types": "./dist/
|
|
32
|
+
"types": "./dist/types/index.d.ts",
|
|
33
33
|
"default": "./dist/neutral/index.mjs"
|
|
34
34
|
},
|
|
35
35
|
"./package.json": "./package.json"
|
|
36
36
|
},
|
|
37
37
|
"module": "./dist/neutral/index.mjs",
|
|
38
|
-
"types": "./dist/
|
|
38
|
+
"types": "./dist/types/index.d.ts",
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@xylabs/ts-scripts-yarn3": "^
|
|
41
|
-
"@xylabs/tsconfig": "^
|
|
42
|
-
"typescript": "^5.8.2"
|
|
40
|
+
"@xylabs/ts-scripts-yarn3": "^6.0.5",
|
|
41
|
+
"@xylabs/tsconfig": "^6.0.5",
|
|
42
|
+
"typescript": "^5.8.2",
|
|
43
|
+
"vitest": "^3.0.8"
|
|
43
44
|
},
|
|
44
45
|
"engines": {
|
|
45
46
|
"node": ">=18"
|
package/src/index.ts
CHANGED
|
@@ -1,30 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
while (Number.parseFloat(result.toFixed(fixed)) !== result && fixed < 20) {
|
|
5
|
-
fixed++
|
|
6
|
-
}
|
|
7
|
-
return result.toFixed(fixed)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const toFixedPoint = (value: bigint | string, places = 18): bigint => {
|
|
11
|
-
if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)
|
|
12
|
-
if (typeof value === 'string') {
|
|
13
|
-
const parts = value.split('.')
|
|
14
|
-
if (parts.length > 2) {
|
|
15
|
-
throw new Error('Too many decimals in value')
|
|
16
|
-
}
|
|
17
|
-
if (parts.length === 1) {
|
|
18
|
-
return BigInt(value) * (10n ** BigInt(places))
|
|
19
|
-
}
|
|
20
|
-
const [whole, fraction] = parts
|
|
21
|
-
const trimmed = fraction.slice(0, places)
|
|
22
|
-
return BigInt(whole + trimmed.padEnd(Number(places), '0'))
|
|
23
|
-
}
|
|
24
|
-
return value * (10n ** BigInt(places))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const fromFixedPoint = (value: bigint, places = 18): bigint => {
|
|
28
|
-
if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)
|
|
29
|
-
return value / (10n ** BigInt(places))
|
|
30
|
-
}
|
|
1
|
+
export * from './fromFixedPoint.ts'
|
|
2
|
+
export * from './toDecimalPrecision.ts'
|
|
3
|
+
export * from './toFixedPoint.ts'
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const toDecimalPrecision = (value: number, digits: number) => {
|
|
2
|
+
let fixed = 0
|
|
3
|
+
const result = Number.parseFloat(value.toPrecision(digits))
|
|
4
|
+
while (Number.parseFloat(result.toFixed(fixed)) !== result && fixed < 20) {
|
|
5
|
+
fixed++
|
|
6
|
+
}
|
|
7
|
+
return result.toFixed(fixed)
|
|
8
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const toFixedPoint = (value: bigint | string, places = 18): bigint => {
|
|
2
|
+
if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)
|
|
3
|
+
if (typeof value === 'string') {
|
|
4
|
+
const parts = value.split('.')
|
|
5
|
+
if (parts.length > 2) {
|
|
6
|
+
throw new Error('Too many decimals in value')
|
|
7
|
+
}
|
|
8
|
+
if (parts.length === 1) {
|
|
9
|
+
return BigInt(value) * (10n ** BigInt(places))
|
|
10
|
+
}
|
|
11
|
+
const [whole, fraction] = parts
|
|
12
|
+
const trimmed = fraction.slice(0, places)
|
|
13
|
+
return BigInt(whole + trimmed.padEnd(Number(places), '0'))
|
|
14
|
+
}
|
|
15
|
+
return value * (10n ** BigInt(places))
|
|
16
|
+
}
|
package/dist/neutral/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export declare const toDecimalPrecision: (value: number, digits: number) => string;
|
|
2
|
-
export declare const toFixedPoint: (value: bigint | string, places?: number) => bigint;
|
|
3
|
-
export declare const fromFixedPoint: (value: bigint, places?: number) => bigint;
|
|
4
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,EAAE,QAAQ,MAAM,WAO/D,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,GAAG,MAAM,EAAE,eAAW,KAAG,MAelE,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,EAAE,eAAW,KAAG,MAG3D,CAAA"}
|