]>
Lady’s Gitweb - Pisces/blob - collection.test.js
544f454849a8dd4507eaa770ad1960db5798ac96
1 // ♓🌟 Piscēs ∷ collection.test.js
2 // ====================================================================
4 // Copyright © 2022 Lady [@ Lady’s Computer].
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
18 } from "./dev-deps.js";
26 } from "./collection.js";
28 describe("findIndexedEntry", () => {
29 it("[[Call]] returns undefined if no matching entry exists", () => {
30 assertStrictEquals(findIndexedEntry([], () => true), void {});
31 assertStrictEquals(findIndexedEntry([1], () => false), void {});
34 it("[[Call]] returns an entry for the first match", () => {
36 findIndexedEntry([, true, false], ($) => $ ?? true),
40 findIndexedEntry(["failure", "success"], ($) => $ == "success"),
45 it("[[Call]] works on arraylike objects", () => {
47 findIndexedEntry({ 1: "success", length
: 2 }, ($) => $),
51 findIndexedEntry({ 1: "failure", length
: 1 }, ($) => $),
56 it("[[Call]] only gets the value once", () => {
57 const get1
= spy(() => true);
64 assertSpyCalls(get1
, 1);
67 it("[[Call]] passes the value, index, and this value to the callback", () => {
68 const arr
= ["failure", "success", "success"];
69 const callback
= spy(($) => $ === "success");
71 findIndexedEntry(arr
, callback
, thisArg
);
72 assertSpyCalls(callback
, 2);
73 assertSpyCall(callback
, 0, {
74 args
: ["failure", 0, arr
],
77 assertSpyCall(callback
, 1, {
78 args
: ["success", 1, arr
],
84 describe("isArrayIndexString", () => {
85 it("[[Call]] returns false for nonstrings", () => {
86 assertStrictEquals(isArrayIndexString(1), false);
89 it("[[Call]] returns false for noncanonical strings", () => {
90 assertStrictEquals(isArrayIndexString(""), false);
91 assertStrictEquals(isArrayIndexString("01"), false);
92 assertStrictEquals(isArrayIndexString("9007199254740993"), false);
95 it("[[Call]] returns false for nonfinite numbers", () => {
96 assertStrictEquals(isArrayIndexString("NaN"), false);
97 assertStrictEquals(isArrayIndexString("Infinity"), false);
98 assertStrictEquals(isArrayIndexString("-Infinity"), false);
101 it("[[Call]] returns false for negative numbers", () => {
102 assertStrictEquals(isArrayIndexString("-0"), false);
103 assertStrictEquals(isArrayIndexString("-1"), false);
106 it("[[Call]] returns false for nonintegers", () => {
107 assertStrictEquals(isArrayIndexString("0.25"), false);
108 assertStrictEquals(isArrayIndexString("1.1"), false);
111 it("[[Call]] returns false for numbers greater than or equal to -1 >>> 0", () => {
112 assertStrictEquals(isArrayIndexString(String(-1 >>> 0)), false);
114 isArrayIndexString(String((-1 >>> 0) + 1)),
119 it("[[Call]] returns true for array lengths less than -1 >>> 0", () => {
120 assertStrictEquals(isArrayIndexString("0"), true);
122 isArrayIndexString(String((-1 >>> 0) - 1)),
128 describe("isArraylikeObject", () => {
129 it("[[Call]] returns false for primitives", () => {
130 assertStrictEquals(isArraylikeObject("failure"), false);
133 it("[[Call]] returns false if length throws", () => {
144 it("[[Call]] returns false if length is not a number and cannot be converted to one", () => {
145 assertStrictEquals(isArraylikeObject({ length
: 1n
}), false);
148 it("[[Call]] returns true if length is convertable to a number", () => {
149 assertStrictEquals(isArraylikeObject({ length
: -0 }), true);
150 assertStrictEquals(isArraylikeObject({ length
: 1 }), true);
151 assertStrictEquals(isArraylikeObject({ length
: -1.25 }), true);
153 isArraylikeObject({ length
: 9007199254740992 }),
156 assertStrictEquals(isArraylikeObject({ length
: Infinity
}), true);
157 assertStrictEquals(isArraylikeObject({ length
: "success" }), true);
161 describe("isCollection", () => {
162 it("[[Call]] returns false for primitives", () => {
163 assertStrictEquals(isCollection("failure"), false);
166 it("[[Call]] returns false if length throws", () => {
177 it("[[Call]] returns false if length is not an integer index and cannot be converted to one", () => {
179 isCollection({ length
: -1, [Symbol
.isConcatSpreadable
]: true }),
185 [Symbol
.isConcatSpreadable
]: true,
191 length
: 9007199254740992,
192 [Symbol
.isConcatSpreadable
]: true,
198 it("[[Call]] returns true if length is an integer index and the object is concat spreadable", () => {
200 isCollection({ length
: 1, [Symbol
.isConcatSpreadable
]: true }),
204 isCollection({ length
: 0, [Symbol
.isConcatSpreadable
]: true }),
209 length
: 9007199254740991,
210 [Symbol
.isConcatSpreadable
]: true,
216 it("[[Call]] returns true if length can be converted to an index without throwing an error and the object is concat spreadable", () => {
218 isCollection({ length
: -0, [Symbol
.isConcatSpreadable
]: true }),
222 isCollection({ length
: NaN
, [Symbol
.isConcatSpreadable
]: true }),
228 describe("isConcatSpreadable", () => {
229 it("[[Call]] returns false for primitives", () => {
230 assertStrictEquals(isConcatSpreadable("failure"), false);
233 it("[[Call]] returns false if [Symbol.isConcatSpreadable] is null or false", () => {
236 Object
.assign([], { [Symbol
.isConcatSpreadable
]: null }),
242 Object
.assign([], { [Symbol
.isConcatSpreadable
]: false }),
248 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is undefined and the object is an array", () => {
251 Object
.assign([], { [Symbol
.isConcatSpreadable
]: undefined }),
257 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is true", () => {
259 isConcatSpreadable({ [Symbol
.isConcatSpreadable
]: true }),
265 describe("isIntegerIndexString", () => {
266 it("[[Call]] returns false for nonstrings", () => {
267 assertStrictEquals(isIntegerIndexString(1), false);
270 it("[[Call]] returns false for noncanonical strings", () => {
271 assertStrictEquals(isIntegerIndexString(""), false);
272 assertStrictEquals(isIntegerIndexString("01"), false);
274 isIntegerIndexString("9007199254740993"),
279 it("[[Call]] returns false for nonfinite numbers", () => {
280 assertStrictEquals(isIntegerIndexString("NaN"), false);
281 assertStrictEquals(isIntegerIndexString("Infinity"), false);
282 assertStrictEquals(isIntegerIndexString("-Infinity"), false);
285 it("[[Call]] returns false for negative numbers", () => {
286 assertStrictEquals(isIntegerIndexString("-0"), false);
287 assertStrictEquals(isIntegerIndexString("-1"), false);
290 it("[[Call]] returns false for nonintegers", () => {
291 assertStrictEquals(isIntegerIndexString("0.25"), false);
292 assertStrictEquals(isIntegerIndexString("1.1"), false);
295 it("[[Call]] returns false for numbers greater than or equal to 2 ** 53", () => {
297 isIntegerIndexString("9007199254740992"),
302 it("[[Call]] returns true for safe canonical integer strings", () => {
303 assertStrictEquals(isIntegerIndexString("0"), true);
304 assertStrictEquals(isIntegerIndexString("9007199254740991"), true);
This page took 0.065202 seconds and 3 git commands to generate.