From: Lady Date: Wed, 22 Nov 2023 02:27:10 +0000 (-0500) Subject: Move toFunctionName into value.js X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/37c295ffa9d5a062fe15d78427e2a7096a416bb3?ds=inline Move toFunctionName into value.js This is a value coercion function akin to `toLength` or `toIndex`. --- diff --git a/function.js b/function.js index d13be72..d341619 100644 --- a/function.js +++ b/function.js @@ -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 . -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; - }, }; })(); diff --git a/function.test.js b/function.test.js index 9be4db6..439f9d3 100644 --- a/function.test.js +++ b/function.test.js @@ -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", - ); - }); - }); -}); diff --git a/object.js b/object.js index ee3d51d..87040ac 100644 --- 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 . -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, diff --git a/value.js b/value.js index 83c1e18..6471ea3 100644 --- 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 ( diff --git a/value.test.js b/value.test.js index 7d6a5e1..9121af4 100644 --- a/value.test.js +++ b/value.test.js @@ -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);