From: Lady Date: Thu, 24 Jul 2025 14:10:13 +0000 (-0400) Subject: Minor refactors to numeric.js X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/HEAD Minor refactors to numeric.js --- diff --git a/numeric.js b/numeric.js index 079e56e..c4d07f8 100644 --- a/numeric.js +++ b/numeric.js @@ -1,11 +1,14 @@ -// ♓🌟 Piscēs ∷ numeric.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 ∷ numeric.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 { call, createArrowFunction } from "./function.js"; import { defineOwnDataProperty } from "./object.js"; @@ -25,10 +28,12 @@ import { UNDEFINED, } from "./value.js"; +const PISCĒS = "♓🧩 Piscēs"; + /** * Returns the magnitude (absolute value) of the provided value. * - * ※ Unlike `Math.abs`, this function can take big·int arguments. + * ※ Unlike `Math.abs´, this function can take big·int arguments. */ export const abs = ($) => { const n = toNumeric($); @@ -48,7 +53,7 @@ export const abs = ($) => { /** * Returns the arccos of the provided value. * - * ※ This function is effectively an alias for `Math.acos`. + * ※ This function is effectively an alias for `Math.acos´. * * ☡ This function does not allow big·int arguments. */ @@ -60,7 +65,7 @@ export const arccos = createArrowFunction( /** * Returns the arccosh of the provided value. * - * ※ This function is effectively an alias for `Math.acosh`. + * ※ This function is effectively an alias for `Math.acosh´. * * ☡ This function does not allow big·int arguments. */ @@ -72,7 +77,7 @@ export const arccosh = createArrowFunction( /** * Returns the arcsin of the provided value. * - * ※ This function is effectively an alias for `Math.asin`. + * ※ This function is effectively an alias for `Math.asin´. * * ☡ This function does not allow big·int arguments. */ @@ -84,7 +89,7 @@ export const arcsin = createArrowFunction( /** * Returns the arcsinh of the provided value. * - * ※ This function is effectively an alias for `Math.asinh`. + * ※ This function is effectively an alias for `Math.asinh´. * * ☡ This function does not allow big·int arguments. */ @@ -96,7 +101,7 @@ export const arcsinh = createArrowFunction( /** * Returns the arctan of the provided value. * - * ※ This function is effectively an alias for `Math.atan`. + * ※ This function is effectively an alias for `Math.atan´. * * ☡ This function does not allow big·int arguments. */ @@ -109,7 +114,7 @@ export const { /** * Returns the arctangent of the dividend of the provided values. * - * ※ Unlike `Math.atan2`, this function can take big·int arguments. + * ※ Unlike `Math.atan2´, this function can take big·int arguments. * However, the result will always be a number. */ arctan2, @@ -118,7 +123,7 @@ export const { * Returns the number of leading zeroes in the 32‐bit representation * of the provided value. * - * ※ Unlike `Math.clz32`, this function accepts either number or + * ※ Unlike `Math.clz32´, this function accepts either number or * big·int values. */ clz32, @@ -127,7 +132,7 @@ export const { * Returns the 32‐bit float which best approximate the provided * value. * - * ※ Unlike `Math.fround`, this function can take big·int arguments. + * ※ Unlike `Math.fround´, this function can take big·int arguments. * However, the result will always be a number. */ toFloat32, @@ -150,7 +155,7 @@ export const { /** * Returns the arctanh of the provided value. * - * ※ This function is effectively an alias for `Math.atanh`. + * ※ This function is effectively an alias for `Math.atanh´. * * ☡ This function does not allow big·int arguments. */ @@ -162,7 +167,7 @@ export const arctanh = createArrowFunction( /** * Returns the cube root of the provided value. * - * ※ This function is effectively an alias for `Math.cbrt`. + * ※ This function is effectively an alias for `Math.cbrt´. * * ☡ This function does not allow big·int arguments. */ @@ -171,7 +176,7 @@ export const cbrt = createArrowFunction(Math.cbrt); /** * Returns the ceiling of the provided value. * - * ※ This function is effectively an alias for `Math.ceil`. + * ※ This function is effectively an alias for `Math.ceil´. * * ☡ This function does not allow big·int arguments. */ @@ -180,7 +185,7 @@ export const ceil = createArrowFunction(Math.ceil); /** * Returns the cos of the provided value. * - * ※ This function is effectively an alias for `Math.cos`. + * ※ This function is effectively an alias for `Math.cos´. * * ☡ This function does not allow big·int arguments. */ @@ -189,7 +194,7 @@ export const cos = createArrowFunction(Math.cos); /** * Returns the cosh of the provided value. * - * ※ This function is effectively an alias for `Math.cosh`. + * ※ This function is effectively an alias for `Math.cosh´. * * ☡ This function does not allow big·int arguments. */ @@ -198,7 +203,7 @@ export const cosh = createArrowFunction(Math.cosh); /** * Returns the Euler number raised to the provided value. * - * ※ This function is effectively an alias for `Math.exp`. + * ※ This function is effectively an alias for `Math.exp´. * * ☡ This function does not allow big·int arguments. */ @@ -207,7 +212,7 @@ export const exp = createArrowFunction(Math.exp); /** * Returns the Euler number raised to the provided value, minus one. * - * ※ This function is effectively an alias for `Math.expm1`. + * ※ This function is effectively an alias for `Math.expm1´. * * ☡ This function does not allow big·int arguments. */ @@ -216,7 +221,7 @@ export const expm1 = createArrowFunction(Math.expm1); /** * Returns the floor of the provided value. * - * ※ This function is effectively an alias for `Math.floor`. + * ※ This function is effectively an alias for `Math.floor´. * * ☡ This function does not allow big·int arguments. */ @@ -226,7 +231,7 @@ export const floor = createArrowFunction(Math.floor); * Returns the square root of the sum of the squares of the provided * arguments. * - * ※ This function is effectively an alias for `Math.hypot`. + * ※ This function is effectively an alias for `Math.hypot´. * * ☡ This function does not allow big·int arguments. */ @@ -235,7 +240,7 @@ export const hypot = createArrowFunction(Math.hypot); /** * Returns whether the provided value is a finite number. * - * ※ This function is effectively an alias for `Number.isFinite`. + * ※ This function is effectively an alias for `Number.isFinite´. */ export const isFiniteNumber = createArrowFunction( Number.isFinite, @@ -245,7 +250,7 @@ export const isFiniteNumber = createArrowFunction( /** * Returns whether the provided value is an integral number. * - * ※ This function is effectively an alias for `Number.isInteger`. + * ※ This function is effectively an alias for `Number.isInteger´. */ export const isIntegralNumber = createArrowFunction( Number.isInteger, @@ -255,7 +260,7 @@ export const isIntegralNumber = createArrowFunction( /** * Returns whether the provided value is nan. * - * ※ This function is effectively an alias for `Number.isNaN`. + * ※ This function is effectively an alias for `Number.isNaN´. */ export const isNan = createArrowFunction( Number.isNaN, @@ -265,7 +270,7 @@ export const isNan = createArrowFunction( /** * Returns whether the provided value is a safe integral number. * - * ※ This function is effectively an alias for `Number.isSafeInteger`. + * ※ This function is effectively an alias for `Number.isSafeInteger´. */ export const isSafeIntegralNumber = createArrowFunction( Number.isSafeInteger, @@ -275,7 +280,7 @@ export const isSafeIntegralNumber = createArrowFunction( /** * Returns the ln of the provided value. * - * ※ This function is effectively an alias for `Math.log`. + * ※ This function is effectively an alias for `Math.log´. * * ☡ This function does not allow big·int arguments. */ @@ -284,7 +289,7 @@ export const ln = createArrowFunction(Math.log, { name: "ln" }); /** * Returns the ln of one plus the provided value. * - * ※ This function is effectively an alias for `Math.log1p`. + * ※ This function is effectively an alias for `Math.log1p´. * * ☡ This function does not allow big·int arguments. */ @@ -293,7 +298,7 @@ export const ln1p = createArrowFunction(Math.log1p, { name: "ln1p" }); /** * Returns the log10 of the provided value. * - * ※ This function is effectively an alias for `Math.log10`. + * ※ This function is effectively an alias for `Math.log10´. * * ☡ This function does not allow big·int arguments. */ @@ -302,7 +307,7 @@ export const log10 = createArrowFunction(Math.log10); /** * Returns the log2 of the provided value. * - * ※ This function is effectively an alias for `Math.log2`. + * ※ This function is effectively an alias for `Math.log2´. * * ☡ This function does not allow big·int arguments. */ @@ -312,7 +317,7 @@ export const log2 = createArrowFunction(Math.log2); * Returns the highest value of the provided arguments, or negative * infinity if no argument is provided. * - * ※ Unlike `Math.max`, this function accepts either number or big·int + * ※ Unlike `Math.max´, this function accepts either number or big·int * values. All values must be of the same type, or this function will * throw an error. * @@ -335,9 +340,9 @@ export const max = Object.defineProperties((...$s) => { } } else { if (typeof highest !== typeof number) { - // The type of the current number and the lowest number don’t + // The type of the current number and the lowest number don¦t // match. - throw new TypeError("Piscēs: Type mismatch."); + throw new TypeError(`${PISCĒS}: Type mismatch.`); } else if (isNan(number)) { // The current number is nan. return NAN; @@ -364,7 +369,7 @@ export const max = Object.defineProperties((...$s) => { * Returns the lowest value of the provided arguments, or positive * infinity if no argument is provided. * - * ※ Unlike `Math.min`, this function accepts either number or big·int + * ※ Unlike `Math.min´, this function accepts either number or big·int * values. All values must be of the same type, or this function will * throw an error. * @@ -388,9 +393,9 @@ export const min = Object.defineProperties((...$s) => { } else { // The current number is not the first one. if (typeof lowest !== typeof number) { - // The type of the current number and the lowest number don’t + // The type of the current number and the lowest number don¦t // match. - throw new TypeError("Piscēs: Type mismatch."); + throw new TypeError(`${PISCĒS}: Type mismatch.`); } else if (isNan(number)) { // The current number is nan. return NAN; @@ -416,7 +421,7 @@ export const min = Object.defineProperties((...$s) => { /** * Returns a pseudo·random value in the range [0, 1). * - * ※ This function is effectively an alias for `Math.random`. + * ※ This function is effectively an alias for `Math.random´. */ export const rand = createArrowFunction( Math.random, @@ -426,7 +431,7 @@ export const rand = createArrowFunction( /** * Returns the round of the provided value. * - * ※ This function is effectively an alias for `Math.round`. + * ※ This function is effectively an alias for `Math.round´. * * ☡ This function does not allow big·int arguments. */ @@ -447,7 +452,7 @@ export const round = createArrowFunction(Math.round); * that positive and negative infinity will return +1 and −1 * respectively. * - * ※ Unlike `Math.sign`, this function accepts either number or + * ※ Unlike `Math.sign´, this function accepts either number or * big·int values. */ export const sgn = ($) => { @@ -465,7 +470,7 @@ export const sgn = ($) => { /** * Returns the sin of the provided value. * - * ※ This function is effectively an alias for `Math.sin`. + * ※ This function is effectively an alias for `Math.sin´. * * ☡ This function does not allow big·int arguments. */ @@ -474,7 +479,7 @@ export const sin = createArrowFunction(Math.sin); /** * Returns the sinh of the provided value. * - * ※ This function is effectively an alias for `Math.sinh`. + * ※ This function is effectively an alias for `Math.sinh´. * * ☡ This function does not allow big·int arguments. */ @@ -483,7 +488,7 @@ export const sinh = createArrowFunction(Math.sinh); /** * Returns the square root of the provided value. * - * ※ This function is effectively an alias for `Math.sqrt`. + * ※ This function is effectively an alias for `Math.sqrt´. * * ☡ This function does not allow big·int arguments. */ @@ -492,7 +497,7 @@ export const sqrt = createArrowFunction(Math.sqrt); /** * Returns the tan of the provided value. * - * ※ This function is effectively an alias for `Math.tan`. + * ※ This function is effectively an alias for `Math.tan´. * * ☡ This function does not allow big·int arguments. */ @@ -501,7 +506,7 @@ export const tan = createArrowFunction(Math.tan); /** * Returns the tanh of the provided value. * - * ※ This function is effectively an alias for `Math.tanh`. + * ※ This function is effectively an alias for `Math.tanh´. * * ☡ This function does not allow big·int arguments. */ @@ -512,7 +517,7 @@ export const tanh = createArrowFunction(Math.tanh); * * ※ This method is safe to use with numbers. * - * ※ This is effectively an alias for `BigInt`. + * ※ This is effectively an alias for `BigInt´. */ export const { toBigInt } = (() => { const makeBigInt = BigInt; @@ -552,7 +557,7 @@ export const { const f = toIntegralNumberOrInfinity(fractionDigits); if (!isFiniteNumber(f) || f < 0 || f > 100) { throw new RangeError( - `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`, + `${PISCĒS}: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`, ); } else { if (typeof n === "number") { @@ -589,7 +594,7 @@ export const { const f = toIntegralNumberOrInfinity(fractionDigits); if (!isFiniteNumber(f) || f < 0 || f > 100) { throw new RangeError( - `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`, + `${PISCĒS}: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`, ); } else { const n = toNumeric($); @@ -649,13 +654,13 @@ export const toIntegralNumberOrInfinity = ($) => { * * ※ This function is safe to use with big·ints. * - * ※ This is effectively a nonconstructible version of the `Number` + * ※ This is effectively a nonconstructible version of the `Number´ * constructor. */ -export const { toNumber } = (() => { - const makeNumber = Number; - return { toNumber: ($) => makeNumber($) }; -})(); +export const toNumber = createArrowFunction( + Number, + { name: "toNumber" }, +); /** * Returns the result of converting the provided value to a number or @@ -674,7 +679,7 @@ export const { * Returns the result of converting the provided value to fit within * the provided number of bits as a signed integer. * - * ※ Unlike `BigInt.asIntN`, this function accepts both big·int and + * ※ Unlike `BigInt.asIntN´, this function accepts both big·int and * number values. * * ☡ The first argument, the number of bits, must be a number. @@ -685,7 +690,7 @@ export const { * Returns the result of converting the provided value to fit within * the provided number of bits as an unsigned integer. * - * ※ Unlike `BigInt.asUintN`, this function accepts both big·int and + * ※ Unlike `BigInt.asUintN´, this function accepts both big·int and * number values. * * ☡ The first argument, the number of bits, must be a number. @@ -734,7 +739,7 @@ export const { /** * Returns the trunc of the provided value. * - * ※ This function is effectively an alias for `Math.trunc`. + * ※ This function is effectively an alias for `Math.trunc´. * * ☡ This function does not allow big·int arguments. */ diff --git a/numeric.test.js b/numeric.test.js index 0129ae3..b6dbf0d 100644 --- a/numeric.test.js +++ b/numeric.test.js @@ -1,11 +1,14 @@ -// ♓🌟 Piscēs ∷ numeric.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 ∷ numeric.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 { assert, @@ -1351,6 +1354,22 @@ describe("toNumber", () => { it("[[Call]] converts to a number", () => { assertStrictEquals(toNumber(2n), 2); }); + + it("[[Construct]] throws an error", () => { + assertThrows(() => new toNumber(1)); + }); + + describe(".length", () => { + it("[[Get]] returns the correct length", () => { + assertStrictEquals(toNumber.length, 1); + }); + }); + + describe(".name", () => { + it("[[Get]] returns the correct name", () => { + assertStrictEquals(toNumber.name, "toNumber"); + }); + }); }); describe("toNumeric", () => {