]>
Lady’s Gitweb - Pisces/blob - object.js
d08f43a00283853679c32b8dcff76b77350fad54
1 // ♓🌟 Piscēs ∷ object.js
2 // ====================================================================
4 // Copyright © 2022 Lady [@ Lady’s Computer].
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
10 /** Returns whether the provided value is a constructor. */
11 export const isConstructor
= ($) => {
13 // The provided value is not an object.
16 // The provided value is an object.
22 ); // will throw if $ is not a constructor
30 /** Returns whether the provided value is an object. */
31 export const isObject
= ($) => {
33 (typeof $ == "function" || typeof $ == "object");
37 * Returns whether the provided object inherits from the prototype of
38 * the provided function.
40 export const ordinaryHasInstance
= Function
.prototype.call
.bind(
41 Function
.prototype[Symbol
.hasInstance
],
45 * Returns the primitive value of the provided object per its
46 * `toString` and `valueOf` methods.
48 * If the provided hint is "string", then `toString` takes precedence;
49 * otherwise, `valueOf` does.
51 * Throws an error if both of these methods are not callable or do not
54 export const ordinaryToPrimitive
= (O
, hint
) => {
56 const name
of hint
== "string"
57 ? ["toString", "valueOf"]
58 : ["valueOf", "toString"]
60 const method
= O
[name
];
61 if (typeof method
== "function") {
62 // Method is callable.
63 const result
= method
.call(O
);
64 if (!isObject(result
)) {
65 // Method returns a primitive.
68 // Method returns an object.
72 // Method is not callable.
76 throw new TypeError("Piscēs: Unable to convert object to primitive");
80 * Returns the provided value converted to a primitive, or throws if
81 * no such conversion is possible.
83 * The provided preferred type, if specified, should be "string",
84 * "number", or "default". If the provided input has a
85 * `Symbol.toPrimitive` method, this function will throw rather than
86 * calling that method with a preferred type other than one of the
89 export const toPrimitive
= ($, preferredType
) => {
91 // The provided value is an object.
92 const exoticToPrim
= $[Symbol
.toPrimitive
] ?? undefined;
93 if (exoticToPrim
!== undefined) {
94 // The provided value has an exotic primitive conversion method.
95 if (typeof exoticToPrim
!= "function") {
96 // The method is not callable.
98 "Piscēs: Symbol.toPrimitive was neither nullish nor callable.",
101 // The method is callable.
102 const hint
= `${preferredType ?? "default"}`;
103 if (!["default", "string", "number"].includes(hint
)) {
104 // An invalid preferred type was specified.
106 `Piscēs: Invalid preferred type: ${preferredType}.`,
109 // The resulting hint is either default, string, or number.
110 return exoticToPrim
.call($, hint
);
114 // Use the ordinary primitive conversion function.
115 ordinaryToPrimitive($, hint
);
118 // The provided value is already a primitive.
124 * Returns the property key (symbol or string) corresponding to the
127 export const toPropertyKey
= ($) => {
128 const key
= toPrimitive($, "string");
129 return typeof key
== "symbol" ? key
: `${key}`;
This page took 0.084168 seconds and 3 git commands to generate.