]>
Lady’s Gitweb - Pisces/blob - collection.js
03326a6cd0de048259206c4310cec6f73d8d2d9e
1 // ♓🌟 Piscēs ∷ collection.js
2 // ====================================================================
4 // Copyright © 2020–2023 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/>.
10 import { call
, createCallableFunction
} from "./function.js";
11 import { toIndex
, type
} from "./value.js";
13 const { prototype: arrayPrototype
} = Array
;
16 /** Returns an array of the provided values. */
19 /** Returns whether the provided value is an array. */
22 /** Returns an array created from the provided arraylike. */
27 * Returns the result of catenating the provided arraylikes into a new
28 * collection according to the algorithm of `Array::concat`.
30 export const catenate
= createCallableFunction(
31 arrayPrototype
.concat
,
36 * Copies the items in the provided object to a new location according
37 * to the algorithm of `Array::copyWithin`.
39 export const copyWithin
= createCallableFunction(
40 arrayPrototype
.copyWithin
,
44 * Fills the provided object with the provided value according to the
45 * algorithm of `Array::fill`.
47 export const fill
= createCallableFunction(arrayPrototype
.fill
);
50 * Returns the result of filtering the provided object with the
51 * provided callback, according to the algorithm of `Array::filter`.
53 export const filter
= createCallableFunction(arrayPrototype
.filter
);
56 * Returns the first index in the provided object whose value satisfies
57 * the provided callback according to the algorithm of
60 export const findIndex
= createCallableFunction(
61 arrayPrototype
.findIndex
,
65 * Returns the first indexed entry in the provided object whose value
66 * satisfies the provided callback.
68 * If a third argument is supplied, it will be used as the this value
71 export const findIndexedEntry
= (
76 let result
= undefined;
77 findItem($, (kValue
, k
, O
) => {
78 if (call(callback
, thisArg
, [kValue
, k
, O
])) {
79 // The callback succeeded.
83 // The callback failed.
91 * Returns the first indexed value in the provided object which
92 * satisfies the provided callback, according to the algorithm of
95 export const findItem
= createCallableFunction(
101 * Returns the result of flatmapping the provided value with the
102 * provided callback according to the algorithm of `Array::flatMap`.
104 export const flatmap
= createCallableFunction(
105 arrayPrototype
.flatMap
,
110 * Returns the result of flattening the provided object according to
111 * the algorithm of `Array::flat`.
113 export const flatten
= createCallableFunction(
119 * Returns the first index of the provided object with a value
120 * equivalent to the provided value according to the algorithm of
123 export const getFirstIndex
= createCallableFunction(
124 arrayPrototype
.indexOf
,
129 * Returns the item on the provided object at the provided index
130 * according to the algorithm of `Array::at`.
132 export const getItem
= createCallableFunction(
138 * Returns the last index of the provided object with a value
139 * equivalent to the provided value according to the algorithm of
140 * `Array::lastIndexOf`.
142 export const getLastIndex
= createCallableFunction(
143 arrayPrototype
.lastIndexOf
,
148 * Returns whether every indexed value in the provided object satisfies
149 * the provided function, according to the algorithm of `Array::every`.
151 export const hasEvery
= createCallableFunction(
152 arrayPrototype
.every
,
157 * Returns whether the provided object has an indexed value which
158 * satisfies the provided function, according to the algorithm of
161 export const hasSome
= createCallableFunction(
167 * Returns whether the provided object has an indexed value equivalent
168 * to the provided value according to the algorithm of
171 * ※ This algorithm treats missing values as `undefined` rather than
174 export const includes
= createCallableFunction(
175 arrayPrototype
.includes
,
179 * Returns an iterator over the indexed entries in the provided value
180 * according to the algorithm of `Array::entries`.
182 export const indexedEntries
= createCallableFunction(
183 arrayPrototype
.entries
,
188 * Returns an iterator over the indices in the provided value according
189 * to the algorithm of `Array::keys`.
191 export const indices
= createCallableFunction(
198 * Returns whether the provided object is a collection.
200 * The definition of “collection” used by Piscēs is similar to
201 * Ecmascript’s definition of an arraylike object, but it differs in
204 * - It requires the provided value to be a proper object.
206 * - It requires the `length` property to be an integer index.
208 * - It requires the object to be concat‐spreadable, meaning it must
209 * either be an array or have `.[Symbol.isConcatSpreadable]` be true.
211 export const isCollection
= ($) => {
212 if (!(type($) === "object" && "length" in $)) {
213 // The provided value is not an object or does not have a `length`.
217 toIndex($.length
); // will throw if `length` is not an index
218 return isConcatSpreadable($);
226 * Returns whether the provided value is spreadable during array
229 * This is also used to determine which things should be treated as
232 export const isConcatSpreadable
= ($) => {
233 if (type($) !== "object") {
234 // The provided value is not an object.
237 // The provided value is an object.
238 const spreadable
= $[Symbol
.isConcatSpreadable
];
239 return spreadable
!== undefined ? !!spreadable
: isArray($);
244 * Returns an iterator over the items in the provided value according
245 * to the algorithm of `Array::values`.
247 export const items
= createCallableFunction(
248 arrayPrototype
.values
,
253 * Returns the result of mapping the provided value with the provided
254 * callback according to the algorithm of `Array::map`.
256 export const map
= createCallableFunction(arrayPrototype
.map
);
259 * Pops from the provided value according to the algorithm of
262 export const pop
= createCallableFunction(arrayPrototype
.pop
);
265 * Pushes onto the provided value according to the algorithm of
268 export const push
= createCallableFunction(arrayPrototype
.push
);
271 * Returns the result of reducing the provided value with the provided
272 * callback, according to the algorithm of `Array::reduce`.
274 export const reduce
= createCallableFunction(arrayPrototype
.reduce
);
277 * Reverses the provided value according to the algorithm of
280 export const reverse
= createCallableFunction(arrayPrototype
.reverse
);
283 * Shifts the provided value according to the algorithm of
286 export const shift
= createCallableFunction(arrayPrototype
.shift
);
289 * Returns a slice of the provided value according to the algorithm of
292 export const slice
= createCallableFunction(arrayPrototype
.slice
);
295 * Sorts the provided value in‐place according to the algorithm of
298 export const sort
= createCallableFunction(arrayPrototype
.sort
);
301 * Splices into and out of the provided value according to the
302 * algorithm of `Array::splice`.
304 export const splice
= createCallableFunction(arrayPrototype
.splice
);
307 * Unshifts the provided value according to the algorithm of
310 export const unshift
= createCallableFunction(arrayPrototype
.unshift
);
This page took 0.058559 seconds and 3 git commands to generate.