UNPKG

3.12 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.replace = exports.move = exports.splice = exports.reduce = exports.map = exports.filter = exports.concurrentFilter = exports.conform = void 0;
4function conform(t) {
5 if (typeof t === 'undefined') {
6 return [];
7 }
8 if (!Array.isArray(t)) {
9 return [t];
10 }
11 return t;
12}
13exports.conform = conform;
14async function concurrentFilter(array, callback) {
15 const mapper = async (v) => [v, await callback(v)];
16 const mapped = await Promise.all(array.map(mapper));
17 return mapped
18 .filter(([, f]) => f)
19 .map(([v]) => v);
20}
21exports.concurrentFilter = concurrentFilter;
22async function filter(array, callback) {
23 const initial = [];
24 return reduce(array, async (acc, v, i, arr) => {
25 if (await callback(v, i, arr)) {
26 acc.push(v);
27 }
28 return acc;
29 }, initial);
30}
31exports.filter = filter;
32async function map(array, callback) {
33 const initial = [];
34 return reduce(array, async (acc, v, i, arr) => {
35 acc.push(await callback(v, i, arr));
36 return acc;
37 }, initial);
38}
39exports.map = map;
40async function reduce(array, callback, initialValue) {
41 const hadInitialValue = typeof initialValue === 'undefined';
42 const startingIndex = hadInitialValue ? 1 : 0;
43 if (typeof initialValue === 'undefined') {
44 if (array.length === 0) {
45 throw new TypeError('Reduce of empty array with no initial value');
46 }
47 initialValue = array[0];
48 }
49 let value = initialValue;
50 for (let i = startingIndex; i < array.length; i++) {
51 const v = await callback(value, array[i], i, array);
52 value = v;
53 }
54 return value;
55}
56exports.reduce = reduce;
57/**
58 * Splice an array.
59 *
60 * This function will return a new array with the standard splice behavior
61 * applied. Unlike the standard array splice, the array of removed items is not
62 * returned.
63 */
64function splice(array, start, deleteCount = array.length - start, ...items) {
65 const result = [...array];
66 result.splice(start, deleteCount, ...items);
67 return result;
68}
69exports.splice = splice;
70/**
71 * Move an item in an array by index.
72 *
73 * This function will return a new array with the item in the `fromIndex`
74 * position moved to the `toIndex` position. If `fromIndex` or `toIndex` are
75 * out of bounds, the array items remain unmoved.
76 */
77function move(array, fromIndex, toIndex) {
78 const element = array[fromIndex];
79 if (fromIndex < 0 || toIndex < 0 || fromIndex >= array.length || toIndex >= array.length) {
80 return [...array];
81 }
82 return splice(splice(array, fromIndex, 1), toIndex, 0, element);
83}
84exports.move = move;
85/**
86 * Replace an item in an array by index.
87 *
88 * This function will return a new array with the item in the `index` position
89 * replaced with `item`. If `index` is out of bounds, the item is not replaced.
90 */
91function replace(array, index, item) {
92 if (index < 0 || index > array.length) {
93 return [...array];
94 }
95 return splice(array, index, 1, item);
96}
97exports.replace = replace;