From: Lady Date: Mon, 4 Sep 2023 20:57:00 +0000 (-0400) Subject: Create symbol.js module with getSymbolDescription X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/ee0f6916f2f56f17bd232f964fec9a4c7ed6ec89 Create symbol.js module with getSymbolDescription --- diff --git a/mod.js b/mod.js index 41bce27..600f619 100644 --- a/mod.js +++ b/mod.js @@ -15,4 +15,5 @@ export * from "./iri.js"; export * from "./numeric.js"; export * from "./object.js"; export * from "./string.js"; +export * from "./symbol.js"; export * from "./value.js"; diff --git a/symbol.js b/symbol.js new file mode 100644 index 0000000..514b526 --- /dev/null +++ b/symbol.js @@ -0,0 +1,22 @@ +// ♓🌟 Piscēs ∷ symbol.js +// ==================================================================== +// +// Copyright © 2023 Lady [@ Lady’s Computer]. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at . + +import { makeCallable } from "./function.js"; +import { getOwnPropertyDescriptor } from "./object.js"; + +/** + * Returns the description for the provided symbol. + * + * ※ This is effectively an alias for the `Symbol::description` + * getter. + */ +export const getSymbolDescription = makeCallable( + getOwnPropertyDescriptor(Symbol.prototype, "description").get, + "getSymbolDescription", +); diff --git a/symbol.test.js b/symbol.test.js new file mode 100644 index 0000000..bedcc72 --- /dev/null +++ b/symbol.test.js @@ -0,0 +1,50 @@ +// ♓🌟 Piscēs ∷ symbol.test.js +// ==================================================================== +// +// Copyright © 2023 Lady [@ Lady’s Computer]. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at . + +import { + assertStrictEquals, + assertThrows, + describe, + it, +} from "./dev-deps.js"; +import { getSymbolDescription } from "./symbol.js"; + +describe("getSymbolDescription", () => { + it("[[Call]] returns undefined when the symbol has no description", () => { + assertStrictEquals(getSymbolDescription(Symbol()), undefined); + }); + + it("[[Call]] returns the empty string when the symbol has an empty description", () => { + assertStrictEquals(getSymbolDescription(Symbol("")), ""); + }); + + it("[[Call]] returns the description", () => { + assertStrictEquals( + getSymbolDescription(Symbol("etaoin")), + "etaoin", + ); + }); + + 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())); + }); + + describe(".name", () => { + it("[[Get]] returns the correct name", () => { + assertStrictEquals( + getSymbolDescription.name, + "getSymbolDescription", + ); + }); + }); +});