@xylabs/decimal-precision 4.5.1 → 4.5.3

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.
@@ -1,2 +1,4 @@
1
1
  export declare const toDecimalPrecision: (value: number, digits: number) => string;
2
+ export declare const toFixedPoint: (value: bigint | string, places?: bigint) => bigint;
3
+ export declare const fromFixedPoint: (value: bigint, places?: bigint) => bigint;
2
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,UAAW,MAAM,UAAU,MAAM,WAO/D,CAAA"}
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,eAAY,KAAG,MAanE,CAAA;AACD,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,EAAE,eAAY,KAAG,MAAiC,CAAA"}
@@ -7,7 +7,24 @@ var toDecimalPrecision = (value, digits) => {
7
7
  }
8
8
  return result.toFixed(fixed);
9
9
  };
10
+ var toFixedPoint = (value, places = 18n) => {
11
+ if (typeof value === "string") {
12
+ const parts = value.split(".");
13
+ if (parts.length > 2) {
14
+ throw new Error("Too many decimals in value");
15
+ }
16
+ if (parts.length === 1) {
17
+ return BigInt(value) * 10n ** places;
18
+ }
19
+ const [whole, fraction] = parts;
20
+ return BigInt(whole + fraction.padEnd(Number(places), "0"));
21
+ }
22
+ return value * 10n ** places;
23
+ };
24
+ var fromFixedPoint = (value, places = 18n) => value / 10n ** places;
10
25
  export {
11
- toDecimalPrecision
26
+ fromFixedPoint,
27
+ toDecimalPrecision,
28
+ toFixedPoint
12
29
  };
13
30
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["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"],"mappings":";AAAO,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;","names":[]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["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\nexport const toFixedPoint = (value: bigint | string, places = 18n): bigint => {\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 ** places)\n }\n const [whole, fraction] = parts\n return BigInt(whole + fraction.padEnd(Number(places), '0'))\n }\n return value * (10n ** places)\n}\nexport const fromFixedPoint = (value: bigint, places = 18n): bigint => value / (10n ** places)\n"],"mappings":";AAAO,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;AAEO,IAAM,eAAe,CAAC,OAAwB,SAAS,QAAgB;AAC5E,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;AAAA,IACjC;AACA,UAAM,CAAC,OAAO,QAAQ,IAAI;AAC1B,WAAO,OAAO,QAAQ,SAAS,OAAO,OAAO,MAAM,GAAG,GAAG,CAAC;AAAA,EAC5D;AACA,SAAO,QAAS,OAAO;AACzB;AACO,IAAM,iBAAiB,CAAC,OAAe,SAAS,QAAgB,QAAS,OAAO;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/decimal-precision",
3
- "version": "4.5.1",
3
+ "version": "4.5.3",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "decimal",
@@ -37,9 +37,9 @@
37
37
  "module": "./dist/neutral/index.mjs",
38
38
  "types": "./dist/neutral/index.d.ts",
39
39
  "devDependencies": {
40
- "@xylabs/ts-scripts-yarn3": "^4.2.6",
41
- "@xylabs/tsconfig": "^4.2.6",
42
- "typescript": "^5.7.3"
40
+ "@xylabs/ts-scripts-yarn3": "^5.0.39",
41
+ "@xylabs/tsconfig": "^5.0.39",
42
+ "typescript": "^5.8.2"
43
43
  },
44
44
  "engines": {
45
45
  "node": ">=18"
package/src/index.ts CHANGED
@@ -6,3 +6,19 @@ export const toDecimalPrecision = (value: number, digits: number) => {
6
6
  }
7
7
  return result.toFixed(fixed)
8
8
  }
9
+
10
+ export const toFixedPoint = (value: bigint | string, places = 18n): bigint => {
11
+ if (typeof value === 'string') {
12
+ const parts = value.split('.')
13
+ if (parts.length > 2) {
14
+ throw new Error('Too many decimals in value')
15
+ }
16
+ if (parts.length === 1) {
17
+ return BigInt(value) * (10n ** places)
18
+ }
19
+ const [whole, fraction] = parts
20
+ return BigInt(whole + fraction.padEnd(Number(places), '0'))
21
+ }
22
+ return value * (10n ** places)
23
+ }
24
+ export const fromFixedPoint = (value: bigint, places = 18n): bigint => value / (10n ** places)