From: Lady Date: Thu, 24 Jul 2025 02:41:32 +0000 (-0400) Subject: Minor refactors & adjustments to values.js X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/9c6be9646631101a3883d4dac08495d5e35e9977 Minor refactors & adjustments to values.js Most significantly, some mathematical constants have been renamed. --- diff --git a/value.js b/value.js index cf858ad..6956b6f 100644 --- a/value.js +++ b/value.js @@ -1,47 +1,52 @@ -// ♓🌟 Piscēs ∷ value.js -// ==================================================================== -// -// Copyright © 2022‐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 . +// SPDX-FileCopyrightText: 2022, 2023, 2025 Lady +// SPDX-License-Identifier: MPL-2.0 +/** + * ⁌ ♓🧩 Piscēs ∷ value.js + * + * Copyright © 2022–2023, 2025 Lady [@ Ladys 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 . + */ + +const PISCĒS = "♓🧩 Piscēs"; export const { - /** The welknown `@@asyncIterator` symbol. */ + /** The welknown `@@asyncIterator´ symbol. */ asyncIterator: ASYNC_ITERATOR, - /** The welknown `@@hasInstance` symbol. */ + /** The welknown `@@hasInstance´ symbol. */ hasInstance: HAS_INSTANCE, - /** The welknown `@@isConcatSpreadable` symbol. */ + /** The welknown `@@isConcatSpreadable´ symbol. */ isConcatSpreadable: IS_CONCAT_SPREADABLE, - /** The welknown `@@iterator` symbol. */ + /** The welknown `@@iterator´ symbol. */ iterator: ITERATOR, - /** The welknown `@@match` symbol. */ + /** The welknown `@@match´ symbol. */ match: MATCH, - /** The welknown `@@matchAll` symbol. */ + /** The welknown `@@matchAll´ symbol. */ matchAll: MATCH_ALL, - /** The welknown `@@replace` symbol. */ + /** The welknown `@@replace´ symbol. */ replace: REPLACE, - /** The welknown `@@species` symbol. */ + /** The welknown `@@species´ symbol. */ species: SPECIES, - /** The welknown `@@split` symbol. */ + /** The welknown `@@split´ symbol. */ split: SPLIT, - /** The welknown `@@toPrimitive` symbol. */ + /** The welknown `@@toPrimitive´ symbol. */ toPrimitive: TO_PRIMITIVE, - /** The welknown `@@toStringTag` symbol. */ + /** The welknown `@@toStringTag´ symbol. */ toStringTag: TO_STRING_TAG, - /** The welknown `@@unscopables` symbol. */ + /** The welknown `@@unscopables´ symbol. */ unscopables: UNSCOPABLES, } = Symbol; @@ -49,116 +54,116 @@ export const { /** * ln(10). * - * ※ This is an alias for `Math.LN10`. + * ※ This is an alias for `Math.LN10´. */ - LN10, + LN10: LN_10, /** * ln(2). * - * ※ This is an alias for `Math.LN2`. + * ※ This is an alias for `Math.LN2´. */ - LN2, + LN2: LN_2, /** - * log10(ℇ). + * log10(e). * - * ※ This is an alias for `Math.LOG10E`. + * ※ This is an alias for `Math.LOG10E´. */ - LOG10E: LOG10ℇ, + LOG10E: LOG10_𝑒, /** - * log2(ℇ). + * log2(e). * - * ※ This is an alias for `Math.LOG2E`. + * ※ This is an alias for `Math.LOG2E´. */ - LOG2E: LOG2ℇ, + LOG2E: LOG2_𝑒, /** * sqrt(½). * - * ※ This is an alias for `Math.SQRT1_2`. + * ※ This is an alias for `Math.SQRT1_2´. */ - SQRT1_2: RECIPROCAL_SQRT2, + SQRT1_2: RECIPROCAL_SQRT_2, /** * sqrt(2). * - * ※ This is an alias for `Math.SQRT2`. + * ※ This is an alias for `Math.SQRT2´. */ - SQRT2, + SQRT2: SQRT_2, /** - * The mathematical constant π. + * The mathematical constant 𝑒. * - * ※ This is an alias for `Math.PI`. + * ※ This is an alias for `Math.E´. */ - PI: Π, + E: 𝑒, /** - * The Euler number. + * The mathematical constant 𝜋. * - * ※ This is an alias for `Math.E`. + * ※ This is an alias for `Math.PI´. */ - E: ℇ, + PI: 𝜋, } = Math; export const { /** * The largest number value less than infinity. * - * ※ This is an alias for `Number.MAX_VALUE`. + * ※ This is an alias for `Number.MAX_VALUE´. */ MAX_VALUE: MAXIMUM_NUMBER, /** - * 2**53 - 1. + * 2^53 - 1. * - * ※ This is an alias for `Number.MAX_SAFE_INTEGER`. + * ※ This is an alias for `Number.MAX_SAFE_INTEGER´. */ MAX_SAFE_INTEGER: MAXIMUM_SAFE_INTEGRAL_NUMBER, /** * The smallest number value greater than negative infinity. * - * ※ This is an alias for `Number.MIN_VALUE`. + * ※ This is an alias for `Number.MIN_VALUE´. */ MIN_VALUE: MINIMUM_NUMBER, /** - * -(2**53 - 1). + * -(2^53 - 1). * - * ※ This is an alias for `Number.MIN_SAFE_INTEGER`. + * ※ This is an alias for `Number.MIN_SAFE_INTEGER´. */ MIN_SAFE_INTEGER: MINIMUM_SAFE_INTEGRAL_NUMBER, /** * Negative infinity. * - * ※ This is an alias for `Number.NEGATIVE_INFINITY`. + * ※ This is an alias for `Number.NEGATIVE_INFINITY´. */ NEGATIVE_INFINITY, /** * Nan. * - * ※ This is an alias for `Number.NaN`. + * ※ This is an alias for `Number.NaN´. */ NaN: NAN, /** * Positive infinity. * - * ※ This is an alias for `Number.POSITIVE_INFINITY`. + * ※ This is an alias for `Number.POSITIVE_INFINITY´. */ POSITIVE_INFINITY, /** * The difference between 1 and the smallest number greater than 1. * - * ※ This is an alias for `Number.EPSILON`. + * ※ This is an alias for `Number.EPSILON´. */ - EPSILON: Ε, + EPSILON: 𝜀, } = Number; /** Negative zero. */ @@ -181,54 +186,55 @@ export const UNDEFINED = undefined; */ export const completePropertyDescriptor = (Desc) => { if (Desc === UNDEFINED) { + // A description was not provided; this is an error. throw new TypeError( - "Piscēs: Cannot complete undefined property descriptor.", + `${PISCĒS}: Cannot complete undefined property descriptor.`, ); } else if (!("get" in Desc || "set" in Desc)) { // This is a generic or data descriptor. if (!("value" in Desc)) { - // `value` is not defined on this. + // `value´ is not defined on this. Desc.value = UNDEFINED; } else { - // `value` is already defined on this. + // `value´ is already defined on this. /* do nothing */ } if (!("writable" in Desc)) { - // `writable` is not defined on this. + // `writable´ is not defined on this. Desc.writable = false; } else { - // `writable` is already defined on this. + // `writable´ is already defined on this. /* do nothing */ } } else { // This is not a generic or data descriptor. if (!("get" in Desc)) { - // `get` is not defined on this. + // `get´ is not defined on this. Desc.get = UNDEFINED; } else { - // `get` is already defined on this. + // `get´ is already defined on this. /* do nothing */ } if (!("set" in Desc)) { - // `set` is not defined on this. + // `set´ is not defined on this. Desc.set = UNDEFINED; } else { - // `set` is already defined on this. + // `set´ is already defined on this. /* do nothing */ } } if (!("enumerable" in Desc)) { - // `enumerable` is not defined on this. + // `enumerable´ is not defined on this. Desc.enumerable = false; } else { - // `enumerable` is already defined on this. + // `enumerable´ is already defined on this. /* do nothing */ } if (!("configurable" in Desc)) { - // `configurable` is not defined on this. + // `configurable´ is not defined on this. Desc.configurable = false; } else { - // `configurable` is already defined on this. + // `configurable´ is already defined on this. /* do nothing */ } }; @@ -246,27 +252,27 @@ export const isDataDescriptor = (Desc) => * descriptor. */ export const isFullyPopulatedDescriptor = (Desc) => - Desc !== UNDEFINED && - ("value" in Desc && "writable" in Desc || - "get" in Desc && "set" in Desc) && - "enumerable" in Desc && "configurable" in Desc; + Desc !== UNDEFINED + && ("value" in Desc && "writable" in Desc + || "get" in Desc && "set" in Desc) + && "enumerable" in Desc && "configurable" in Desc; /** * Gets whether the provided value is a generic (not accessor or data) * descrtiptor. */ export const isGenericDescriptor = (Desc) => - Desc !== UNDEFINED && - !("get" in Desc || "set" in Desc || "value" in Desc || - "writable" in Desc); + Desc !== UNDEFINED + && !("get" in Desc || "set" in Desc || "value" in Desc + || "writable" in Desc); export const { /** * Returns the primitive value of the provided object per its - * `.toString` and `.valueOf` methods. + * `.toString´ and `.valueOf´ methods. * - * If the provided hint is "string", then `.toString` takes - * precedence; otherwise, `.valueOf` does. + * If the provided hint is "string", then `.toString´ takes + * precedence; otherwise, `.valueOf´ does. * * Throws an error if both of these methods are not callable or do * not return a primitive. @@ -285,7 +291,7 @@ export const { * * The provided preferred type, if specified, should be "string", * "number", or "default". If the provided input has a - * `.[Symbol.toPrimitive]` method, this function will throw rather + * `.[Symbol.toPrimitive]´ method, this function will throw rather * than calling that method with a preferred type other than one of * the above. */ @@ -303,6 +309,11 @@ export const { ? ["toString", "valueOf"] : ["valueOf", "toString"]; for (let index = 0; index < methodNames.length; ++index) { + // Test the methods in the order determined above (based on the + // hint) and return the result if the method returns a + // primitive. + // + // ☡ If this loop exits with·out returning, it is an error. const method = O[methodNames[index]]; if (typeof method === "function") { // Method is callable. @@ -320,19 +331,22 @@ export const { } } throw new TypeError( - "Piscēs: Unable to convert object to primitive", + `${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. + // 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. + // The provided value not a symbol. + // + // Convert it to a string property key. return `${key}`; } })(); @@ -341,12 +355,12 @@ export const { toPrimitive: ($, preferredType = "default") => { const hint = `${preferredType}`; if ( - "default" !== hint && "string" !== hint && - "number" !== hint + "default" !== hint && "string" !== hint + && "number" !== hint ) { // An invalid preferred type was specified. throw new TypeError( - `Piscēs: Invalid preferred type: ${preferredType}.`, + `${PISCĒS}: Invalid preferred type: ${preferredType}.`, ); } else if (type($) === "object") { // The provided value is an object. @@ -357,7 +371,7 @@ export const { if (typeof exoticToPrim !== "function") { // The method is not callable. throw new TypeError( - "Piscēs: `.[Symbol.toPrimitive]` was neither nullish nor callable.", + `${PISCĒS}: .[Symbol.toPrimitive] was neither nullish nor callable.`, ); } else { // The method is callable. @@ -379,7 +393,7 @@ export const { /** * Returns whether the provided values are the same value. * - * ※ This differs from `===` in the cases of nan and zero. + * ※ This differs from `===´ in the cases of nan and zero. */ sameValue, @@ -387,7 +401,7 @@ export const { * Returns whether the provided values are either the same value or * both zero (either positive or negative). * - * ※ This differs from `===` in the case of nan. + * ※ This differs from `===´ in the case of nan. */ sameValueZero, @@ -414,11 +428,14 @@ export const { // The provided values are not of the same type. return false; } else if (type1 === "number") { - // The provided values are numbers; check if they are nan and - // use strict equality otherwise. + // The provided values are numbers. + // + // Check if they are nan and use strict equality otherwise. return isNan($1) && isNan($2) || $1 === $2; } else { - // The provided values are not numbers; use strict equality. + // The provided values are not numbers. + // + // Use strict equality. return $1 === $2; } }, @@ -426,15 +443,19 @@ export const { const integer = floor($); if (isNan(integer) || integer == 0) { // The value is zero·like. + // + // Return positive zero. return 0; } else { // The value is not zero·like. const clamped = toLength(integer); if (clamped !== integer) { - // Clamping the value changes it. - throw new RangeError(`Piscēs: Index out of range: ${$}.`); + // Clamping the value changes it; this is an error. + throw new RangeError(`${PISCĒS}: Index out of range: ${$}.`); } else { // The value is within appropriate bounds. + // + // Return it. return integer; } } @@ -461,8 +482,8 @@ export const toPropertyKey = ($) => { * Returns a lowercase string identifying the type of the provided * value. * - * This differs from the value of the `typeof` operator only in the - * cases of objects and null. + * This differs from the value of the `typeof´ operator only in the + * cases of callable objects and null. */ export const type = ($) => { if ($ === null) { diff --git a/value.test.js b/value.test.js index ce6464d..b7dcd69 100644 --- a/value.test.js +++ b/value.test.js @@ -1,11 +1,14 @@ -// ♓🌟 Piscēs ∷ value.test.js -// ==================================================================== -// -// Copyright © 2022–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 . +// SPDX-FileCopyrightText: 2022, 2023, 2025 Lady +// SPDX-License-Identifier: MPL-2.0 +/** + * ⁌ ♓🌟 Piscēs ∷ value.test.js + * + * Copyright © 2022–2023, 2025 Lady [@ Ladys 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 { assertEquals, @@ -24,10 +27,10 @@ import { isFullyPopulatedDescriptor, isGenericDescriptor, ITERATOR, - LN10, - LN2, - LOG10ℇ, - LOG2ℇ, + LN_10, + LN_2, + LOG10_𝑒, + LOG2_𝑒, MATCH, MATCH_ALL, MAXIMUM_NUMBER, @@ -41,13 +44,13 @@ import { ordinaryToPrimitive, POSITIVE_INFINITY, POSITIVE_ZERO, - RECIPROCAL_SQRT2, + RECIPROCAL_SQRT_2, REPLACE, sameValue, sameValueZero, SPECIES, SPLIT, - SQRT2, + SQRT_2, TO_PRIMITIVE, TO_STRING_TAG, toFunctionName, @@ -58,9 +61,9 @@ import { type, UNDEFINED, UNSCOPABLES, - Ε, - Π, - ℇ, + 𝑒, + 𝜀, + 𝜋, } from "./value.js"; describe("ASYNC_ITERATOR", () => { @@ -90,27 +93,27 @@ describe("ITERATOR", () => { }); }); -describe("LN10", () => { +describe("LN_10", () => { it("[[Get]] is ln(10)", () => { - assertStrictEquals(LN10, Math.LN10); + assertStrictEquals(LN_10, Math.LN10); }); }); -describe("LN2", () => { +describe("LN_2", () => { it("[[Get]] is ln(2)", () => { - assertStrictEquals(LN2, Math.LN2); + assertStrictEquals(LN_2, Math.LN2); }); }); -describe("LOG10ℇ", () => { - it("[[Get]] is log10(ℇ)", () => { - assertStrictEquals(LOG10ℇ, Math.LOG10E); +describe("LOG10_𝑒", () => { + it("[[Get]] is log10(𝑒)", () => { + assertStrictEquals(LOG10_𝑒, Math.LOG10E); }); }); -describe("LOG2ℇ", () => { +describe("LOG2_𝑒", () => { it("[[Get]] is log2(ℇ)", () => { - assertStrictEquals(LOG2ℇ, Math.LOG2E); + assertStrictEquals(LOG2_𝑒, Math.LOG2E); }); }); @@ -192,9 +195,9 @@ describe("POSITIVE_ZERO", () => { }); }); -describe("RECIPROCAL_SQRT2", () => { +describe("RECIPROCAL_SQRT_2", () => { it("[[Get]] is sqrt(½)", () => { - assertStrictEquals(RECIPROCAL_SQRT2, Math.SQRT1_2); + assertStrictEquals(RECIPROCAL_SQRT_2, Math.SQRT1_2); }); }); @@ -216,9 +219,9 @@ describe("SPLIT", () => { }); }); -describe("SQRT2", () => { +describe("SQRT_2", () => { it("[[Get]] is sqrt(2)", () => { - assertStrictEquals(SQRT2, Math.SQRT2); + assertStrictEquals(SQRT_2, Math.SQRT2); }); }); @@ -1071,20 +1074,20 @@ describe("type", () => { }); }); -describe("Ε", () => { - it("[[Get]] is ε", () => { - assertStrictEquals(Ε, Number.EPSILON); +describe("𝑒", () => { + it("[[Get]] is 𝑒", () => { + assertStrictEquals(𝑒, Math.E); }); }); -describe("Π", () => { - it("[[Get]] is π", () => { - assertStrictEquals(Π, Math.PI); +describe("𝜀", () => { + it("[[Get]] is 𝜀", () => { + assertStrictEquals(𝜀, Number.EPSILON); }); }); -describe("ℇ", () => { - it("[[Get]] is ℇ", () => { - assertStrictEquals(ℇ, Math.E); +describe("𝜋", () => { + it("[[Get]] is 𝜋", () => { + assertStrictEquals(𝜋, Math.PI); }); });