pema 0.14.1 → 0.14.2

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,10 +1,27 @@
1
1
  import is from "@rcompat/is";
2
2
  export default function coerce_bigint(x) {
3
- // normalize from string to number; other types unaffected
4
- const n = is.numeric(x) ? Number(x) : x;
5
- // normalize from number to bigint; other types unaffected
6
- if (is.int(n))
7
- return BigInt(n);
3
+ if (is.numeric(x)) {
4
+ // Retain non-finite rejection and bound exponent expansion. This number
5
+ // is only an eligibility check, never the source of the bigint value.
6
+ if (!is.finite(Number(x)))
7
+ return x;
8
+ const [coefficient, exponent = "0"] = x.trim().toLowerCase().split("e");
9
+ const [whole, fraction = ""] = coefficient.split(".");
10
+ const digits = whole + fraction;
11
+ if (/^[+-]?0+$/.test(digits))
12
+ return 0n;
13
+ const shift = Number(exponent) - fraction.length;
14
+ if (shift >= 0)
15
+ return BigInt(digits) * 10n ** BigInt(shift);
16
+ // A decimal shift may discard only zeroes, never a fractional value.
17
+ const end = digits.length + shift;
18
+ if (end <= 0 || /[1-9]/.test(digits.slice(end)))
19
+ return x;
20
+ return BigInt(digits.slice(0, end));
21
+ }
22
+ // Number inputs have already acquired JavaScript's precision semantics.
23
+ if (is.int(x))
24
+ return BigInt(x);
8
25
  // bigint or invalid
9
26
  return x;
10
27
  }
package/package.json CHANGED
@@ -48,5 +48,5 @@
48
48
  "test": "proby"
49
49
  },
50
50
  "type": "module",
51
- "version": "0.14.1"
51
+ "version": "0.14.2"
52
52
  }