From: Lady Date: Fri, 17 Nov 2023 02:47:41 +0000 (-0500) Subject: Move isArrayIndexString into string.js X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/6ff9787a78f601e65581024fe605698c75f9dd05 Move isArrayIndexString into string.js …and add some missing tests for isIntegerIndexString. --- diff --git a/collection.js b/collection.js index 0d4e623..69c0bee 100644 --- a/collection.js +++ b/collection.js @@ -8,14 +8,7 @@ // file, You can obtain one at . import { call, createCallableFunction } from "./function.js"; -import { canonicalNumericIndexString } from "./string.js"; -import { - lengthOfArraylike, - sameValue, - toIndex, - toLength, - type, -} from "./value.js"; +import { lengthOfArraylike, toIndex, type } from "./value.js"; const { prototype: arrayPrototype } = Array; @@ -200,19 +193,6 @@ export const indices = createCallableFunction( "indices", ); -/** Returns whether the provided value is an array index string. */ -export const isArrayIndexString = ($) => { - const value = canonicalNumericIndexString($); - if (value !== undefined) { - // The provided value is a canonical numeric index string. - return sameValue(value, 0) || value > 0 && value < -1 >>> 0 && - value === toLength(value); - } else { - // The provided value is not a canonical numeric index string. - return false; - } -}; - /** Returns whether the provided value is arraylike. */ export const isArraylikeObject = ($) => { if (type($) !== "object") { diff --git a/collection.test.js b/collection.test.js index 7c242cc..acd90d2 100644 --- a/collection.test.js +++ b/collection.test.js @@ -18,7 +18,6 @@ import { } from "./dev-deps.js"; import { findIndexedEntry, - isArrayIndexString, isArraylikeObject, isCollection, isConcatSpreadable, @@ -80,50 +79,6 @@ describe("findIndexedEntry", () => { }); }); -describe("isArrayIndexString", () => { - it("[[Call]] returns false for nonstrings", () => { - assertStrictEquals(isArrayIndexString(1), false); - }); - - it("[[Call]] returns false for noncanonical strings", () => { - assertStrictEquals(isArrayIndexString(""), false); - assertStrictEquals(isArrayIndexString("01"), false); - assertStrictEquals(isArrayIndexString("9007199254740993"), false); - }); - - it("[[Call]] returns false for nonfinite numbers", () => { - assertStrictEquals(isArrayIndexString("NaN"), false); - assertStrictEquals(isArrayIndexString("Infinity"), false); - assertStrictEquals(isArrayIndexString("-Infinity"), false); - }); - - it("[[Call]] returns false for negative numbers", () => { - assertStrictEquals(isArrayIndexString("-0"), false); - assertStrictEquals(isArrayIndexString("-1"), false); - }); - - it("[[Call]] returns false for nonintegers", () => { - assertStrictEquals(isArrayIndexString("0.25"), false); - assertStrictEquals(isArrayIndexString("1.1"), false); - }); - - it("[[Call]] returns false for numbers greater than or equal to -1 >>> 0", () => { - assertStrictEquals(isArrayIndexString(String(-1 >>> 0)), false); - assertStrictEquals( - isArrayIndexString(String((-1 >>> 0) + 1)), - false, - ); - }); - - it("[[Call]] returns true for array lengths less than -1 >>> 0", () => { - assertStrictEquals(isArrayIndexString("0"), true); - assertStrictEquals( - isArrayIndexString(String((-1 >>> 0) - 1)), - true, - ); - }); -}); - describe("isArraylikeObject", () => { it("[[Call]] returns false for primitives", () => { assertStrictEquals(isArraylikeObject("failure"), false); diff --git a/string.js b/string.js index bffa6d2..0bd5f9e 100644 --- a/string.js +++ b/string.js @@ -521,6 +521,19 @@ export const getLastSubstringIndex = createCallableFunction( { name: "getLastSubstringIndex" }, ); +/** Returns whether the provided value is an array index string. */ +export const isArrayIndexString = ($) => { + const value = canonicalNumericIndexString($); + if (value !== undefined) { + // The provided value is a canonical numeric index string. + return sameValue(value, 0) || value > 0 && value < -1 >>> 0 && + value === toLength(value); + } else { + // The provided value is not a canonical numeric index string. + return false; + } +}; + /** * Returns the result of joining the provided iterable. * diff --git a/string.test.js b/string.test.js index bdef389..47a87db 100644 --- a/string.test.js +++ b/string.test.js @@ -29,6 +29,7 @@ import { getCodeUnit, getFirstSubstringIndex, getLastSubstringIndex, + isArrayIndexString, isIntegerIndexString, join, Matcher, @@ -934,6 +935,69 @@ describe("getLastSubstringIndex", () => { }); }); +describe("isArrayIndexString", () => { + it("[[Call]] returns false for nonstrings", () => { + assertStrictEquals(isArrayIndexString(1), false); + }); + + it("[[Call]] returns false for noncanonical strings", () => { + assertStrictEquals(isArrayIndexString(""), false); + assertStrictEquals(isArrayIndexString("01"), false); + assertStrictEquals(isArrayIndexString("9007199254740993"), false); + }); + + it("[[Call]] returns false for nonfinite numbers", () => { + assertStrictEquals(isArrayIndexString("NaN"), false); + assertStrictEquals(isArrayIndexString("Infinity"), false); + assertStrictEquals(isArrayIndexString("-Infinity"), false); + }); + + it("[[Call]] returns false for negative numbers", () => { + assertStrictEquals(isArrayIndexString("-0"), false); + assertStrictEquals(isArrayIndexString("-1"), false); + }); + + it("[[Call]] returns false for nonintegers", () => { + assertStrictEquals(isArrayIndexString("0.25"), false); + assertStrictEquals(isArrayIndexString("1.1"), false); + }); + + it("[[Call]] returns false for numbers greater than or equal to -1 >>> 0", () => { + assertStrictEquals(isArrayIndexString(String(-1 >>> 0)), false); + assertStrictEquals( + isArrayIndexString(String((-1 >>> 0) + 1)), + false, + ); + }); + + it("[[Call]] returns true for array lengths less than -1 >>> 0", () => { + assertStrictEquals(isArrayIndexString("0"), true); + assertStrictEquals( + isArrayIndexString(String((-1 >>> 0) - 1)), + true, + ); + }); + + it("[[Construct]] throws an error", () => { + assertThrows(() => new isArrayIndexString("0")); + }); + + describe(".length", () => { + it("[[Get]] returns the correct length", () => { + assertStrictEquals(isArrayIndexString.length, 1); + }); + }); + + describe(".name", () => { + it("[[Get]] returns the correct name", () => { + assertStrictEquals( + isArrayIndexString.name, + "isArrayIndexString", + ); + }); + }); +}); + describe("isIntegerIndexString", () => { it("[[Call]] returns false for nonstrings", () => { assertStrictEquals(isIntegerIndexString(1), false); @@ -975,6 +1039,25 @@ describe("isIntegerIndexString", () => { assertStrictEquals(isIntegerIndexString("0"), true); assertStrictEquals(isIntegerIndexString("9007199254740991"), true); }); + + it("[[Construct]] throws an error", () => { + assertThrows(() => new isIntegerIndexString("0")); + }); + + describe(".length", () => { + it("[[Get]] returns the correct length", () => { + assertStrictEquals(isIntegerIndexString.length, 1); + }); + }); + + describe(".name", () => { + it("[[Get]] returns the correct name", () => { + assertStrictEquals( + isIntegerIndexString.name, + "isIntegerIndexString", + ); + }); + }); }); describe("join", () => {