complete-common 2.12.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.
@@ -51,6 +51,20 @@ export declare function arrayRemoveAllInPlace<T>(array: T[], ...elementsToRemove
51
51
  export declare function arrayRemoveInPlace<T>(array: T[], ...elementsToRemove: readonly T[]): readonly T[];
52
52
  /** Helper function to remove all of the elements in an array in-place. */
53
53
  export declare function emptyArray(array: unknown[]): void;
54
+ /**
55
+ * Helper function to perform an asynchronous filter. The vanilla `Array.filter` method does not
56
+ * wait for promises (and treats them as truthy), so this function runs the predicate on all
57
+ * elements concurrently, awaits the results, and then filters the original array.
58
+ *
59
+ * Usage:
60
+ *
61
+ * ```ts
62
+ * const results = await filterAsync(things, async (thing) => await filterFunc(thing));
63
+ * ```
64
+ *
65
+ * (This is an abstraction around `Promise.all`.)
66
+ */
67
+ export declare function filterAsync<T>(array: readonly T[], predicate: (element: T, index: number, array: readonly T[]) => Promise<boolean>): Promise<readonly T[]>;
54
68
  /**
55
69
  * Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
56
70
  * function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
@@ -82,7 +96,7 @@ export declare function filterMap<OldT, NewT>(array: readonly OldT[], func: (ele
82
96
  *
83
97
  * (This is an abstraction around `Promise.all`.)
84
98
  */
85
- 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[]>;
86
100
  /**
87
101
  * Helper function to get a random element from the provided array.
88
102
  *
@@ -122,6 +136,23 @@ export declare function isArrayBoolean(variable: unknown): variable is boolean[]
122
136
  export declare function isArrayNumber(variable: unknown): variable is number[];
123
137
  /** Helper function to check every value of an array to see if it is a string. */
124
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[]>;
125
156
  /** Initializes an array with all elements containing the specified default value. */
126
157
  export declare function newArray<T>(length: number, value: T): readonly T[];
127
158
  /** Helper function to sum every value in an array together. */
@@ -51,6 +51,20 @@ export declare function arrayRemoveAllInPlace<T>(array: T[], ...elementsToRemove
51
51
  export declare function arrayRemoveInPlace<T>(array: T[], ...elementsToRemove: readonly T[]): readonly T[];
52
52
  /** Helper function to remove all of the elements in an array in-place. */
53
53
  export declare function emptyArray(array: unknown[]): void;
54
+ /**
55
+ * Helper function to perform an asynchronous filter. The vanilla `Array.filter` method does not
56
+ * wait for promises (and treats them as truthy), so this function runs the predicate on all
57
+ * elements concurrently, awaits the results, and then filters the original array.
58
+ *
59
+ * Usage:
60
+ *
61
+ * ```ts
62
+ * const results = await filterAsync(things, async (thing) => await filterFunc(thing));
63
+ * ```
64
+ *
65
+ * (This is an abstraction around `Promise.all`.)
66
+ */
67
+ export declare function filterAsync<T>(array: readonly T[], predicate: (element: T, index: number, array: readonly T[]) => Promise<boolean>): Promise<readonly T[]>;
54
68
  /**
55
69
  * Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
56
70
  * function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
@@ -82,7 +96,7 @@ export declare function filterMap<OldT, NewT>(array: readonly OldT[], func: (ele
82
96
  *
83
97
  * (This is an abstraction around `Promise.all`.)
84
98
  */
85
- 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[]>;
86
100
  /**
87
101
  * Helper function to get a random element from the provided array.
88
102
  *
@@ -122,6 +136,23 @@ export declare function isArrayBoolean(variable: unknown): variable is boolean[]
122
136
  export declare function isArrayNumber(variable: unknown): variable is number[];
123
137
  /** Helper function to check every value of an array to see if it is a string. */
124
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[]>;
125
156
  /** Initializes an array with all elements containing the specified default value. */
126
157
  export declare function newArray<T>(length: number, value: T): readonly T[];
127
158
  /** Helper function to sum every value in an array together. */
@@ -51,6 +51,20 @@ export declare function arrayRemoveAllInPlace<T>(array: T[], ...elementsToRemove
51
51
  export declare function arrayRemoveInPlace<T>(array: T[], ...elementsToRemove: readonly T[]): readonly T[];
52
52
  /** Helper function to remove all of the elements in an array in-place. */
53
53
  export declare function emptyArray(array: unknown[]): void;
54
+ /**
55
+ * Helper function to perform an asynchronous filter. The vanilla `Array.filter` method does not
56
+ * wait for promises (and treats them as truthy), so this function runs the predicate on all
57
+ * elements concurrently, awaits the results, and then filters the original array.
58
+ *
59
+ * Usage:
60
+ *
61
+ * ```ts
62
+ * const results = await filterAsync(things, async (thing) => await filterFunc(thing));
63
+ * ```
64
+ *
65
+ * (This is an abstraction around `Promise.all`.)
66
+ */
67
+ export declare function filterAsync<T>(array: readonly T[], predicate: (element: T, index: number, array: readonly T[]) => Promise<boolean>): Promise<readonly T[]>;
54
68
  /**
55
69
  * Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
56
70
  * function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
@@ -82,7 +96,7 @@ export declare function filterMap<OldT, NewT>(array: readonly OldT[], func: (ele
82
96
  *
83
97
  * (This is an abstraction around `Promise.all`.)
84
98
  */
85
- 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[]>;
86
100
  /**
87
101
  * Helper function to get a random element from the provided array.
88
102
  *
@@ -122,6 +136,23 @@ export declare function isArrayBoolean(variable: unknown): variable is boolean[]
122
136
  export declare function isArrayNumber(variable: unknown): variable is number[];
123
137
  /** Helper function to check every value of an array to see if it is a string. */
124
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[]>;
125
156
  /** Initializes an array with all elements containing the specified default value. */
126
157
  export declare function newArray<T>(length: number, value: T): readonly T[];
127
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,KAAK,EAAE,OAAO,EAAE,GAAG,IAAI,CAEjD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAClC,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,IAAI,EAAE,CAAC,OAAO,EAAE,IAAI,KAAK,I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a,EAAE,YAAY,CAAC,CAAC,CAAC,GAC7B,aAAa,IAAI,YAAY,CAG/B;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC3B,KAAK,EAAE,SAAS,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
@@ -188,6 +188,12 @@ function arrayRemoveInPlace(array, ...elementsToRemove) {
188
188
  function emptyArray(array) {
189
189
  array.splice(0);
190
190
  }
191
+ async function filterAsync(array, predicate) {
192
+ const results = await Promise.all(
193
+ array.map(async (element, index) => await predicate(element, index, array))
194
+ );
195
+ return array.filter((_, index) => results[index] === true);
196
+ }
191
197
  function filterMap(array, func) {
192
198
  const filteredArray = [];
193
199
  for (const element of array) {
@@ -199,7 +205,9 @@ function filterMap(array, func) {
199
205
  return filteredArray;
200
206
  }
201
207
  async function filterMapAsync(array, func) {
202
- const promises = array.map(async (element) => await func(element));
208
+ const promises = array.map(
209
+ async (element, index) => await func(element, index, array)
210
+ );
203
211
  const results = await Promise.all(promises);
204
212
  return results.filter((item) => item !== void 0);
205
213
  }
@@ -254,6 +262,12 @@ function isArrayString(variable) {
254
262
  }
255
263
  return variable.every((element) => typeof element === "string");
256
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
+ }
257
271
  function newArray(length, value) {
258
272
  return Array.from({ length }, () => value);
259
273
  }
@@ -626,6 +640,7 @@ exports.copySet = copySet;
626
640
  exports.eRange = eRange;
627
641
  exports.emptyArray = emptyArray;
628
642
  exports.escapeHTMLCharacters = escapeHTMLCharacters;
643
+ exports.filterAsync = filterAsync;
629
644
  exports.filterMap = filterMap;
630
645
  exports.filterMapAsync = filterMapAsync;
631
646
  exports.getElapsedSeconds = getElapsedSeconds;
@@ -658,6 +673,7 @@ exports.isSemanticVersion = isSemanticVersion;
658
673
  exports.isUpperCase = isUpperCase;
659
674
  exports.kebabCaseToCamelCase = kebabCaseToCamelCase;
660
675
  exports.kebabCaseToPascalCase = kebabCaseToPascalCase;
676
+ exports.mapAsync = mapAsync;
661
677
  exports.mapFilter = mapFilter;
662
678
  exports.mapFind = mapFind;
663
679
  exports.newArray = newArray;
package/dist/index.mjs CHANGED
@@ -186,6 +186,12 @@ function arrayRemoveInPlace(array, ...elementsToRemove) {
186
186
  function emptyArray(array) {
187
187
  array.splice(0);
188
188
  }
189
+ async function filterAsync(array, predicate) {
190
+ const results = await Promise.all(
191
+ array.map(async (element, index) => await predicate(element, index, array))
192
+ );
193
+ return array.filter((_, index) => results[index] === true);
194
+ }
189
195
  function filterMap(array, func) {
190
196
  const filteredArray = [];
191
197
  for (const element of array) {
@@ -197,7 +203,9 @@ function filterMap(array, func) {
197
203
  return filteredArray;
198
204
  }
199
205
  async function filterMapAsync(array, func) {
200
- const promises = array.map(async (element) => await func(element));
206
+ const promises = array.map(
207
+ async (element, index) => await func(element, index, array)
208
+ );
201
209
  const results = await Promise.all(promises);
202
210
  return results.filter((item) => item !== void 0);
203
211
  }
@@ -252,6 +260,12 @@ function isArrayString(variable) {
252
260
  }
253
261
  return variable.every((element) => typeof element === "string");
254
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
+ }
255
269
  function newArray(length, value) {
256
270
  return Array.from({ length }, () => value);
257
271
  }
@@ -591,4 +605,4 @@ function* tupleKeys(tuple) {
591
605
 
592
606
  const ReadonlyMap = Map;
593
607
 
594
- 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, 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "complete-common",
3
- "version": "2.12.0",
3
+ "version": "2.14.0",
4
4
  "description": "Helper functions for TypeScript projects.",
5
5
  "homepage": "https://complete-ts.github.io/",
6
6
  "bugs": {
@@ -35,7 +35,7 @@
35
35
  "complete-node": "16.2.0",
36
36
  "eslint-plugin-sort-exports": "0.9.1",
37
37
  "typescript": "5.9.3",
38
- "typescript-eslint": "8.51.0",
38
+ "typescript-eslint": "8.52.0",
39
39
  "unbuild": "3.6.1"
40
40
  }
41
41
  }
@@ -136,6 +136,34 @@ export function emptyArray(array: unknown[]): void {
136
136
  array.splice(0);
137
137
  }
138
138
 
139
+ /**
140
+ * Helper function to perform an asynchronous filter. The vanilla `Array.filter` method does not
141
+ * wait for promises (and treats them as truthy), so this function runs the predicate on all
142
+ * elements concurrently, awaits the results, and then filters the original array.
143
+ *
144
+ * Usage:
145
+ *
146
+ * ```ts
147
+ * const results = await filterAsync(things, async (thing) => await filterFunc(thing));
148
+ * ```
149
+ *
150
+ * (This is an abstraction around `Promise.all`.)
151
+ */
152
+ export async function filterAsync<T>(
153
+ array: readonly T[],
154
+ predicate: (
155
+ element: T,
156
+ index: number,
157
+ array: readonly T[],
158
+ ) => Promise<boolean>,
159
+ ): Promise<readonly T[]> {
160
+ const results = await Promise.all(
161
+ array.map(async (element, index) => await predicate(element, index, array)),
162
+ );
163
+
164
+ return array.filter((_, index) => results[index] === true);
165
+ }
166
+
139
167
  /**
140
168
  * Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
141
169
  * function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
@@ -184,9 +212,15 @@ export function filterMap<OldT, NewT>(
184
212
  */
185
213
  export async function filterMapAsync<OldT, NewT>(
186
214
  array: readonly OldT[],
187
- func: (element: OldT) => Promise<NewT | undefined>,
215
+ func: (
216
+ element: OldT,
217
+ index: number,
218
+ array: readonly OldT[],
219
+ ) => Promise<NewT | undefined>,
188
220
  ): Promise<readonly NewT[]> {
189
- const promises = array.map(async (element) => await func(element));
221
+ const promises = array.map(
222
+ async (element, index) => await func(element, index, array),
223
+ );
190
224
  const results = await Promise.all(promises);
191
225
  return results.filter((item) => item !== undefined);
192
226
  }
@@ -300,6 +334,32 @@ export function isArrayString(variable: unknown): variable is string[] {
300
334
  return variable.every((element) => typeof element === "string");
301
335
  }
302
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
+
303
363
  /** Initializes an array with all elements containing the specified default value. */
304
364
  export function newArray<T>(length: number, value: T): readonly T[] {
305
365
  return Array.from({ length }, () => value);