…and add some missing tests for isIntegerIndexString.
// file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
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;
"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") {
} from "./dev-deps.js";
import {
findIndexedEntry,
- isArrayIndexString,
isArraylikeObject,
isCollection,
isConcatSpreadable,
});
});
-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);
{ 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.
*
getCodeUnit,
getFirstSubstringIndex,
getLastSubstringIndex,
+ isArrayIndexString,
isIntegerIndexString,
join,
Matcher,
});
});
+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);
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", () => {