]>
Lady’s Gitweb - Pisces/blob - collection.js
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 { isConcatSpreadableObject
} from "./object.js";
12 import { toIndex
, type
} from "./value.js";
14 const { prototype: arrayPrototype
} = Array
;
17 /** Returns an array of the provided values. */
20 /** Returns whether the provided value is an array. */
23 /** Returns an array created from the provided arraylike. */
28 * Returns the result of catenating the provided arraylikes into a new
29 * collection according to the algorithm of `Array::concat`.
31 export const catenate
= createCallableFunction(
32 arrayPrototype
.concat
,
37 * Copies the items in the provided object to a new location according
38 * to the algorithm of `Array::copyWithin`.
40 export const copyWithin
= createCallableFunction(
41 arrayPrototype
.copyWithin
,
45 * Fills the provided object with the provided value according to the
46 * algorithm of `Array::fill`.
48 export const fill
= createCallableFunction(arrayPrototype
.fill
);
51 * Returns the result of filtering the provided object with the
52 * provided callback, according to the algorithm of `Array::filter`.
54 export const filter
= createCallableFunction(arrayPrototype
.filter
);
57 * Returns the first index in the provided object whose value satisfies
58 * the provided callback according to the algorithm of
61 export const findIndex
= createCallableFunction(
62 arrayPrototype
.findIndex
,
66 * Returns the first indexed entry in the provided object whose value
67 * satisfies the provided callback.
69 * If a third argument is supplied, it will be used as the this value
72 export const findIndexedEntry
= (
77 let result
= undefined;
78 findItem($, (kValue
, k
, O
) => {
79 if (call(callback
, thisArg
, [kValue
, k
, O
])) {
80 // The callback succeeded.
84 // The callback failed.
92 * Returns the first indexed value in the provided object which
93 * satisfies the provided callback, according to the algorithm of
96 export const findItem
= createCallableFunction(
102 * Returns the result of flatmapping the provided value with the
103 * provided callback according to the algorithm of `Array::flatMap`.
105 export const flatmap
= createCallableFunction(
106 arrayPrototype
.flatMap
,
111 * Returns the result of flattening the provided object according to
112 * the algorithm of `Array::flat`.
114 export const flatten
= createCallableFunction(
120 * Returns the first index of the provided object with a value
121 * equivalent to the provided value according to the algorithm of
124 export const getFirstIndex
= createCallableFunction(
125 arrayPrototype
.indexOf
,
130 * Returns the item on the provided object at the provided index
131 * according to the algorithm of `Array::at`.
133 export const getItem
= createCallableFunction(
139 * Returns the last index of the provided object with a value
140 * equivalent to the provided value according to the algorithm of
141 * `Array::lastIndexOf`.
143 export const getLastIndex
= createCallableFunction(
144 arrayPrototype
.lastIndexOf
,
149 * Returns whether every indexed value in the provided object satisfies
150 * the provided function, according to the algorithm of `Array::every`.
152 export const hasEvery
= createCallableFunction(
153 arrayPrototype
.every
,
158 * Returns whether the provided object has an indexed value which
159 * satisfies the provided function, according to the algorithm of
162 export const hasSome
= createCallableFunction(
168 * Returns whether the provided object has an indexed value equivalent
169 * to the provided value according to the algorithm of
172 * ※ This algorithm treats missing values as `undefined` rather than
175 export const includes
= createCallableFunction(
176 arrayPrototype
.includes
,
180 * Returns an iterator over the indexed entries in the provided value
181 * according to the algorithm of `Array::entries`.
183 export const indexedEntries
= createCallableFunction(
184 arrayPrototype
.entries
,
189 * Returns an iterator over the indices in the provided value according
190 * to the algorithm of `Array::keys`.
192 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 isConcatSpreadableObject($);
226 * Returns an iterator over the items in the provided value according
227 * to the algorithm of `Array::values`.
229 export const items
= createCallableFunction(
230 arrayPrototype
.values
,
235 * Returns the result of mapping the provided value with the provided
236 * callback according to the algorithm of `Array::map`.
238 export const map
= createCallableFunction(arrayPrototype
.map
);
241 * Pops from the provided value according to the algorithm of
244 export const pop
= createCallableFunction(arrayPrototype
.pop
);
247 * Pushes onto the provided value according to the algorithm of
250 export const push
= createCallableFunction(arrayPrototype
.push
);
253 * Returns the result of reducing the provided value with the provided
254 * callback, according to the algorithm of `Array::reduce`.
256 export const reduce
= createCallableFunction(arrayPrototype
.reduce
);
259 * Reverses the provided value according to the algorithm of
262 export const reverse
= createCallableFunction(arrayPrototype
.reverse
);
265 * Shifts the provided value according to the algorithm of
268 export const shift
= createCallableFunction(arrayPrototype
.shift
);
271 * Returns a slice of the provided value according to the algorithm of
274 export const slice
= createCallableFunction(arrayPrototype
.slice
);
277 * Sorts the provided value in‐place according to the algorithm of
280 export const sort
= createCallableFunction(arrayPrototype
.sort
);
283 * Splices into and out of the provided value according to the
284 * algorithm of `Array::splice`.
286 export const splice
= createCallableFunction(arrayPrototype
.splice
);
289 * Unshifts the provided value according to the algorithm of
292 export const unshift
= createCallableFunction(arrayPrototype
.unshift
);
This page took 0.136082 seconds and 5 git commands to generate.