From: Lady Date: Fri, 17 Nov 2023 02:09:38 +0000 (-0500) Subject: Move isIntegerIndexString into value.js X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/8c273de93ce5adb58bb90c3fe676730a451b3c89 Move isIntegerIndexString into value.js While potentially relevant to collections, this function is actually about classifying types of value. --- diff --git a/collection.js b/collection.js index 4545321..d106ba4 100644 --- a/collection.js +++ b/collection.js @@ -8,10 +8,6 @@ // file, You can obtain one at . import { call, createCallableFunction } from "./function.js"; -import { - isIntegralNumber, - MAXIMUM_SAFE_INTEGRAL_NUMBER, -} from "./numeric.js"; import { canonicalNumericIndexString, lengthOfArraylike, @@ -277,20 +273,6 @@ export const isConcatSpreadable = ($) => { } }; -/** Returns whether the provided value is an integer index string. */ -export const isIntegerIndexString = ($) => { - const value = canonicalNumericIndexString($); - if (value !== undefined && isIntegralNumber(value)) { - // The provided value is a canonical numeric index string. - return sameValue(value, 0) || - value > 0 && value <= MAXIMUM_SAFE_INTEGRAL_NUMBER && - value === toLength(value); - } else { - // The provided value is not a canonical numeric index string. - return false; - } -}; - /** * Returns an iterator over the items in the provided value according * to the algorithm of `Array::values`. diff --git a/collection.test.js b/collection.test.js index 544f454..7c242cc 100644 --- a/collection.test.js +++ b/collection.test.js @@ -22,7 +22,6 @@ import { isArraylikeObject, isCollection, isConcatSpreadable, - isIntegerIndexString, } from "./collection.js"; describe("findIndexedEntry", () => { @@ -261,46 +260,3 @@ describe("isConcatSpreadable", () => { ); }); }); - -describe("isIntegerIndexString", () => { - it("[[Call]] returns false for nonstrings", () => { - assertStrictEquals(isIntegerIndexString(1), false); - }); - - it("[[Call]] returns false for noncanonical strings", () => { - assertStrictEquals(isIntegerIndexString(""), false); - assertStrictEquals(isIntegerIndexString("01"), false); - assertStrictEquals( - isIntegerIndexString("9007199254740993"), - false, - ); - }); - - it("[[Call]] returns false for nonfinite numbers", () => { - assertStrictEquals(isIntegerIndexString("NaN"), false); - assertStrictEquals(isIntegerIndexString("Infinity"), false); - assertStrictEquals(isIntegerIndexString("-Infinity"), false); - }); - - it("[[Call]] returns false for negative numbers", () => { - assertStrictEquals(isIntegerIndexString("-0"), false); - assertStrictEquals(isIntegerIndexString("-1"), false); - }); - - it("[[Call]] returns false for nonintegers", () => { - assertStrictEquals(isIntegerIndexString("0.25"), false); - assertStrictEquals(isIntegerIndexString("1.1"), false); - }); - - it("[[Call]] returns false for numbers greater than or equal to 2 ** 53", () => { - assertStrictEquals( - isIntegerIndexString("9007199254740992"), - false, - ); - }); - - it("[[Call]] returns true for safe canonical integer strings", () => { - assertStrictEquals(isIntegerIndexString("0"), true); - assertStrictEquals(isIntegerIndexString("9007199254740991"), true); - }); -}); diff --git a/value.js b/value.js index dca20e8..5e9f052 100644 --- a/value.js +++ b/value.js @@ -171,6 +171,9 @@ export const { })(); export const { + /** Returns whether the provided value is an integer index string. */ + isIntegerIndexString, + /** * Returns whether the provided values are the same value. * @@ -200,10 +203,25 @@ export const { const { floor, max, min } = Math; const { MAX_SAFE_INTEGER: MAXIMUM_SAFE_INTEGRAL_NUMBER, + isInteger: isIntegralNumber, isNaN: isNan, } = Number; const { is } = Object; return { + isIntegerIndexString: ($) => { + const value = canonicalNumericIndexString($); + if (value !== undefined && isIntegralNumber(value)) { + // The provided value is an integral canonical numeric index + // string. + return sameValue(value, 0) || + value > 0 && value <= MAXIMUM_SAFE_INTEGRAL_NUMBER && + value === toLength(value); + } else { + // The provided value is not an integral canonical numeric + // index string. + return false; + } + }, sameValue: (a, b) => is(a, b), sameValueZero: ($1, $2) => { const type1 = type($1); diff --git a/value.test.js b/value.test.js index cecafbb..babd59d 100644 --- a/value.test.js +++ b/value.test.js @@ -18,6 +18,7 @@ import { canonicalNumericIndexString, HAS_INSTANCE, IS_CONCAT_SPREADABLE, + isIntegerIndexString, ITERATOR, lengthOfArraylike, MATCH, @@ -179,6 +180,49 @@ describe("canonicalNumericIndexString", () => { }); }); +describe("isIntegerIndexString", () => { + it("[[Call]] returns false for nonstrings", () => { + assertStrictEquals(isIntegerIndexString(1), false); + }); + + it("[[Call]] returns false for noncanonical strings", () => { + assertStrictEquals(isIntegerIndexString(""), false); + assertStrictEquals(isIntegerIndexString("01"), false); + assertStrictEquals( + isIntegerIndexString("9007199254740993"), + false, + ); + }); + + it("[[Call]] returns false for nonfinite numbers", () => { + assertStrictEquals(isIntegerIndexString("NaN"), false); + assertStrictEquals(isIntegerIndexString("Infinity"), false); + assertStrictEquals(isIntegerIndexString("-Infinity"), false); + }); + + it("[[Call]] returns false for negative numbers", () => { + assertStrictEquals(isIntegerIndexString("-0"), false); + assertStrictEquals(isIntegerIndexString("-1"), false); + }); + + it("[[Call]] returns false for nonintegers", () => { + assertStrictEquals(isIntegerIndexString("0.25"), false); + assertStrictEquals(isIntegerIndexString("1.1"), false); + }); + + it("[[Call]] returns false for numbers greater than or equal to 2 ** 53", () => { + assertStrictEquals( + isIntegerIndexString("9007199254740992"), + false, + ); + }); + + it("[[Call]] returns true for safe canonical integer strings", () => { + assertStrictEquals(isIntegerIndexString("0"), true); + assertStrictEquals(isIntegerIndexString("9007199254740991"), true); + }); +}); + describe("lengthOfArraylike", () => { it("[[Call]] returns the length", () => { assertStrictEquals(