]> Lady’s Gitweb - Pisces/commitdiff
Add symbolToString and symbolValue to symbol.js
authorLady <redacted>
Mon, 4 Sep 2023 21:34:27 +0000 (17:34 -0400)
committerLady <redacted>
Mon, 4 Sep 2023 21:35:46 +0000 (17:35 -0400)
symbol.js
symbol.test.js

index 343e4096e1c681fd52768795c9a25277467fbfc5..a96cb812636aca31952302955166c036fee18077 100644 (file)
--- a/symbol.js
+++ b/symbol.js
@@ -15,8 +15,38 @@ import { getOwnPropertyDescriptor } from "./object.js";
  *
  * ※ This is effectively an alias for the `Symbol::description`
  * getter.
+ *
+ * ☡ This function throws if the provided argument is not a symbol.
  */
 export const getSymbolDescription = createCallableFunction(
   getOwnPropertyDescriptor(Symbol.prototype, "description").get,
   "getSymbolDescription",
 );
+
+/**
+ * Returns a string representation of the provided symbol.
+ *
+ * ※ Use `getSymbolDescription` instead if you just want the text
+ * description of a symbol.
+ *
+ * ※ This is effectively an alias for the `Symbol::toString`.
+ *
+ * ☡ This function throws if the provided argument is not a symbol.
+ */
+export const symbolToString = createCallableFunction(
+  Symbol.prototype.toString,
+  "symbolToString",
+);
+
+/**
+ * Returns the value of the provided symbol.
+ *
+ * ※ This is effectively an alias for the `Symbol::valueOf`.
+ *
+ * ☡ This function throws if the provided argument is not a symbol and
+ * does not have a `[[SymbolData]]` slot.
+ */
+export const symbolValue = createCallableFunction(
+  Symbol.prototype.valueOf,
+  "symbolValue",
+);
index bedcc72218473d5b8606b9fda7e82006dec2174d..38d74259e7b74f989aec5e331d0bee05848a4c30 100644 (file)
@@ -13,7 +13,11 @@ import {
   describe,
   it,
 } from "./dev-deps.js";
-import { getSymbolDescription } from "./symbol.js";
+import {
+  getSymbolDescription,
+  symbolToString,
+  symbolValue,
+} from "./symbol.js";
 
 describe("getSymbolDescription", () => {
   it("[[Call]] returns undefined when the symbol has no description", () => {
@@ -31,10 +35,6 @@ describe("getSymbolDescription", () => {
     );
   });
 
-  it("[[Call]] returns the empty string when the symbol has an empty description", () => {
-    assertStrictEquals(getSymbolDescription(Symbol("")), "");
-  });
-
   it("[[Construct]] throws an error", () => {
     assertThrows(() => new getSymbolDescription(Symbol()));
   });
@@ -48,3 +48,74 @@ describe("getSymbolDescription", () => {
     });
   });
 });
+
+describe("symbolToString", () => {
+  it('[[Call]] returns "Symbol()" when the symbol has no description', () => {
+    assertStrictEquals(symbolToString(Symbol()), "Symbol()");
+  });
+
+  it('[[Call]] returns "Symbol()" when the symbol has an empty description', () => {
+    assertStrictEquals(symbolToString(Symbol("")), "Symbol()");
+  });
+
+  it('[[Call]] returns "Symbol()" wrapping the description', () => {
+    assertStrictEquals(
+      symbolToString(Symbol("etaoin")),
+      "Symbol(etaoin)",
+    );
+  });
+
+  it("[[Construct]] throws an error", () => {
+    assertThrows(() => new symbolToString(Symbol()));
+  });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(
+        symbolToString.name,
+        "symbolToString",
+      );
+    });
+  });
+});
+
+describe("symbolValue", () => {
+  it("[[Call]] returns the value of a symbol", () => {
+    const symbol = Symbol();
+    assertStrictEquals(symbolValue(symbol), symbol);
+  });
+
+  it("[[Call]] returns the value of a symbol wrapper", () => {
+    const symbol = Symbol();
+    assertStrictEquals(symbolValue(new Object(symbol)), symbol);
+  });
+
+  it("[[Call]] returns the value of a symbol subclass", () => {
+    class SymbolExtension extends Symbol {
+      constructor(symbol) {
+        return Object.setPrototypeOf(
+          new Object(symbol),
+          SymbolExtension.prototype,
+        );
+      }
+    }
+    const symbol = Symbol();
+    assertStrictEquals(
+      symbolValue(new SymbolExtension(symbol)),
+      symbol,
+    );
+  });
+
+  it("[[Construct]] throws an error", () => {
+    assertThrows(() => new symbolValue(Symbol()));
+  });
+
+  describe(".name", () => {
+    it("[[Get]] returns the correct name", () => {
+      assertStrictEquals(
+        symbolValue.name,
+        "symbolValue",
+      );
+    });
+  });
+});
This page took 0.033961 seconds and 4 git commands to generate.