]> Lady’s Gitweb - Pisces/commitdiff
toIntegerOrInfinity 🔜 toIntegralNumberOrInfinity
authorLady <redacted>
Tue, 20 Sep 2022 03:34:38 +0000 (20:34 -0700)
committerLady <redacted>
Fri, 12 May 2023 03:56:48 +0000 (20:56 -0700)
This new name is clearer, especially as this function now accepts
big·int arguments.

numeric.js
numeric.test.js

index 64d9a80502f5fc8b363d59acb5aa18d9e5e988d7..f16308d483ffc41075cdf1edac780889db1559e6 100644 (file)
@@ -646,13 +646,28 @@ export const {
   };
 })();
 
+/**
+ * Returns the result of converting the provided number to an integral
+ * number.
+ *
+ * ※ This function will never return negative zero.
+ */
+export const toIntegralNumber = ($) => {
+  const n = toIntegralNumberOrInfinity($);
+  return !isFiniteNumber(n) || n == 0 ? 0 : n;
+};
+
 /**
  * Returns the result of converting the provided number to an integer
  * or infinity.
  *
+ * ※ Unlike the ToIntegerOrInfinity function defined in the Ecmascript
+ * specification, this function is safe to use with big·ints. However,
+ * the result will always be a number.
+ *
  * ※ This function will never return negative zero.
  */
-export const toIntegerOrInfinity = ($) => {
+export const toIntegralNumberOrInfinity = ($) => {
   const integer = trunc(toNumber($));
   if (isNan(integer) || integer == 0) {
     // The provided value truncs to nan or (positive or negative) zero.
@@ -669,17 +684,6 @@ export const toIntegerOrInfinity = ($) => {
   }
 };
 
-/**
- * Returns the result of converting the provided number to an integral
- * number.
- *
- * ※ This function will never return negative zero.
- */
-export const toIntegralNumber = ($) => {
-  const n = toIntegerOrInfinity($);
-  return !isFiniteNumber(n) || n == 0 ? 0 : n;
-};
-
 /**
  * Returns the result of converting the provided value to a number.
  *
index dfb806301bbf7a3587cce3bc98b308664787b055..23dbd892af8fff7ad01ea4c4c6bde141188e0155 100644 (file)
@@ -24,8 +24,8 @@ import {
   sgn,
   toBigInt,
   toFloat32,
-  toIntegerOrInfinity,
   toIntegralNumber,
+  toIntegralNumberOrInfinity,
   toIntN,
   toNumber,
   toNumeric,
@@ -172,55 +172,58 @@ describe("toIntN", () => {
   });
 });
 
-describe("toIntegerOrInfinity", () => {
+describe("toIntegralNumber", () => {
   it("[[Call]] converts nan to zero", () => {
-    assertStrictEquals(toIntegerOrInfinity(NaN), 0);
+    assertStrictEquals(toIntegralNumber(NaN), 0);
   });
 
   it("[[Call]] converts negative zero to positive zero", () => {
-    assertStrictEquals(toIntegerOrInfinity(-0), 0);
+    assertStrictEquals(toIntegralNumber(-0), 0);
   });
 
   it("[[Call]] drops the fractional part of negative numbers", () => {
-    assertStrictEquals(toIntegerOrInfinity(-1.79), -1);
+    assertStrictEquals(toIntegralNumber(-1.79), -1);
   });
 
-  it("[[Call]] returns infinity for infinity", () => {
-    assertStrictEquals(toIntegerOrInfinity(Infinity), Infinity);
+  it("[[Call]] returns zero for infinity", () => {
+    assertStrictEquals(toIntegralNumber(Infinity), 0);
   });
 
-  it("[[Call]] returns negative infinity for negative infinity", () => {
-    assertStrictEquals(toIntegerOrInfinity(-Infinity), -Infinity);
+  it("[[Call]] returns zero for negative infinity", () => {
+    assertStrictEquals(toIntegralNumber(-Infinity), 0);
   });
 
   it("[[Call]] works with big·ints", () => {
-    assertStrictEquals(toIntegerOrInfinity(2n), 2);
+    assertStrictEquals(toIntegralNumber(2n), 2);
   });
 });
 
-describe("toIntegralNumber", () => {
+describe("toIntegralNumberOrInfinity", () => {
   it("[[Call]] converts nan to zero", () => {
-    assertStrictEquals(toIntegralNumber(NaN), 0);
+    assertStrictEquals(toIntegralNumberOrInfinity(NaN), 0);
   });
 
   it("[[Call]] converts negative zero to positive zero", () => {
-    assertStrictEquals(toIntegralNumber(-0), 0);
+    assertStrictEquals(toIntegralNumberOrInfinity(-0), 0);
   });
 
   it("[[Call]] drops the fractional part of negative numbers", () => {
-    assertStrictEquals(toIntegralNumber(-1.79), -1);
+    assertStrictEquals(toIntegralNumberOrInfinity(-1.79), -1);
   });
 
-  it("[[Call]] returns zero for infinity", () => {
-    assertStrictEquals(toIntegralNumber(Infinity), 0);
+  it("[[Call]] returns infinity for infinity", () => {
+    assertStrictEquals(toIntegralNumberOrInfinity(Infinity), Infinity);
   });
 
-  it("[[Call]] returns zero for negative infinity", () => {
-    assertStrictEquals(toIntegralNumber(-Infinity), 0);
+  it("[[Call]] returns negative infinity for negative infinity", () => {
+    assertStrictEquals(
+      toIntegralNumberOrInfinity(-Infinity),
+      -Infinity,
+    );
   });
 
   it("[[Call]] works with big·ints", () => {
-    assertStrictEquals(toIntegralNumber(2n), 2);
+    assertStrictEquals(toIntegralNumberOrInfinity(2n), 2);
   });
 });
 
This page took 0.027224 seconds and 4 git commands to generate.