]> Lady’s Gitweb - Pisces/blob - collection.js
Add arraylike functions and collection unit tests
[Pisces] / collection.js
1 // ♓🌟 Piscēs ∷ collection.js
2 // ====================================================================
3 //
4 // Copyright © 2020–2022 Lady [@ Lady’s Computer].
5 //
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/>.
9
10 import { call, makeCallable } from "./function.js";
11 import {
12 floor,
13 isIntegralNumber,
14 isNan,
15 max,
16 MAXIMUM_SAFE_INTEGRAL_NUMBER,
17 min,
18 } from "./numeric.js";
19 import { sameValue, type } from "./value.js";
20
21 export const {
22 /** Returns an array of the provided values. */
23 of: array,
24
25 /** Returns whether the provided value is an array. */
26 isArray,
27
28 /** Returns an array created from the provided arraylike. */
29 from: toArray,
30 } = Array;
31
32 /**
33 * Returns -0 if the provided argument is "-0"; returns a number
34 * representing the index if the provided argument is a canonical
35 * numeric index string; otherwise, returns undefined.
36 *
37 * There is no clamping of the numeric index, but note that numbers
38 * above 2^53 − 1 are not safe nor valid integer indices.
39 */
40 export const canonicalNumericIndexString = ($) => {
41 if (typeof $ !== "string") {
42 return undefined;
43 } else if ($ === "-0") {
44 return -0;
45 } else {
46 const n = +$;
47 return $ === `${n}` ? n : undefined;
48 }
49 };
50
51 /**
52 * Returns the result of catenating the provided arraylikes, returning
53 * a new collection according to the algorithm of Array::concat.
54 */
55 export const catenate = makeCallable(Array.prototype.concat);
56
57 /**
58 * Copies the items in the provided object to a new location according
59 * to the algorithm of Array::copyWithin.
60 */
61 export const copyWithin = makeCallable(Array.prototype.copyWithin);
62
63 /**
64 * Fills the provided object with the provided value using the
65 * algorithm of Array::fill.
66 */
67 export const fill = makeCallable(Array.prototype.fill);
68
69 /**
70 * Returns the result of filtering the provided object with the
71 * provided callback, using the algorithm of Array::filter.
72 */
73 export const filter = makeCallable(Array.prototype.filter);
74
75 /**
76 * Returns the first index in the provided object whose value satisfies
77 * the provided callback using the algorithm of Array::findIndex.
78 */
79 export const findIndex = makeCallable(Array.prototype.findIndex);
80
81 /**
82 * Returns the first indexed entry in the provided object whose value
83 * satisfies the provided callback.
84 *
85 * If a third argument is supplied, it will be used as the this value
86 * of the callback.
87 */
88 export const findIndexedEntry = (
89 $,
90 callback,
91 thisArg = undefined,
92 ) => {
93 let result = undefined;
94 findItem($, (kValue, k, O) => {
95 if (call(callback, thisArg, [kValue, k, O])) {
96 // The callback succeeded.
97 result = [k, kValue];
98 return true;
99 } else {
100 // The callback failed.
101 return false;
102 }
103 });
104 return result;
105 };
106
107 /**
108 * Returns the first indexed value in the provided object which
109 * satisfies the provided callback, using the algorithm of Array::find.
110 */
111 export const findItem = makeCallable(Array.prototype.find);
112
113 /**
114 * Returns the result of flatmapping the provided value with the
115 * provided callback using the algorithm of Array::flatMap.
116 */
117 export const flatmap = makeCallable(Array.prototype.flatMap);
118
119 /**
120 * Returns the result of flattening the provided object using the
121 * algorithm of Array::flat.
122 */
123 export const flatten = makeCallable(Array.prototype.flat);
124
125 /**
126 * Returns the first index of the provided object with a value
127 * equivalent to the provided value according to the algorithm of
128 * Array::indexOf.
129 */
130 export const getFirstIndex = makeCallable(Array.prototype.indexOf);
131
132 /**
133 * Returns the item on the provided object at the provided index using
134 * the algorithm of Array::at.
135 */
136 export const getItem = makeCallable(Array.prototype.at);
137
138 /**
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.
142 */
143 export const getLastIndex = makeCallable(Array.prototype.lastIndexOf);
144
145 /**
146 * Returns whether every indexed value in the provided object satisfies
147 * the provided function, using the algorithm of Array::every.
148 */
149 export const hasEvery = makeCallable(Array.prototype.every);
150
151 /**
152 * Returns whether the provided object has an indexed value which
153 * satisfies the provided function, using the algorithm of Array::some.
154 */
155 export const hasSome = makeCallable(Array.prototype.some);
156
157 /**
158 * Returns whether the provided object has an indexed value equivalent
159 * to the provided value according to the algorithm of Array::includes.
160 *
161 * > ☡ This algorithm treats missing values as `undefined` rather than
162 * > skipping them.
163 */
164 export const includes = makeCallable(Array.prototype.includes);
165
166 /**
167 * Returns an iterator over the indexed entries in the provided value
168 * according to the algorithm of Array::entries.
169 */
170 export const indexedEntries = makeCallable(Array.prototype.entries);
171
172 /**
173 * Returns an iterator over the indices in the provided value according
174 * to the algorithm of Array::keys.
175 */
176 export const indices = makeCallable(Array.prototype.keys);
177
178 /** Returns whether the provided value is an array index string. */
179 export const isArrayIndexString = ($) => {
180 const value = canonicalNumericIndexString($);
181 if (value !== undefined) {
182 // The provided value is a canonical numeric index string.
183 return sameValue(value, 0) || value > 0 && value < -1 >>> 0 &&
184 value === toLength(value);
185 } else {
186 // The provided value is not a canonical numeric index string.
187 return false;
188 }
189 };
190
191 /**
192 * Returns whether the provided object is a collection.
193 *
194 * The definition of “collection” used by Piscēs is similar to
195 * Ecmascript’s definition of an arraylike object, but it differs in
196 * a few ways :—
197 *
198 * - It requires the provided value to be a proper object.
199 *
200 * - It requires the `length` property to be an integer index.
201 *
202 * - It requires the object to be concat‐spreadable, meaning it must
203 * either be an array or have `[Symbol.isConcatSpreadable]` be true.
204 */
205 export const isCollection = ($) => {
206 if (!(type($) === "object" && "length" in $)) {
207 // The provided value is not an object or does not have a `length`.
208 return false;
209 } else {
210 try {
211 toIndex($.length); // will throw if `length` is not an index
212 return isConcatSpreadable($);
213 } catch {
214 return false;
215 }
216 }
217 };
218
219 /**
220 * Returns whether the provided value is spreadable during array
221 * concatenation.
222 *
223 * This is also used to determine which things should be treated as
224 * collections.
225 */
226 export const isConcatSpreadable = ($) => {
227 if (type($) !== "object") {
228 // The provided value is not an object.
229 return false;
230 } else {
231 // The provided value is an object.
232 const spreadable = $[Symbol.isConcatSpreadable];
233 return spreadable !== undefined ? !!spreadable : isArray($);
234 }
235 };
236
237 /** Returns whether the provided value is an integer index string. */
238 export const isIntegerIndexString = ($) => {
239 const value = canonicalNumericIndexString($);
240 if (value !== undefined && isIntegralNumber(value)) {
241 // The provided value is a canonical numeric index string.
242 return sameValue(value, 0) ||
243 value > 0 && value <= MAXIMUM_SAFE_INTEGRAL_NUMBER &&
244 value === toLength(value);
245 } else {
246 // The provided value is not a canonical numeric index string.
247 return false;
248 }
249 };
250
251 /**
252 * Returns an iterator over the items in the provided value according
253 * to the algorithm of Array::values.
254 */
255 export const items = makeCallable(Array.prototype.values);
256
257 /**
258 * Returns the length of the provided arraylike object.
259 *
260 * Will throw if the provided object is not arraylike.
261 *
262 * This can produce larger lengths than can actually be stored in
263 * arrays, because no such restrictions exist on arraylike methods.
264 */
265 export const lengthOfArrayLike = ({ length }) => toLength(length);
266
267 /**
268 * Returns the result of mapping the provided value with the provided
269 * callback using the algorithm of Array::map.
270 */
271 export const map = makeCallable(Array.prototype.map);
272
273 /** Pops from the provided value using the algorithm of Array::pop. */
274 export const pop = makeCallable(Array.prototype.pop);
275
276 /**
277 * Pushes onto the provided value using the algorithm of Array::push.
278 */
279 export const push = makeCallable(Array.prototype.push);
280
281 /**
282 * Returns the result of reducing the provided value with the provided
283 * callback, using the algorithm of Array::reduce.
284 */
285 export const reduce = makeCallable(Array.prototype.reduce);
286
287 /**
288 * Reverses the provided value using the algorithm of Array::reverse.
289 */
290 export const reverse = makeCallable(Array.prototype.reverse);
291
292 /** Shifts the provided value using the algorithm of Array::shift. */
293 export const shift = makeCallable(Array.prototype.shift);
294
295 /**
296 * Returns a slice of the provided value using the algorithm of
297 * Array::slice.
298 */
299 export const slice = makeCallable(Array.prototype.slice);
300
301 /**
302 * Sorts the provided value in‐place using the algorithm of
303 * Array::sort.
304 */
305 export const sort = makeCallable(Array.prototype.sort);
306
307 /**
308 * Splices into and out of the provided value using the algorithm of
309 * Array::splice.
310 */
311 export const splice = makeCallable(Array.prototype.splice);
312
313 /**
314 * Returns the result of converting the provided value to an array
315 * index, or throws an error if it is out of range.
316 */
317 export const toIndex = ($) => {
318 const integer = floor($);
319 if (isNan(integer) || integer == 0) {
320 // The value is zero·like.
321 return 0;
322 } else {
323 // The value is not zero·like.
324 const clamped = toLength(integer);
325 if (clamped !== integer) {
326 // Clamping the value changes it.
327 throw new RangeError(`Piscēs: Index out of range: ${$}.`);
328 } else {
329 // The value is within appropriate bounds.
330 return integer;
331 }
332 }
333 };
334
335 /** Returns the result of converting the provided value to a length. */
336 export const toLength = ($) => {
337 const len = floor($);
338 return isNan(len) || len == 0
339 ? 0
340 : max(min(len, MAXIMUM_SAFE_INTEGRAL_NUMBER), 0);
341 };
342
343 /**
344 * Unshifts the provided value using the algorithm of Array::unshift.
345 */
346 export const unshift = makeCallable(Array.prototype.unshift);
This page took 0.143052 seconds and 5 git commands to generate.