]> Lady’s Gitweb - Pisces/commitdiff
Add toFunctionName to function.js
authorLady <redacted>
Mon, 4 Sep 2023 22:25:40 +0000 (18:25 -0400)
committerLady <redacted>
Mon, 4 Sep 2023 22:25:40 +0000 (18:25 -0400)
function.js
function.test.js

index cc81fb661534a96e53f5b67e420d516d2498ce8a..d5888eb854be9cffff96e0ed4de89f7d3f6607be 100644 (file)
@@ -199,3 +199,33 @@ export const ordinaryHasInstance = createCallableFunction(
   Function.prototype[Symbol.hasInstance],
   "ordinaryHasInstance",
 );
+
+export const {
+  /**
+   * Returns a string function name generated from the provided value
+   * and optional prefix.
+   */
+  toFunctionName,
+} = (() => {
+  const getSymbolDescription = Object.getOwnPropertyDescriptor(
+    Symbol.prototype,
+    "description",
+  ).get;
+
+  return {
+    toFunctionName: ($, prefix = undefined) => {
+      const name = (() => {
+        if (typeof $ === "symbol") {
+          // The provided value is a symbol; format its description.
+          const description = call(getSymbolDescription, $, []);
+          return description === undefined ? "" : `[${description}]`;
+        } else {
+          // The provided value not a symbol; convert it to a string
+          // property key.
+          return `${$}`;
+        }
+      })();
+      return prefix !== undefined ? `${prefix} ${name}` : name;
+    },
+  };
+})();
index 9711f792d4d4caf063f019be41388f3ab2908994..75ba30008df964ac424e37518f40960576abcf1a 100644 (file)
@@ -26,6 +26,7 @@ import {
   isCallable,
   isConstructor,
   ordinaryHasInstance,
+  toFunctionName,
 } from "./function.js";
 
 describe("bind", () => {
@@ -567,3 +568,75 @@ 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(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(
+        toFunctionName.name,
+        "toFunctionName",
+      );
+    });
+  });
+});
This page took 0.026093 seconds and 4 git commands to generate.