summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
inline | side by side (from parent 1:
c136497)
These used to not handle non‐integers well, and now do gracefully.
return {
toIntN: (n, $) => {
const prim = toPrimitive($);
return {
toIntN: (n, $) => {
const prim = toPrimitive($);
- const big·int = toBigInt(prim);
- const intN = asIntN(n, big·int);
- return typeof prim === "bigint" ? intN : toNumber(intN);
+ 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)));
+ }
+ }
},
toUintN: (n, $) => {
const prim = toPrimitive($);
},
toUintN: (n, $) => {
const prim = toPrimitive($);
- const big·int = toBigInt(prim);
- const uintN = asUintN(n, big·int);
- return typeof prim === "bigint" ? uintN : toNumber(uintN);
+ 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)));
+ }
+ }
it("[[Call]] works with numbers", () => {
assertStrictEquals(toIntN(2, 7), -1);
});
it("[[Call]] works with numbers", () => {
assertStrictEquals(toIntN(2, 7), -1);
});
+
+ it("[[Call]] works with non‐integers", () => {
+ assertStrictEquals(toIntN(2, 7.21), -1);
+ assertStrictEquals(toIntN(2, Infinity), 0);
+ });
});
describe("toIntegerOrInfinity", () => {
});
describe("toIntegerOrInfinity", () => {
it("[[Call]] works with numbers", () => {
assertStrictEquals(toUintN(2, 7), 3);
});
it("[[Call]] works with numbers", () => {
assertStrictEquals(toUintN(2, 7), 3);
});
+
+ it("[[Call]] works with non‐integers", () => {
+ assertStrictEquals(toUintN(2, 7.21), 3);
+ assertStrictEquals(toUintN(2, Infinity), 0);
+ });