complete-common 2.13.0 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/functions/array.d.cts +19 -2
- package/dist/functions/array.d.mts +19 -2
- package/dist/functions/array.d.ts +19 -2
- package/dist/functions/array.d.ts.map +1 -1
- package/dist/index.cjs +11 -2
- package/dist/index.mjs +11 -3
- package/package.json +1 -1
- package/src/functions/array.ts +40 -4
|
@@ -64,7 +64,7 @@ export declare function emptyArray(array: unknown[]): void;
|
|
|
64
64
|
*
|
|
65
65
|
* (This is an abstraction around `Promise.all`.)
|
|
66
66
|
*/
|
|
67
|
-
export declare function filterAsync<T>(array: readonly T[], predicate: (element: T) => Promise<boolean>): Promise<readonly T[]>;
|
|
67
|
+
export declare function filterAsync<T>(array: readonly T[], predicate: (element: T, index: number, array: readonly T[]) => Promise<boolean>): Promise<readonly T[]>;
|
|
68
68
|
/**
|
|
69
69
|
* Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
|
|
70
70
|
* function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
|
|
@@ -96,7 +96,7 @@ export declare function filterMap<OldT, NewT>(array: readonly OldT[], func: (ele
|
|
|
96
96
|
*
|
|
97
97
|
* (This is an abstraction around `Promise.all`.)
|
|
98
98
|
*/
|
|
99
|
-
export declare function filterMapAsync<OldT, NewT>(array: readonly OldT[], func: (element: OldT) => Promise<NewT | undefined>): Promise<readonly NewT[]>;
|
|
99
|
+
export declare function filterMapAsync<OldT, NewT>(array: readonly OldT[], func: (element: OldT, index: number, array: readonly OldT[]) => Promise<NewT | undefined>): Promise<readonly NewT[]>;
|
|
100
100
|
/**
|
|
101
101
|
* Helper function to get a random element from the provided array.
|
|
102
102
|
*
|
|
@@ -136,6 +136,23 @@ export declare function isArrayBoolean(variable: unknown): variable is boolean[]
|
|
|
136
136
|
export declare function isArrayNumber(variable: unknown): variable is number[];
|
|
137
137
|
/** Helper function to check every value of an array to see if it is a string. */
|
|
138
138
|
export declare function isArrayString(variable: unknown): variable is string[];
|
|
139
|
+
/**
|
|
140
|
+
* Helper function to perform an asynchronous map. The vanilla `Array.map` method does not wait for
|
|
141
|
+
* promises, resulting in an array of promises rather than the resolved values. This function runs
|
|
142
|
+
* the callback on all elements concurrently, awaits the results, and returns the mapped array.
|
|
143
|
+
*
|
|
144
|
+
* You can also use this function to simply run an asynchronous function on each element of an array
|
|
145
|
+
* concurrently.
|
|
146
|
+
*
|
|
147
|
+
* Usage:
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* const results = await mapAsync(things, async (thing) => await mapFunc(thing));
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* (This is an abstraction around `Promise.all`.)
|
|
154
|
+
*/
|
|
155
|
+
export declare function mapAsync<T, U>(array: readonly T[], callback: (element: T, index: number, array: readonly T[]) => Promise<U>): Promise<readonly U[]>;
|
|
139
156
|
/** Initializes an array with all elements containing the specified default value. */
|
|
140
157
|
export declare function newArray<T>(length: number, value: T): readonly T[];
|
|
141
158
|
/** Helper function to sum every value in an array together. */
|
|
@@ -64,7 +64,7 @@ export declare function emptyArray(array: unknown[]): void;
|
|
|
64
64
|
*
|
|
65
65
|
* (This is an abstraction around `Promise.all`.)
|
|
66
66
|
*/
|
|
67
|
-
export declare function filterAsync<T>(array: readonly T[], predicate: (element: T) => Promise<boolean>): Promise<readonly T[]>;
|
|
67
|
+
export declare function filterAsync<T>(array: readonly T[], predicate: (element: T, index: number, array: readonly T[]) => Promise<boolean>): Promise<readonly T[]>;
|
|
68
68
|
/**
|
|
69
69
|
* Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
|
|
70
70
|
* function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
|
|
@@ -96,7 +96,7 @@ export declare function filterMap<OldT, NewT>(array: readonly OldT[], func: (ele
|
|
|
96
96
|
*
|
|
97
97
|
* (This is an abstraction around `Promise.all`.)
|
|
98
98
|
*/
|
|
99
|
-
export declare function filterMapAsync<OldT, NewT>(array: readonly OldT[], func: (element: OldT) => Promise<NewT | undefined>): Promise<readonly NewT[]>;
|
|
99
|
+
export declare function filterMapAsync<OldT, NewT>(array: readonly OldT[], func: (element: OldT, index: number, array: readonly OldT[]) => Promise<NewT | undefined>): Promise<readonly NewT[]>;
|
|
100
100
|
/**
|
|
101
101
|
* Helper function to get a random element from the provided array.
|
|
102
102
|
*
|
|
@@ -136,6 +136,23 @@ export declare function isArrayBoolean(variable: unknown): variable is boolean[]
|
|
|
136
136
|
export declare function isArrayNumber(variable: unknown): variable is number[];
|
|
137
137
|
/** Helper function to check every value of an array to see if it is a string. */
|
|
138
138
|
export declare function isArrayString(variable: unknown): variable is string[];
|
|
139
|
+
/**
|
|
140
|
+
* Helper function to perform an asynchronous map. The vanilla `Array.map` method does not wait for
|
|
141
|
+
* promises, resulting in an array of promises rather than the resolved values. This function runs
|
|
142
|
+
* the callback on all elements concurrently, awaits the results, and returns the mapped array.
|
|
143
|
+
*
|
|
144
|
+
* You can also use this function to simply run an asynchronous function on each element of an array
|
|
145
|
+
* concurrently.
|
|
146
|
+
*
|
|
147
|
+
* Usage:
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* const results = await mapAsync(things, async (thing) => await mapFunc(thing));
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* (This is an abstraction around `Promise.all`.)
|
|
154
|
+
*/
|
|
155
|
+
export declare function mapAsync<T, U>(array: readonly T[], callback: (element: T, index: number, array: readonly T[]) => Promise<U>): Promise<readonly U[]>;
|
|
139
156
|
/** Initializes an array with all elements containing the specified default value. */
|
|
140
157
|
export declare function newArray<T>(length: number, value: T): readonly T[];
|
|
141
158
|
/** Helper function to sum every value in an array together. */
|
|
@@ -64,7 +64,7 @@ export declare function emptyArray(array: unknown[]): void;
|
|
|
64
64
|
*
|
|
65
65
|
* (This is an abstraction around `Promise.all`.)
|
|
66
66
|
*/
|
|
67
|
-
export declare function filterAsync<T>(array: readonly T[], predicate: (element: T) => Promise<boolean>): Promise<readonly T[]>;
|
|
67
|
+
export declare function filterAsync<T>(array: readonly T[], predicate: (element: T, index: number, array: readonly T[]) => Promise<boolean>): Promise<readonly T[]>;
|
|
68
68
|
/**
|
|
69
69
|
* Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
|
|
70
70
|
* function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
|
|
@@ -96,7 +96,7 @@ export declare function filterMap<OldT, NewT>(array: readonly OldT[], func: (ele
|
|
|
96
96
|
*
|
|
97
97
|
* (This is an abstraction around `Promise.all`.)
|
|
98
98
|
*/
|
|
99
|
-
export declare function filterMapAsync<OldT, NewT>(array: readonly OldT[], func: (element: OldT) => Promise<NewT | undefined>): Promise<readonly NewT[]>;
|
|
99
|
+
export declare function filterMapAsync<OldT, NewT>(array: readonly OldT[], func: (element: OldT, index: number, array: readonly OldT[]) => Promise<NewT | undefined>): Promise<readonly NewT[]>;
|
|
100
100
|
/**
|
|
101
101
|
* Helper function to get a random element from the provided array.
|
|
102
102
|
*
|
|
@@ -136,6 +136,23 @@ export declare function isArrayBoolean(variable: unknown): variable is boolean[]
|
|
|
136
136
|
export declare function isArrayNumber(variable: unknown): variable is number[];
|
|
137
137
|
/** Helper function to check every value of an array to see if it is a string. */
|
|
138
138
|
export declare function isArrayString(variable: unknown): variable is string[];
|
|
139
|
+
/**
|
|
140
|
+
* Helper function to perform an asynchronous map. The vanilla `Array.map` method does not wait for
|
|
141
|
+
* promises, resulting in an array of promises rather than the resolved values. This function runs
|
|
142
|
+
* the callback on all elements concurrently, awaits the results, and returns the mapped array.
|
|
143
|
+
*
|
|
144
|
+
* You can also use this function to simply run an asynchronous function on each element of an array
|
|
145
|
+
* concurrently.
|
|
146
|
+
*
|
|
147
|
+
* Usage:
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* const results = await mapAsync(things, async (thing) => await mapFunc(thing));
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* (This is an abstraction around `Promise.all`.)
|
|
154
|
+
*/
|
|
155
|
+
export declare function mapAsync<T, U>(array: readonly T[], callback: (element: T, index: number, array: readonly T[]) => Promise<U>): Promise<readonly U[]>;
|
|
139
156
|
/** Initializes an array with all elements containing the specified default value. */
|
|
140
157
|
export declare function newArray<T>(length: number, value: T): readonly T[];
|
|
141
158
|
/** Helper function to sum every value in an array together. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../src/functions/array.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAI7D;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,EACvC,KAAK,EAAE,aAAa,CAAC,SAAS,CAAC,EAAE,CAAC,GACjC,aAAa,CAAC,SAAS,CAAC,EAAE,CAAC,CAQ7B;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC3B,MAAM,EAAE,SAAS,CAAC,EAAE,EACpB,MAAM,EAAE,SAAS,CAAC,EAAE,GACnB,OAAO,CAST;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC3B,aAAa,EAAE,SAAS,CAAC,EAAE,EAC3B,GAAG,gBAAgB,EAAE,SAAS,CAAC,EAAE,GAChC,SAAS,CAAC,EAAE,CAWd;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAErC,KAAK,EAAE,CAAC,EAAE,EACV,GAAG,gBAAgB,EAAE,SAAS,CAAC,EAAE,GAChC,OAAO,CAeT;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAElC,KAAK,EAAE,CAAC,EAAE,EACV,GAAG,gBAAgB,EAAE,SAAS,CAAC,EAAE,GAChC,SAAS,CAAC,EAAE,CAYd;AAED,0EAA0E;AAE1E,wBAAgB,UAAU,CAAC,KAA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,CAAC,EAAE,EACnB,GAAG,cAAc,EAAE,SAAS,CAAC,EAAE,GAC9B,OAAO,CAET;AAED,uFAAuF;AACvF,wBAAgB,OAAO,CAAC,QAAQ,EAAE,OAAO,GAAG,QAAQ,IAAI,OAAO,EAAE,CAEhE;AAED,kFAAkF;AAClF,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,GAAG,QAAQ,IAAI,OAAO,EAAE,CAMvE;AAED,iFAAiF;AACjF,wBAAgB,aAAa,CAAC,QAAQ,EAAE,OAAO,GAAG,QAAQ,IAAI,MAAM,EAAE,CAMrE;AAED,iFAAiF;AACjF,wBAAgB,aAAa,CAAC,QAAQ,EAAE,OAAO,GAAG,QAAQ,IAAI,MAAM,EAAE,CAMrE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAAE,CAAC,EACjC,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,QAAQ,EAAE,CAAC,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,GACvE,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC,CAKvB;AAED,qFAAqF;AACrF,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,SAAS,CAAC,EAAE,CAElE;AAED,+DAA+D;AAC/D,wBAAgB,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAEzD"}
|
package/dist/index.cjs
CHANGED
|
@@ -190,7 +190,7 @@ function emptyArray(array) {
|
|
|
190
190
|
}
|
|
191
191
|
async function filterAsync(array, predicate) {
|
|
192
192
|
const results = await Promise.all(
|
|
193
|
-
array.map(async (element) => await predicate(element))
|
|
193
|
+
array.map(async (element, index) => await predicate(element, index, array))
|
|
194
194
|
);
|
|
195
195
|
return array.filter((_, index) => results[index] === true);
|
|
196
196
|
}
|
|
@@ -205,7 +205,9 @@ function filterMap(array, func) {
|
|
|
205
205
|
return filteredArray;
|
|
206
206
|
}
|
|
207
207
|
async function filterMapAsync(array, func) {
|
|
208
|
-
const promises = array.map(
|
|
208
|
+
const promises = array.map(
|
|
209
|
+
async (element, index) => await func(element, index, array)
|
|
210
|
+
);
|
|
209
211
|
const results = await Promise.all(promises);
|
|
210
212
|
return results.filter((item) => item !== void 0);
|
|
211
213
|
}
|
|
@@ -260,6 +262,12 @@ function isArrayString(variable) {
|
|
|
260
262
|
}
|
|
261
263
|
return variable.every((element) => typeof element === "string");
|
|
262
264
|
}
|
|
265
|
+
async function mapAsync(array, callback) {
|
|
266
|
+
const promises = array.map(
|
|
267
|
+
async (element, index) => await callback(element, index, array)
|
|
268
|
+
);
|
|
269
|
+
return await Promise.all(promises);
|
|
270
|
+
}
|
|
263
271
|
function newArray(length, value) {
|
|
264
272
|
return Array.from({ length }, () => value);
|
|
265
273
|
}
|
|
@@ -665,6 +673,7 @@ exports.isSemanticVersion = isSemanticVersion;
|
|
|
665
673
|
exports.isUpperCase = isUpperCase;
|
|
666
674
|
exports.kebabCaseToCamelCase = kebabCaseToCamelCase;
|
|
667
675
|
exports.kebabCaseToPascalCase = kebabCaseToPascalCase;
|
|
676
|
+
exports.mapAsync = mapAsync;
|
|
668
677
|
exports.mapFilter = mapFilter;
|
|
669
678
|
exports.mapFind = mapFind;
|
|
670
679
|
exports.newArray = newArray;
|
package/dist/index.mjs
CHANGED
|
@@ -188,7 +188,7 @@ function emptyArray(array) {
|
|
|
188
188
|
}
|
|
189
189
|
async function filterAsync(array, predicate) {
|
|
190
190
|
const results = await Promise.all(
|
|
191
|
-
array.map(async (element) => await predicate(element))
|
|
191
|
+
array.map(async (element, index) => await predicate(element, index, array))
|
|
192
192
|
);
|
|
193
193
|
return array.filter((_, index) => results[index] === true);
|
|
194
194
|
}
|
|
@@ -203,7 +203,9 @@ function filterMap(array, func) {
|
|
|
203
203
|
return filteredArray;
|
|
204
204
|
}
|
|
205
205
|
async function filterMapAsync(array, func) {
|
|
206
|
-
const promises = array.map(
|
|
206
|
+
const promises = array.map(
|
|
207
|
+
async (element, index) => await func(element, index, array)
|
|
208
|
+
);
|
|
207
209
|
const results = await Promise.all(promises);
|
|
208
210
|
return results.filter((item) => item !== void 0);
|
|
209
211
|
}
|
|
@@ -258,6 +260,12 @@ function isArrayString(variable) {
|
|
|
258
260
|
}
|
|
259
261
|
return variable.every((element) => typeof element === "string");
|
|
260
262
|
}
|
|
263
|
+
async function mapAsync(array, callback) {
|
|
264
|
+
const promises = array.map(
|
|
265
|
+
async (element, index) => await callback(element, index, array)
|
|
266
|
+
);
|
|
267
|
+
return await Promise.all(promises);
|
|
268
|
+
}
|
|
261
269
|
function newArray(length, value) {
|
|
262
270
|
return Array.from({ length }, () => value);
|
|
263
271
|
}
|
|
@@ -597,4 +605,4 @@ function* tupleKeys(tuple) {
|
|
|
597
605
|
|
|
598
606
|
const ReadonlyMap = Map;
|
|
599
607
|
|
|
600
|
-
export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveAllInPlace, arrayRemoveInPlace, assertArray, assertArrayBoolean, assertArrayNumber, assertArrayObject, assertArrayString, assertBoolean, assertDefined, assertEnumValue, assertInteger, assertIs, assertNotNull, assertNumber, assertObject, assertString, assertStringNotEmpty, capitalizeFirstLetter, clamp, combineSets, copySet, eRange, emptyArray, escapeHTMLCharacters, filterAsync, filterMap, filterMapAsync, getElapsedSeconds, getEnumEntries, getEnumKeys, getEnumValues, getNumConsecutiveDiacritics, getRandomArrayElement, getRandomArrayIndex, getRandomInt, hasDiacritic, hasEmoji, hasWhitespace, iRange, includes, includesAny, interfaceSatisfiesEnum, isASCII, isArray, isArrayBoolean, isArrayNumber, isArrayString, isEnumValue, isFirstLetterCapitalized, isKebabCase, isKeyOf, isLowerCase, isObject, isSemanticVersion, isUpperCase, kebabCaseToCamelCase, kebabCaseToPascalCase, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToSet, objectToMap, objectToReverseMap, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
|
|
608
|
+
export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveAllInPlace, arrayRemoveInPlace, assertArray, assertArrayBoolean, assertArrayNumber, assertArrayObject, assertArrayString, assertBoolean, assertDefined, assertEnumValue, assertInteger, assertIs, assertNotNull, assertNumber, assertObject, assertString, assertStringNotEmpty, capitalizeFirstLetter, clamp, combineSets, copySet, eRange, emptyArray, escapeHTMLCharacters, filterAsync, filterMap, filterMapAsync, getElapsedSeconds, getEnumEntries, getEnumKeys, getEnumValues, getNumConsecutiveDiacritics, getRandomArrayElement, getRandomArrayIndex, getRandomInt, hasDiacritic, hasEmoji, hasWhitespace, iRange, includes, includesAny, interfaceSatisfiesEnum, isASCII, isArray, isArrayBoolean, isArrayNumber, isArrayString, isEnumValue, isFirstLetterCapitalized, isKebabCase, isKeyOf, isLowerCase, isObject, isSemanticVersion, isUpperCase, kebabCaseToCamelCase, kebabCaseToPascalCase, mapAsync, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToSet, objectToMap, objectToReverseMap, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
|
package/package.json
CHANGED
package/src/functions/array.ts
CHANGED
|
@@ -151,10 +151,14 @@ export function emptyArray(array: unknown[]): void {
|
|
|
151
151
|
*/
|
|
152
152
|
export async function filterAsync<T>(
|
|
153
153
|
array: readonly T[],
|
|
154
|
-
predicate: (
|
|
154
|
+
predicate: (
|
|
155
|
+
element: T,
|
|
156
|
+
index: number,
|
|
157
|
+
array: readonly T[],
|
|
158
|
+
) => Promise<boolean>,
|
|
155
159
|
): Promise<readonly T[]> {
|
|
156
160
|
const results = await Promise.all(
|
|
157
|
-
array.map(async (element) => await predicate(element)),
|
|
161
|
+
array.map(async (element, index) => await predicate(element, index, array)),
|
|
158
162
|
);
|
|
159
163
|
|
|
160
164
|
return array.filter((_, index) => results[index] === true);
|
|
@@ -208,9 +212,15 @@ export function filterMap<OldT, NewT>(
|
|
|
208
212
|
*/
|
|
209
213
|
export async function filterMapAsync<OldT, NewT>(
|
|
210
214
|
array: readonly OldT[],
|
|
211
|
-
func: (
|
|
215
|
+
func: (
|
|
216
|
+
element: OldT,
|
|
217
|
+
index: number,
|
|
218
|
+
array: readonly OldT[],
|
|
219
|
+
) => Promise<NewT | undefined>,
|
|
212
220
|
): Promise<readonly NewT[]> {
|
|
213
|
-
const promises = array.map(
|
|
221
|
+
const promises = array.map(
|
|
222
|
+
async (element, index) => await func(element, index, array),
|
|
223
|
+
);
|
|
214
224
|
const results = await Promise.all(promises);
|
|
215
225
|
return results.filter((item) => item !== undefined);
|
|
216
226
|
}
|
|
@@ -324,6 +334,32 @@ export function isArrayString(variable: unknown): variable is string[] {
|
|
|
324
334
|
return variable.every((element) => typeof element === "string");
|
|
325
335
|
}
|
|
326
336
|
|
|
337
|
+
/**
|
|
338
|
+
* Helper function to perform an asynchronous map. The vanilla `Array.map` method does not wait for
|
|
339
|
+
* promises, resulting in an array of promises rather than the resolved values. This function runs
|
|
340
|
+
* the callback on all elements concurrently, awaits the results, and returns the mapped array.
|
|
341
|
+
*
|
|
342
|
+
* You can also use this function to simply run an asynchronous function on each element of an array
|
|
343
|
+
* concurrently.
|
|
344
|
+
*
|
|
345
|
+
* Usage:
|
|
346
|
+
*
|
|
347
|
+
* ```ts
|
|
348
|
+
* const results = await mapAsync(things, async (thing) => await mapFunc(thing));
|
|
349
|
+
* ```
|
|
350
|
+
*
|
|
351
|
+
* (This is an abstraction around `Promise.all`.)
|
|
352
|
+
*/
|
|
353
|
+
export async function mapAsync<T, U>(
|
|
354
|
+
array: readonly T[],
|
|
355
|
+
callback: (element: T, index: number, array: readonly T[]) => Promise<U>,
|
|
356
|
+
): Promise<readonly U[]> {
|
|
357
|
+
const promises = array.map(
|
|
358
|
+
async (element, index) => await callback(element, index, array),
|
|
359
|
+
);
|
|
360
|
+
return await Promise.all(promises);
|
|
361
|
+
}
|
|
362
|
+
|
|
327
363
|
/** Initializes an array with all elements containing the specified default value. */
|
|
328
364
|
export function newArray<T>(length: number, value: T): readonly T[] {
|
|
329
365
|
return Array.from({ length }, () => value);
|