@andrew_l/toolkit 0.3.1 → 0.3.21

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/index.d.cts CHANGED
@@ -4452,7 +4452,9 @@ declare const unset: (object: any, path: lodash.PropertyPath) => boolean;
4452
4452
  *
4453
4453
  * @group Promise
4454
4454
  */
4455
- declare function asyncFilter<T>(array: T[], predicate: (value: T, index: number, array: Array<T>) => Promise<boolean> | boolean): Promise<T[]>;
4455
+ declare function asyncFilter<T>(array: T[], predicate: (value: T, index: number, array: Array<T>) => Promise<boolean> | boolean, { concurrency }?: {
4456
+ concurrency?: number | undefined;
4457
+ }): Promise<T[]>;
4456
4458
 
4457
4459
  /**
4458
4460
  * Asynchronously finds the first element in an array that satisfies the provided async predicate.
package/dist/index.d.mts CHANGED
@@ -4452,7 +4452,9 @@ declare const unset: (object: any, path: lodash.PropertyPath) => boolean;
4452
4452
  *
4453
4453
  * @group Promise
4454
4454
  */
4455
- declare function asyncFilter<T>(array: T[], predicate: (value: T, index: number, array: Array<T>) => Promise<boolean> | boolean): Promise<T[]>;
4455
+ declare function asyncFilter<T>(array: T[], predicate: (value: T, index: number, array: Array<T>) => Promise<boolean> | boolean, { concurrency }?: {
4456
+ concurrency?: number | undefined;
4457
+ }): Promise<T[]>;
4456
4458
 
4457
4459
  /**
4458
4460
  * Asynchronously finds the first element in an array that satisfies the provided async predicate.
package/dist/index.d.ts CHANGED
@@ -4452,7 +4452,9 @@ declare const unset: (object: any, path: lodash.PropertyPath) => boolean;
4452
4452
  *
4453
4453
  * @group Promise
4454
4454
  */
4455
- declare function asyncFilter<T>(array: T[], predicate: (value: T, index: number, array: Array<T>) => Promise<boolean> | boolean): Promise<T[]>;
4455
+ declare function asyncFilter<T>(array: T[], predicate: (value: T, index: number, array: Array<T>) => Promise<boolean> | boolean, { concurrency }?: {
4456
+ concurrency?: number | undefined;
4457
+ }): Promise<T[]>;
4456
4458
 
4457
4459
  /**
4458
4460
  * Asynchronously finds the first element in an array that satisfies the provided async predicate.
package/dist/index.mjs CHANGED
@@ -4750,28 +4750,6 @@ const logger = (...baseArgs) => {
4750
4750
  return instance;
4751
4751
  };
4752
4752
 
4753
- function asyncFilter(array, predicate) {
4754
- var i = 0;
4755
- var result = [];
4756
- var cooldown = nextTickIteration(10);
4757
- return new Promise((resolve, reject) => {
4758
- var processNextBatch = () => {
4759
- if (i < array.length) {
4760
- cooldown().then(() => predicate(array[i], i, array)).then((valid) => {
4761
- if (Boolean(valid)) {
4762
- result.push(array[i]);
4763
- }
4764
- i++;
4765
- setTimeout(processNextBatch, 0);
4766
- }).catch(reject);
4767
- } else {
4768
- resolve(result);
4769
- }
4770
- };
4771
- processNextBatch();
4772
- });
4773
- }
4774
-
4775
4753
  function nextTickIteration(amount, delay = "tick") {
4776
4754
  let counter = 0;
4777
4755
  const tickInterval = delay === "tick";
@@ -4791,6 +4769,45 @@ function nextTickIteration(amount, delay = "tick") {
4791
4769
  };
4792
4770
  }
4793
4771
 
4772
+ function asyncFilter(array, predicate, { concurrency = 1 } = {}) {
4773
+ concurrency = Math.max(concurrency, 1);
4774
+ if (array.length === 0) {
4775
+ return Promise.resolve([]);
4776
+ }
4777
+ var result = [];
4778
+ var currentIndex = 0;
4779
+ var completed = 0;
4780
+ var hasError = false;
4781
+ var cooldown = nextTickIteration(10);
4782
+ return new Promise((resolve, reject) => {
4783
+ var processItem = (index) => {
4784
+ if (hasError || index >= array.length) return;
4785
+ cooldown().then(() => predicate(array[index], index, array)).then((include) => {
4786
+ if (hasError) return;
4787
+ if (include) {
4788
+ result.push(index);
4789
+ }
4790
+ completed++;
4791
+ if (completed >= array.length) {
4792
+ resolve(result.toSorted((a, b) => a - b).map((idx) => array[idx]));
4793
+ } else {
4794
+ if (currentIndex < array.length) {
4795
+ processItem(currentIndex++);
4796
+ }
4797
+ }
4798
+ }).catch((error) => {
4799
+ if (!hasError) {
4800
+ hasError = true;
4801
+ reject(error);
4802
+ }
4803
+ });
4804
+ };
4805
+ for (; currentIndex < concurrency; currentIndex++) {
4806
+ processItem(currentIndex);
4807
+ }
4808
+ });
4809
+ }
4810
+
4794
4811
  function asyncFind(array, callbackfn) {
4795
4812
  var i = 0;
4796
4813
  var cooldown = nextTickIteration(10);
@@ -4815,35 +4832,36 @@ function asyncFind(array, callbackfn) {
4815
4832
 
4816
4833
  function asyncForEach(array, callbackfn, { concurrency = 1 } = {}) {
4817
4834
  concurrency = Math.max(concurrency, 1);
4818
- var i = 0;
4835
+ if (array.length === 0) {
4836
+ return Promise.resolve();
4837
+ }
4838
+ var hasError = false;
4839
+ var completed = 0;
4840
+ var currentIndex = 0;
4819
4841
  var cooldown = nextTickIteration(10);
4820
- var tasks = Array(concurrency);
4821
4842
  return new Promise((resolve, reject) => {
4822
- var processNextBatch = () => {
4823
- if (i >= array.length) {
4824
- resolve();
4825
- return;
4826
- }
4827
- cooldown().then(() => {
4828
- for (var idx = 0; idx < concurrency; idx++) {
4829
- tasks[idx] = Promise.resolve(i);
4830
- if (i < array.length) {
4831
- tasks[idx] = tasks[idx].then(
4832
- (itemIndex) => callbackfn(array[itemIndex], itemIndex, array)
4833
- );
4843
+ var processItem = (index) => {
4844
+ if (hasError || index >= array.length) return;
4845
+ cooldown().then(() => callbackfn(array[index], index, array)).then((transformed) => {
4846
+ if (hasError) return;
4847
+ completed++;
4848
+ if (completed >= array.length) {
4849
+ resolve();
4850
+ } else {
4851
+ if (currentIndex < array.length) {
4852
+ processItem(currentIndex++);
4834
4853
  }
4835
- i++;
4836
4854
  }
4837
- return Promise.all(tasks);
4838
- }).then(() => {
4839
- if (i < array.length) {
4840
- setTimeout(processNextBatch, 0);
4841
- } else {
4842
- resolve();
4855
+ }).catch((error) => {
4856
+ if (!hasError) {
4857
+ hasError = true;
4858
+ reject(error);
4843
4859
  }
4844
- }).catch(reject);
4860
+ });
4845
4861
  };
4846
- processNextBatch();
4862
+ for (; currentIndex < concurrency; currentIndex++) {
4863
+ processItem(currentIndex);
4864
+ }
4847
4865
  });
4848
4866
  }
4849
4867
 
@@ -4984,38 +5002,38 @@ class AsyncIterableQueue {
4984
5002
 
4985
5003
  function asyncMap(array, callbackfn, { concurrency = 1 } = {}) {
4986
5004
  concurrency = Math.max(concurrency, 1);
4987
- var result = [];
4988
- var i = 0;
5005
+ if (array.length === 0) {
5006
+ return Promise.resolve([]);
5007
+ }
5008
+ var hasError = false;
5009
+ var result = Array(array.length);
5010
+ var completed = 0;
5011
+ var currentIndex = 0;
4989
5012
  var cooldown = nextTickIteration(10);
4990
- var tasks = Array(concurrency);
4991
5013
  return new Promise((resolve, reject) => {
4992
- var processNextBatch = () => {
4993
- if (i >= array.length) {
4994
- resolve(result);
4995
- } else {
4996
- cooldown().then(() => {
4997
- for (var idx = 0; idx < concurrency; idx++) {
4998
- tasks[idx] = Promise.resolve(i);
4999
- if (i < array.length) {
5000
- tasks[idx] = tasks[idx].then(
5001
- (itemIndex) => callbackfn(array[itemIndex], itemIndex, array)
5002
- ).then((transformed) => {
5003
- result.push(transformed);
5004
- });
5005
- }
5006
- i++;
5007
- }
5008
- return Promise.all(tasks);
5009
- }).then(() => {
5010
- if (i < array.length) {
5011
- setTimeout(processNextBatch, 0);
5012
- } else {
5013
- resolve(result);
5014
+ var processItem = (index) => {
5015
+ if (hasError || index >= array.length) return;
5016
+ cooldown().then(() => callbackfn(array[index], index, array)).then((transformed) => {
5017
+ if (hasError) return;
5018
+ result[index] = transformed;
5019
+ completed++;
5020
+ if (completed >= array.length) {
5021
+ resolve(result);
5022
+ } else {
5023
+ if (currentIndex < array.length) {
5024
+ processItem(currentIndex++);
5014
5025
  }
5015
- }).catch(reject);
5016
- }
5026
+ }
5027
+ }).catch((error) => {
5028
+ if (!hasError) {
5029
+ hasError = true;
5030
+ reject(error);
5031
+ }
5032
+ });
5017
5033
  };
5018
- processNextBatch();
5034
+ for (; currentIndex < concurrency; currentIndex++) {
5035
+ processItem(currentIndex);
5036
+ }
5019
5037
  });
5020
5038
  }
5021
5039