]>
Lady’s Gitweb - Pisces/blob - collection.test.js
7c242cc987023ab23d35f01dbbf7c9809a6336b7
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";
25 } from "./collection.js";
27 describe("findIndexedEntry", () => {
28 it("[[Call]] returns undefined if no matching entry exists", () => {
29 assertStrictEquals(findIndexedEntry([], () => true), void {});
30 assertStrictEquals(findIndexedEntry([1], () => false), void {});
33 it("[[Call]] returns an entry for the first match", () => {
35 findIndexedEntry([, true, false], ($) => $ ?? true),
39 findIndexedEntry(["failure", "success"], ($) => $ == "success"),
44 it("[[Call]] works on arraylike objects", () => {
46 findIndexedEntry({ 1: "success", length
: 2 }, ($) => $),
50 findIndexedEntry({ 1: "failure", length
: 1 }, ($) => $),
55 it("[[Call]] only gets the value once", () => {
56 const get1
= spy(() => true);
63 assertSpyCalls(get1
, 1);
66 it("[[Call]] passes the value, index, and this value to the callback", () => {
67 const arr
= ["failure", "success", "success"];
68 const callback
= spy(($) => $ === "success");
70 findIndexedEntry(arr
, callback
, thisArg
);
71 assertSpyCalls(callback
, 2);
72 assertSpyCall(callback
, 0, {
73 args
: ["failure", 0, arr
],
76 assertSpyCall(callback
, 1, {
77 args
: ["success", 1, arr
],
83 describe("isArrayIndexString", () => {
84 it("[[Call]] returns false for nonstrings", () => {
85 assertStrictEquals(isArrayIndexString(1), false);
88 it("[[Call]] returns false for noncanonical strings", () => {
89 assertStrictEquals(isArrayIndexString(""), false);
90 assertStrictEquals(isArrayIndexString("01"), false);
91 assertStrictEquals(isArrayIndexString("9007199254740993"), false);
94 it("[[Call]] returns false for nonfinite numbers", () => {
95 assertStrictEquals(isArrayIndexString("NaN"), false);
96 assertStrictEquals(isArrayIndexString("Infinity"), false);
97 assertStrictEquals(isArrayIndexString("-Infinity"), false);
100 it("[[Call]] returns false for negative numbers", () => {
101 assertStrictEquals(isArrayIndexString("-0"), false);
102 assertStrictEquals(isArrayIndexString("-1"), false);
105 it("[[Call]] returns false for nonintegers", () => {
106 assertStrictEquals(isArrayIndexString("0.25"), false);
107 assertStrictEquals(isArrayIndexString("1.1"), false);
110 it("[[Call]] returns false for numbers greater than or equal to -1 >>> 0", () => {
111 assertStrictEquals(isArrayIndexString(String(-1 >>> 0)), false);
113 isArrayIndexString(String((-1 >>> 0) + 1)),
118 it("[[Call]] returns true for array lengths less than -1 >>> 0", () => {
119 assertStrictEquals(isArrayIndexString("0"), true);
121 isArrayIndexString(String((-1 >>> 0) - 1)),
127 describe("isArraylikeObject", () => {
128 it("[[Call]] returns false for primitives", () => {
129 assertStrictEquals(isArraylikeObject("failure"), false);
132 it("[[Call]] returns false if length throws", () => {
143 it("[[Call]] returns false if length is not a number and cannot be converted to one", () => {
144 assertStrictEquals(isArraylikeObject({ length
: 1n
}), false);
147 it("[[Call]] returns true if length is convertable to a number", () => {
148 assertStrictEquals(isArraylikeObject({ length
: -0 }), true);
149 assertStrictEquals(isArraylikeObject({ length
: 1 }), true);
150 assertStrictEquals(isArraylikeObject({ length
: -1.25 }), true);
152 isArraylikeObject({ length
: 9007199254740992 }),
155 assertStrictEquals(isArraylikeObject({ length
: Infinity
}), true);
156 assertStrictEquals(isArraylikeObject({ length
: "success" }), true);
160 describe("isCollection", () => {
161 it("[[Call]] returns false for primitives", () => {
162 assertStrictEquals(isCollection("failure"), false);
165 it("[[Call]] returns false if length throws", () => {
176 it("[[Call]] returns false if length is not an integer index and cannot be converted to one", () => {
178 isCollection({ length
: -1, [Symbol
.isConcatSpreadable
]: true }),
184 [Symbol
.isConcatSpreadable
]: true,
190 length
: 9007199254740992,
191 [Symbol
.isConcatSpreadable
]: true,
197 it("[[Call]] returns true if length is an integer index and the object is concat spreadable", () => {
199 isCollection({ length
: 1, [Symbol
.isConcatSpreadable
]: true }),
203 isCollection({ length
: 0, [Symbol
.isConcatSpreadable
]: true }),
208 length
: 9007199254740991,
209 [Symbol
.isConcatSpreadable
]: true,
215 it("[[Call]] returns true if length can be converted to an index without throwing an error and the object is concat spreadable", () => {
217 isCollection({ length
: -0, [Symbol
.isConcatSpreadable
]: true }),
221 isCollection({ length
: NaN
, [Symbol
.isConcatSpreadable
]: true }),
227 describe("isConcatSpreadable", () => {
228 it("[[Call]] returns false for primitives", () => {
229 assertStrictEquals(isConcatSpreadable("failure"), false);
232 it("[[Call]] returns false if [Symbol.isConcatSpreadable] is null or false", () => {
235 Object
.assign([], { [Symbol
.isConcatSpreadable
]: null }),
241 Object
.assign([], { [Symbol
.isConcatSpreadable
]: false }),
247 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is undefined and the object is an array", () => {
250 Object
.assign([], { [Symbol
.isConcatSpreadable
]: undefined }),
256 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is true", () => {
258 isConcatSpreadable({ [Symbol
.isConcatSpreadable
]: true }),
This page took 0.232446 seconds and 3 git commands to generate.