]> Lady’s Gitweb - Pisces/commitdiff
Move toFunctionName into value.js
authorLady <redacted>
Wed, 22 Nov 2023 02:27:10 +0000 (21:27 -0500)
committerLady <redacted>
Thu, 23 Nov 2023 02:03:34 +0000 (21:03 -0500)
This is a value coercion function akin to `toLength` or `toIndex`.

function.js
function.test.js
object.js
value.js
value.test.js

index d13be725527efb910f3fa59102630c6941c81052..d3416192089169bb369caa7a2e9a20191accfc81 100644 (file)
@@ -7,7 +7,7 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
 
-import { ITERATOR, toLength, toPrimitive } from "./value.js";
+import { ITERATOR, toFunctionName, toLength } from "./value.js";
 
 export const {
   /**
@@ -52,12 +52,6 @@ export const {
    * be used to override `length`, `name`, and `prototype`.
    */
   createIllegalConstructor,
-
-  /**
-   * Returns a string function name generated from the provided value
-   * and optional prefix.
-   */
-  toFunctionName,
 } = (() => {
   // ☡ Because these functions are used to initialize module constants,
   // they can’t depend on imports from elsewhere.
@@ -72,7 +66,6 @@ export const {
     create: objectCreate,
     defineProperty: defineOwnProperty,
     defineProperties: defineOwnProperties,
-    getOwnPropertyDescriptor,
     getPrototypeOf: getPrototype,
     setPrototypeOf: setPrototype,
   } = Object;
@@ -93,10 +86,6 @@ export const {
       };
     },
   };
-  const getSymbolDescription = getOwnPropertyDescriptor(
-    Symbol.prototype,
-    "description",
-  ).get;
   const applyBaseFunction = ($, base, lengthDelta = 0) => {
     if (base === undefined) {
       // No base function was provided to apply.
@@ -199,21 +188,6 @@ export const {
         writable: false,
       });
     },
-    toFunctionName: ($, prefix = undefined) => {
-      const key = toPrimitive($, "string");
-      const name = (() => {
-        if (typeof key === "symbol") {
-          // The provided value is a symbol; format its description.
-          const description = call(getSymbolDescription, key, []);
-          return description === undefined ? "" : `[${description}]`;
-        } else {
-          // The provided value not a symbol; convert it to a string
-          // property key.
-          return `${key}`;
-        }
-      })();
-      return prefix !== undefined ? `${prefix} ${name}` : name;
-    },
   };
 })();
 
index 9be4db682418619a53820f35a3da2657ad1e4773..439f9d38c2ad771d0529415c6f5f29d8735f35d3 100644 (file)
@@ -27,7 +27,6 @@ import {
   isCallable,
   isConstructor,
   ordinaryHasInstance,
-  toFunctionName,
 } from "./function.js";
 
 describe("bind", () => {
@@ -753,81 +752,3 @@ describe("ordinaryHasInstance", () => {
     });
   });
 });
-
-describe("toFunctionName", () => {
-  it("[[Call]] works with strings and no prefix", () => {
-    assertStrictEquals(toFunctionName("etaoin"), "etaoin");
-  });
-
-  it("[[Call]] works with objects and no prefix", () => {
-    assertStrictEquals(
-      toFunctionName({
-        toString() {
-          return "etaoin";
-        },
-      }),
-      "etaoin",
-    );
-  });
-
-  it("[[Call]] works with descriptionless symbols and no prefix", () => {
-    assertStrictEquals(toFunctionName(Symbol()), "");
-  });
-
-  it("[[Call]] works with empty description symbols and no prefix", () => {
-    assertStrictEquals(toFunctionName(Symbol("")), "[]");
-  });
-
-  it("[[Call]] works with described symbols and no prefix", () => {
-    assertStrictEquals(toFunctionName(Symbol("etaoin")), "[etaoin]");
-  });
-
-  it("[[Call]] works with strings and a prefix", () => {
-    assertStrictEquals(toFunctionName("etaoin", "foo"), "foo etaoin");
-  });
-
-  it("[[Call]] works with objects and no prefix", () => {
-    assertStrictEquals(
-      toFunctionName({
-        toString() {
-          return "etaoin";
-        },
-      }, "foo"),
-      "foo etaoin",
-    );
-  });
-
-  it("[[Call]] works with descriptionless symbols and no prefix", () => {
-    assertStrictEquals(toFunctionName(Symbol(), "foo"), "foo ");
-  });
-
-  it("[[Call]] works with empty description symbols and no prefix", () => {
-    assertStrictEquals(toFunctionName(Symbol(""), "foo"), "foo []");
-  });
-
-  it("[[Call]] works with described symbols and no prefix", () => {
-    assertStrictEquals(
-      toFunctionName(Symbol("etaoin"), "foo"),
-      "foo [etaoin]",
-    );
-  });
-
-  it("[[Construct]] throws an error", () => {
-    assertThrows(() => new toFunctionName(""));
-  });
-
-  describe(".length", () => {
-    it("[[Get]] returns the correct length", () => {
-      assertStrictEquals(toFunctionName.length, 1);
-    });
-  });
-
-  describe(".name", () => {
-    it("[[Get]] returns the correct name", () => {
-      assertStrictEquals(
-        toFunctionName.name,
-        "toFunctionName",
-      );
-    });
-  });
-});
index ee3d51da336d6f1c5a09123cd41b8ec921550f80..87040ac26f927bfe29aec5d5de6b750589af1b41 100644 (file)
--- a/object.js
+++ b/object.js
@@ -7,16 +7,12 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
 
-import {
-  bind,
-  call,
-  createArrowFunction,
-  toFunctionName,
-} from "./function.js";
+import { bind, call, createArrowFunction } from "./function.js";
 import {
   IS_CONCAT_SPREADABLE,
   ITERATOR,
   SPECIES,
+  toFunctionName,
   toLength,
   toPrimitive,
   type,
index 83c1e1803d78d2c35f232faf96bad5aa7a29c931..6471ea3142f66f2a40ac899415a51e9d7391b5bd 100644 (file)
--- a/value.js
+++ b/value.js
@@ -186,6 +186,12 @@ export const {
    */
   ordinaryToPrimitive,
 
+  /**
+   * Returns a string function name generated from the provided value
+   * and optional prefix.
+   */
+  toFunctionName,
+
   /**
    * Returns the provided value converted to a primitive, or throws if
    * no such conversion is possible.
@@ -199,6 +205,10 @@ export const {
   toPrimitive,
 } = (() => {
   const { apply: call } = Reflect;
+  const getSymbolDescription = Object.getOwnPropertyDescriptor(
+    Symbol.prototype,
+    "description",
+  ).get;
 
   return {
     ordinaryToPrimitive: (O, hint) => {
@@ -226,6 +236,21 @@ export const {
         "Piscēs: Unable to convert object to primitive",
       );
     },
+    toFunctionName: ($, prefix = undefined) => {
+      const key = toPrimitive($, "string");
+      const name = (() => {
+        if (typeof key === "symbol") {
+          // The provided value is a symbol; format its description.
+          const description = call(getSymbolDescription, key, []);
+          return description === undefined ? "" : `[${description}]`;
+        } else {
+          // The provided value not a symbol; convert it to a string
+          // property key.
+          return `${key}`;
+        }
+      })();
+      return prefix !== undefined ? `${prefix} ${name}` : name;
+    },
     toPrimitive: ($, preferredType = "default") => {
       const hint = `${preferredType}`;
       if (
index 7d6a5e13e6e2c3d7c1ffc1c17e6c2abd6a7727a7..9121af479c46f3e2a190ec64742bf82ffd31ae34 100644 (file)
@@ -44,6 +44,7 @@ import {
   SQRT2,
   TO_PRIMITIVE,
   TO_STRING_TAG,
+  toFunctionName,
   toIndex,
   toLength,
   toPrimitive,
@@ -469,6 +470,84 @@ describe("sameValueZero", () => {
   });
 });
 
+describe("toFunctionName", () => {
+  it("[[Call]] works with strings and no prefix", () => {
+    assertStrictEquals(toFunctionName("etaoin"), "etaoin");
+  });
+
+  it("[[Call]] works with objects and no prefix", () => {
+    assertStrictEquals(
+      toFunctionName({
+        toString() {
+          return "etaoin";
+        },
+      }),
+      "etaoin",
+    );
+  });
+
+  it("[[Call]] works with descriptionless symbols and no prefix", () => {
+    assertStrictEquals(toFunctionName(Symbol()), "");
+  });
+
+  it("[[Call]] works with empty description symbols and no prefix", () => {
+    assertStrictEquals(toFunctionName(Symbol("")), "[]");
+  });
+
+  it("[[Call]] works with described symbols and no prefix", () => {
+    assertStrictEquals(toFunctionName(Symbol("etaoin")), "[etaoin]");
+  });
+
+  it("[[Call]] works with strings and a prefix", () => {
+    assertStrictEquals(toFunctionName("etaoin", "foo"), "foo etaoin");
+  });
+
+  it("[[Call]] works with objects and no prefix", () => {
+    assertStrictEquals(
+      toFunctionName({
+        toString() {
+          return "etaoin";
+        },
+      }, "foo"),
+      "foo etaoin",
+    );
+  });
+
+  it("[[Call]] works with descriptionless symbols and no prefix", () => {
+    assertStrictEquals(toFunctionName(Symbol(), "foo"), "foo ");
+  });
+
+  it("[[Call]] works with empty description symbols and no prefix", () => {
+    assertStrictEquals(toFunctionName(Symbol(""), "foo"), "foo []");
+  });
+
+  it("[[Call]] works with described symbols and no prefix", () => {
+    assertStrictEquals(
+      toFunctionName(Symbol("etaoin"), "foo"),
+      "foo [etaoin]",
+    );
+  });
+
+  it("[[Construct]] throws an error", () => {
+    assertThrows(() => new toFunctionName(""));
+  });
+
+  describe(".length", () => {
+    it("[[Get]] returns the correct length", () => {
+      assertStrictEquals(toFunctionName.length, 1);
+    });
+  });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(
+        toFunctionName.name,
+        "toFunctionName",
+      );
+    });
+  });
+});
+
 describe("toIndex", () => {
   it("[[Call]] returns an index", () => {
     assertStrictEquals(toIndex(9007199254740991), 9007199254740991);
This page took 0.033731 seconds and 4 git commands to generate.