]>
Lady’s Gitweb - Pisces/blob - value.js
1 // ♓🌟 Piscēs ∷ value.js
2 // ====================================================================
4 // Copyright © 2022‐2023 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/>.
11 /** The welknown `@@asyncIterator` symbol. */
12 asyncIterator
: ASYNC_ITERATOR
,
14 /** The welknown `@@hasInstance` symbol. */
15 hasInstance
: HAS_INSTANCE
,
17 /** The welknown `@@isConcatSpreadable` symbol. */
18 isConcatSpreadable
: IS_CONCAT_SPREADABLE
,
20 /** The welknown `@@iterator` symbol. */
23 /** The welknown `@@match` symbol. */
26 /** The welknown `@@matchAll` symbol. */
29 /** The welknown `@@replace` symbol. */
32 /** The welknown `@@species` symbol. */
35 /** The welknown `@@split` symbol. */
38 /** The welknown `@@toPrimitive` symbol. */
39 toPrimitive
: TO_PRIMITIVE
,
41 /** The welknown `@@toStringTag` symbol. */
42 toStringTag
: TO_STRING_TAG
,
44 /** The welknown `@@unscopables` symbol. */
45 unscopables
: UNSCOPABLES
,
48 /** The null primitive. */
49 export const NULL
= null;
51 /** The undefined primitive. */
52 export const UNDEFINED
= undefined;
56 * Returns the primitive value of the provided object per its
57 * `.toString` and `.valueOf` methods.
59 * If the provided hint is "string", then `.toString` takes
60 * precedence; otherwise, `.valueOf` does.
62 * Throws an error if both of these methods are not callable or do
63 * not return a primitive.
68 * Returns the provided value converted to a primitive, or throws if
69 * no such conversion is possible.
71 * The provided preferred type, if specified, should be "string",
72 * "number", or "default". If the provided input has a
73 * `.[Symbol.toPrimitive]` method, this function will throw rather
74 * than calling that method with a preferred type other than one of
79 const { apply
: call
} = Reflect
;
82 ordinaryToPrimitive
: (O
, hint
) => {
83 const methodNames
= hint
== "string"
84 ? ["toString", "valueOf"]
85 : ["valueOf", "toString"];
86 for (let index
= 0; index
< methodNames
.length
; ++index
) {
87 const method
= O
[methodNames
[index
]];
88 if (typeof method
=== "function") {
89 // Method is callable.
90 const result
= call(method
, O
, []);
91 if (type(result
) !== "object") {
92 // Method returns a primitive.
95 // Method returns an object.
99 // Method is not callable.
104 "Piscēs: Unable to convert object to primitive",
107 toPrimitive
: ($, preferredType
= "default") => {
108 const hint
= `${preferredType}`;
110 "default" !== hint
&& "string" !== hint
&&
113 // An invalid preferred type was specified.
115 `Piscēs: Invalid preferred type: ${preferredType}.`,
117 } else if (type($) === "object") {
118 // The provided value is an object.
119 const exoticToPrim
= $[TO_PRIMITIVE
] ?? undefined;
120 if (exoticToPrim
!== undefined) {
121 // The provided value has an exotic primitive conversion
123 if (typeof exoticToPrim
!== "function") {
124 // The method is not callable.
126 "Piscēs: `.[Symbol.toPrimitive]` was neither nullish nor callable.",
129 // The method is callable.
130 return call(exoticToPrim
, $, [hint
]);
133 // Use the ordinary primitive conversion function.
134 return ordinaryToPrimitive($, hint
);
137 // The provided value is already a primitive.
145 * Returns whether the provided values are the same value.
147 * ※ This differs from `===` in the cases of nan and zero.
149 export const sameValue
= Object
.is
;
153 * Returns whether the provided values are either the same value or
154 * both zero (either positive or negative).
156 * ※ This differs from `===` in the case of nan.
160 const { isNaN
: isNan
} = Number
;
162 sameValueZero
: ($1, $2) => {
163 const type1
= type($1);
164 const type2
= type($2);
165 if (type1
!== type2
) {
166 // The provided values are not of the same type.
168 } else if (type1
=== "number") {
169 // The provided values are numbers; check if they are nan and
170 // use strict equality otherwise.
171 return isNan($1) && isNan($2) || $1 === $2;
173 // The provided values are not numbers; use strict equality.
181 * Returns a lowercase string identifying the type of the provided
184 * This differs from the value of the `typeof` operator only in the
185 * cases of objects and null.
187 export const type
= ($) => {
189 // The provided value is null.
192 // The provided value is not null.
193 const type
·of = typeof $;
194 return type
·of === "function" ? "object" : type
·of;
This page took 0.060349 seconds and 5 git commands to generate.