This is a value coercion function akin to `toLength` or `toIndex`.
// 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 {
/**
* 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.
create: objectCreate,
defineProperty: defineOwnProperty,
defineProperties: defineOwnProperties,
- getOwnPropertyDescriptor,
getPrototypeOf: getPrototype,
setPrototypeOf: setPrototype,
} = Object;
};
},
};
- const getSymbolDescription = getOwnPropertyDescriptor(
- Symbol.prototype,
- "description",
- ).get;
const applyBaseFunction = ($, base, lengthDelta = 0) => {
if (base === undefined) {
// No base function was provided to apply.
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;
- },
};
})();
isCallable,
isConstructor,
ordinaryHasInstance,
- toFunctionName,
} from "./function.js";
describe("bind", () => {
});
});
});
-
-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",
- );
- });
- });
-});
// 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,
*/
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.
toPrimitive,
} = (() => {
const { apply: call } = Reflect;
+ const getSymbolDescription = Object.getOwnPropertyDescriptor(
+ Symbol.prototype,
+ "description",
+ ).get;
return {
ordinaryToPrimitive: (O, hint) => {
"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 (
SQRT2,
TO_PRIMITIVE,
TO_STRING_TAG,
+ toFunctionName,
toIndex,
toLength,
toPrimitive,
});
});
+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);