]>
Lady’s Gitweb - Pisces/blob - collection.test.js
acd90d2eaa73046c744cdf3cd6668683582fe8f0
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";
24 } from "./collection.js";
26 describe("findIndexedEntry", () => {
27 it("[[Call]] returns undefined if no matching entry exists", () => {
28 assertStrictEquals(findIndexedEntry([], () => true), void {});
29 assertStrictEquals(findIndexedEntry([1], () => false), void {});
32 it("[[Call]] returns an entry for the first match", () => {
34 findIndexedEntry([, true, false], ($) => $ ?? true),
38 findIndexedEntry(["failure", "success"], ($) => $ == "success"),
43 it("[[Call]] works on arraylike objects", () => {
45 findIndexedEntry({ 1: "success", length
: 2 }, ($) => $),
49 findIndexedEntry({ 1: "failure", length
: 1 }, ($) => $),
54 it("[[Call]] only gets the value once", () => {
55 const get1
= spy(() => true);
62 assertSpyCalls(get1
, 1);
65 it("[[Call]] passes the value, index, and this value to the callback", () => {
66 const arr
= ["failure", "success", "success"];
67 const callback
= spy(($) => $ === "success");
69 findIndexedEntry(arr
, callback
, thisArg
);
70 assertSpyCalls(callback
, 2);
71 assertSpyCall(callback
, 0, {
72 args
: ["failure", 0, arr
],
75 assertSpyCall(callback
, 1, {
76 args
: ["success", 1, arr
],
82 describe("isArraylikeObject", () => {
83 it("[[Call]] returns false for primitives", () => {
84 assertStrictEquals(isArraylikeObject("failure"), false);
87 it("[[Call]] returns false if length throws", () => {
98 it("[[Call]] returns false if length is not a number and cannot be converted to one", () => {
99 assertStrictEquals(isArraylikeObject({ length
: 1n
}), false);
102 it("[[Call]] returns true if length is convertable to a number", () => {
103 assertStrictEquals(isArraylikeObject({ length
: -0 }), true);
104 assertStrictEquals(isArraylikeObject({ length
: 1 }), true);
105 assertStrictEquals(isArraylikeObject({ length
: -1.25 }), true);
107 isArraylikeObject({ length
: 9007199254740992 }),
110 assertStrictEquals(isArraylikeObject({ length
: Infinity
}), true);
111 assertStrictEquals(isArraylikeObject({ length
: "success" }), true);
115 describe("isCollection", () => {
116 it("[[Call]] returns false for primitives", () => {
117 assertStrictEquals(isCollection("failure"), false);
120 it("[[Call]] returns false if length throws", () => {
131 it("[[Call]] returns false if length is not an integer index and cannot be converted to one", () => {
133 isCollection({ length
: -1, [Symbol
.isConcatSpreadable
]: true }),
139 [Symbol
.isConcatSpreadable
]: true,
145 length
: 9007199254740992,
146 [Symbol
.isConcatSpreadable
]: true,
152 it("[[Call]] returns true if length is an integer index and the object is concat spreadable", () => {
154 isCollection({ length
: 1, [Symbol
.isConcatSpreadable
]: true }),
158 isCollection({ length
: 0, [Symbol
.isConcatSpreadable
]: true }),
163 length
: 9007199254740991,
164 [Symbol
.isConcatSpreadable
]: true,
170 it("[[Call]] returns true if length can be converted to an index without throwing an error and the object is concat spreadable", () => {
172 isCollection({ length
: -0, [Symbol
.isConcatSpreadable
]: true }),
176 isCollection({ length
: NaN
, [Symbol
.isConcatSpreadable
]: true }),
182 describe("isConcatSpreadable", () => {
183 it("[[Call]] returns false for primitives", () => {
184 assertStrictEquals(isConcatSpreadable("failure"), false);
187 it("[[Call]] returns false if [Symbol.isConcatSpreadable] is null or false", () => {
190 Object
.assign([], { [Symbol
.isConcatSpreadable
]: null }),
196 Object
.assign([], { [Symbol
.isConcatSpreadable
]: false }),
202 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is undefined and the object is an array", () => {
205 Object
.assign([], { [Symbol
.isConcatSpreadable
]: undefined }),
211 it("[[Call]] returns true if [Symbol.isConcatSpreadable] is true", () => {
213 isConcatSpreadable({ [Symbol
.isConcatSpreadable
]: true }),
This page took 0.110444 seconds and 3 git commands to generate.