complete-common 2.16.1 → 2.17.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.
@@ -153,6 +153,17 @@ export declare function isArrayString(variable: unknown): variable is string[];
153
153
  * (This is an abstraction around `Promise.all`.)
154
154
  */
155
155
  export declare function mapAsync<T, U>(array: readonly T[], callback: (element: T, index: number, array: readonly T[]) => Promise<U>): Promise<readonly U[]>;
156
+ /**
157
+ * Helper function to perform an asynchronous map. This is the same thing as the `mapAsync`
158
+ * function, but allows for limiting the number of array elements that should be worked on at a
159
+ * time. This is useful to prevent the system from running out of memory in situations with large
160
+ * arrays and/or expensive functions.
161
+ *
162
+ * @param array The array to map.
163
+ * @param limit The maximum number of concurrent executions that should happen at once.
164
+ * @param callback The map function.
165
+ */
166
+ export declare function mapAsyncLimit<T, U>(array: readonly T[], limit: number, callback: (item: T) => Promise<U>): Promise<readonly U[]>;
156
167
  /** Initializes an array with all elements containing the specified default value. */
157
168
  export declare function newArray<T>(length: number, value: T): readonly T[];
158
169
  /** Helper function to sum every value in an array together. */
@@ -153,6 +153,17 @@ export declare function isArrayString(variable: unknown): variable is string[];
153
153
  * (This is an abstraction around `Promise.all`.)
154
154
  */
155
155
  export declare function mapAsync<T, U>(array: readonly T[], callback: (element: T, index: number, array: readonly T[]) => Promise<U>): Promise<readonly U[]>;
156
+ /**
157
+ * Helper function to perform an asynchronous map. This is the same thing as the `mapAsync`
158
+ * function, but allows for limiting the number of array elements that should be worked on at a
159
+ * time. This is useful to prevent the system from running out of memory in situations with large
160
+ * arrays and/or expensive functions.
161
+ *
162
+ * @param array The array to map.
163
+ * @param limit The maximum number of concurrent executions that should happen at once.
164
+ * @param callback The map function.
165
+ */
166
+ export declare function mapAsyncLimit<T, U>(array: readonly T[], limit: number, callback: (item: T) => Promise<U>): Promise<readonly U[]>;
156
167
  /** Initializes an array with all elements containing the specified default value. */
157
168
  export declare function newArray<T>(length: number, value: T): readonly T[];
158
169
  /** Helper function to sum every value in an array together. */
@@ -153,6 +153,17 @@ export declare function isArrayString(variable: unknown): variable is string[];
153
153
  * (This is an abstraction around `Promise.all`.)
154
154
  */
155
155
  export declare function mapAsync<T, U>(array: readonly T[], callback: (element: T, index: number, array: readonly T[]) => Promise<U>): Promise<readonly U[]>;
156
+ /**
157
+ * Helper function to perform an asynchronous map. This is the same thing as the `mapAsync`
158
+ * function, but allows for limiting the number of array elements that should be worked on at a
159
+ * time. This is useful to prevent the system from running out of memory in situations with large
160
+ * arrays and/or expensive functions.
161
+ *
162
+ * @param array The array to map.
163
+ * @param limit The maximum number of concurrent executions that should happen at once.
164
+ * @param callback The map function.
165
+ */
166
+ export declare function mapAsyncLimit<T, U>(array: readonly T[], limit: number, callback: (item: T) => Promise<U>): Promise<readonly U[]>;
156
167
  /** Initializes an array with all elements containing the specified default value. */
157
168
  export declare function newArray<T>(length: number, value: T): readonly T[];
158
169
  /** 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,wBAA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G,MAAM,CAEzD"}
package/dist/index.cjs CHANGED
@@ -276,6 +276,30 @@ async function mapAsync(array, callback) {
276
276
  );
277
277
  return await Promise.all(promises);
278
278
  }
279
+ async function mapAsyncLimit(array, limit, callback) {
280
+ if (limit < 1) {
281
+ throw new RangeError("The limit argument must be at least 1.");
282
+ }
283
+ const results = [];
284
+ let currentIndex = 0;
285
+ let hasFailed = false;
286
+ async function worker() {
287
+ while (currentIndex < array.length && !hasFailed) {
288
+ const index = currentIndex;
289
+ currentIndex++;
290
+ try {
291
+ results[index] = await callback(array[index]);
292
+ } catch (error) {
293
+ hasFailed = true;
294
+ throw error;
295
+ }
296
+ }
297
+ }
298
+ await Promise.all(
299
+ Array.from({ length: Math.min(limit, array.length) }, worker)
300
+ );
301
+ return results;
302
+ }
279
303
  function newArray(length, value) {
280
304
  return Array.from({ length }, () => value);
281
305
  }
@@ -695,6 +719,7 @@ exports.isUpperCase = isUpperCase;
695
719
  exports.kebabCaseToCamelCase = kebabCaseToCamelCase;
696
720
  exports.kebabCaseToPascalCase = kebabCaseToPascalCase;
697
721
  exports.mapAsync = mapAsync;
722
+ exports.mapAsyncLimit = mapAsyncLimit;
698
723
  exports.mapFilter = mapFilter;
699
724
  exports.mapFind = mapFind;
700
725
  exports.newArray = newArray;
package/dist/index.mjs CHANGED
@@ -274,6 +274,30 @@ async function mapAsync(array, callback) {
274
274
  );
275
275
  return await Promise.all(promises);
276
276
  }
277
+ async function mapAsyncLimit(array, limit, callback) {
278
+ if (limit < 1) {
279
+ throw new RangeError("The limit argument must be at least 1.");
280
+ }
281
+ const results = [];
282
+ let currentIndex = 0;
283
+ let hasFailed = false;
284
+ async function worker() {
285
+ while (currentIndex < array.length && !hasFailed) {
286
+ const index = currentIndex;
287
+ currentIndex++;
288
+ try {
289
+ results[index] = await callback(array[index]);
290
+ } catch (error) {
291
+ hasFailed = true;
292
+ throw error;
293
+ }
294
+ }
295
+ }
296
+ await Promise.all(
297
+ Array.from({ length: Math.min(limit, array.length) }, worker)
298
+ );
299
+ return results;
300
+ }
277
301
  function newArray(length, value) {
278
302
  return Array.from({ length }, () => value);
279
303
  }
@@ -626,4 +650,4 @@ function* tupleKeys(tuple) {
626
650
 
627
651
  const ReadonlyMap = Map;
628
652
 
629
- 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, objectMap, objectToMap, objectToReverseMap, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, titleCaseToKebabCase, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
653
+ 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, mapAsyncLimit, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToSet, objectMap, objectToMap, objectToReverseMap, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, titleCaseToKebabCase, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "complete-common",
3
- "version": "2.16.1",
3
+ "version": "2.17.0",
4
4
  "description": "Helper functions for TypeScript projects.",
5
5
  "homepage": "https://complete-ts.github.io/",
6
6
  "bugs": {
@@ -360,6 +360,51 @@ export async function mapAsync<T, U>(
360
360
  return await Promise.all(promises);
361
361
  }
362
362
 
363
+ /**
364
+ * Helper function to perform an asynchronous map. This is the same thing as the `mapAsync`
365
+ * function, but allows for limiting the number of array elements that should be worked on at a
366
+ * time. This is useful to prevent the system from running out of memory in situations with large
367
+ * arrays and/or expensive functions.
368
+ *
369
+ * @param array The array to map.
370
+ * @param limit The maximum number of concurrent executions that should happen at once.
371
+ * @param callback The map function.
372
+ */
373
+ export async function mapAsyncLimit<T, U>(
374
+ array: readonly T[],
375
+ limit: number,
376
+ callback: (item: T) => Promise<U>,
377
+ ): Promise<readonly U[]> {
378
+ if (limit < 1) {
379
+ throw new RangeError("The limit argument must be at least 1.");
380
+ }
381
+
382
+ const results: U[] = [];
383
+ let currentIndex = 0;
384
+ let hasFailed = false;
385
+
386
+ async function worker() {
387
+ // Stop processing if the queue is empty or if another worker failed.
388
+ while (currentIndex < array.length && !hasFailed) {
389
+ const index = currentIndex;
390
+ currentIndex++;
391
+ try {
392
+ // eslint-disable-next-line no-await-in-loop, @typescript-eslint/no-non-null-assertion
393
+ results[index] = await callback(array[index]!);
394
+ } catch (error) {
395
+ hasFailed = true; // Signal other workers to stop taking new items.
396
+ throw error; // Bubble the error up to Promise.all.
397
+ }
398
+ }
399
+ }
400
+
401
+ await Promise.all(
402
+ Array.from({ length: Math.min(limit, array.length) }, worker),
403
+ );
404
+
405
+ return results;
406
+ }
407
+
363
408
  /** Initializes an array with all elements containing the specified default value. */
364
409
  export function newArray<T>(length: number, value: T): readonly T[] {
365
410
  return Array.from({ length }, () => value);