@xylabs/decimal-precision 4.5.4 → 4.5.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.
@@ -1,4 +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
+ export declare const toFixedPoint: (value: bigint | string, places?: number) => bigint;
3
+ export declare const fromFixedPoint: (value: bigint, places?: number) => bigint;
4
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,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"}
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"}
@@ -7,21 +7,26 @@ var toDecimalPrecision = (value, digits) => {
7
7
  }
8
8
  return result.toFixed(fixed);
9
9
  };
10
- var toFixedPoint = (value, places = 18n) => {
10
+ var toFixedPoint = (value, places = 18) => {
11
+ if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`);
11
12
  if (typeof value === "string") {
12
13
  const parts = value.split(".");
13
14
  if (parts.length > 2) {
14
15
  throw new Error("Too many decimals in value");
15
16
  }
16
17
  if (parts.length === 1) {
17
- return BigInt(value) * 10n ** places;
18
+ return BigInt(value) * 10n ** BigInt(places);
18
19
  }
19
20
  const [whole, fraction] = parts;
20
- return BigInt(whole + fraction.padEnd(Number(places), "0"));
21
+ const trimmed = fraction.slice(0, places);
22
+ return BigInt(whole + trimmed.padEnd(Number(places), "0"));
21
23
  }
22
- return value * 10n ** places;
24
+ return value * 10n ** BigInt(places);
25
+ };
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);
23
29
  };
24
- var fromFixedPoint = (value, places = 18n) => value / 10n ** places;
25
30
  export {
26
31
  fromFixedPoint,
27
32
  toDecimalPrecision,
@@ -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\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":[]}
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 = 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\nexport 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"],"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,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;AAEO,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;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/decimal-precision",
3
- "version": "4.5.4",
3
+ "version": "4.5.6",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "decimal",
package/src/index.ts CHANGED
@@ -7,18 +7,24 @@ export const toDecimalPrecision = (value: number, digits: number) => {
7
7
  return result.toFixed(fixed)
8
8
  }
9
9
 
10
- export const toFixedPoint = (value: bigint | string, places = 18n): bigint => {
10
+ export const toFixedPoint = (value: bigint | string, places = 18): bigint => {
11
+ if (!Number.isInteger(places)) throw new Error(`places (${places}) must be an Integer`)
11
12
  if (typeof value === 'string') {
12
13
  const parts = value.split('.')
13
14
  if (parts.length > 2) {
14
15
  throw new Error('Too many decimals in value')
15
16
  }
16
17
  if (parts.length === 1) {
17
- return BigInt(value) * (10n ** places)
18
+ return BigInt(value) * (10n ** BigInt(places))
18
19
  }
19
20
  const [whole, fraction] = parts
20
- return BigInt(whole + fraction.padEnd(Number(places), '0'))
21
+ const trimmed = fraction.slice(0, places)
22
+ return BigInt(whole + trimmed.padEnd(Number(places), '0'))
21
23
  }
22
- return value * (10n ** places)
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))
23
30
  }
24
- export const fromFixedPoint = (value: bigint, places = 18n): bigint => value / (10n ** places)