+
+export const {
+ /**
+ * Returns the result of converting the provided value to fit within
+ * the provided number of bits as a signed integer.
+ *
+ * ※ Unlike BigInt.asIntN, this function accepts both big·int and
+ * number values.
+ *
+ * ☡ The first argument, the number of bits, must be a number.
+ */
+ toSignedIntegralNumeric,
+
+ /**
+ * Returns the result of converting the provided value to fit within
+ * the provided number of bits as an unsigned integer.
+ *
+ * ※ Unlike BigInt.asUintN, this function accepts both big·int and
+ * number values.
+ *
+ * ☡ The first argument, the number of bits, must be a number.
+ */
+ toUnsignedIntegralNumeric,
+} = (() => {
+ const { asIntN, asUintN } = BigInt;
+ return {
+ toSignedIntegralNumeric: (n, $) => {
+ const prim = toPrimitive($);
+ if (typeof prim === "bigint") {
+ // The primitive value is a big·int.
+ return asIntN(n, prim);
+ } else {
+ // The primitive value is not a big·int.
+ const int = trunc(prim);
+ if (!isFiniteNumber(int) || int == 0) {
+ // The truncated value is zero or not finite.
+ return 0;
+ } else {
+ // The truncated value is finite.
+ return toNumber(asIntN(n, toBigInt(int)));
+ }
+ }
+ },
+ toUnsignedIntegralNumeric: (n, $) => {
+ const prim = toPrimitive($);
+ if (typeof prim === "bigint") {
+ // The primitive value is a big·int.
+ return asUintN(n, prim);
+ } else {
+ // The primitive value is not a big·int.
+ const int = trunc(prim);
+ if (!isFiniteNumber(int) || int == 0) {
+ // The truncated value is zero or not finite.
+ return 0;
+ } else {
+ // The truncated value is finite.
+ return toNumber(asUintN(n, toBigInt(int)));
+ }
+ }
+ },
+ };
+})();