@andrew_l/toolkit 0.3.2 → 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.cjs CHANGED
@@ -4761,28 +4761,6 @@ const logger = (...baseArgs) => {
4761
4761
  return instance;
4762
4762
  };
4763
4763
 
4764
- function asyncFilter(array, predicate) {
4765
- var i = 0;
4766
- var result = [];
4767
- var cooldown = nextTickIteration(10);
4768
- return new Promise((resolve, reject) => {
4769
- var processNextBatch = () => {
4770
- if (i < array.length) {
4771
- cooldown().then(() => predicate(array[i], i, array)).then((valid) => {
4772
- if (Boolean(valid)) {
4773
- result.push(array[i]);
4774
- }
4775
- i++;
4776
- setTimeout(processNextBatch, 0);
4777
- }).catch(reject);
4778
- } else {
4779
- resolve(result);
4780
- }
4781
- };
4782
- processNextBatch();
4783
- });
4784
- }
4785
-
4786
4764
  function nextTickIteration(amount, delay = "tick") {
4787
4765
  let counter = 0;
4788
4766
  const tickInterval = delay === "tick";
@@ -4802,6 +4780,45 @@ function nextTickIteration(amount, delay = "tick") {
4802
4780
  };
4803
4781
  }
4804
4782
 
4783
+ function asyncFilter(array, predicate, { concurrency = 1 } = {}) {
4784
+ concurrency = Math.max(concurrency, 1);
4785
+ if (array.length === 0) {
4786
+ return Promise.resolve([]);
4787
+ }
4788
+ var result = [];
4789
+ var currentIndex = 0;
4790
+ var completed = 0;
4791
+ var hasError = false;
4792
+ var cooldown = nextTickIteration(10);
4793
+ return new Promise((resolve, reject) => {
4794
+ var processItem = (index) => {
4795
+ if (hasError || index >= array.length) return;
4796
+ cooldown().then(() => predicate(array[index], index, array)).then((include) => {
4797
+ if (hasError) return;
4798
+ if (include) {
4799
+ result.push(index);
4800
+ }
4801
+ completed++;
4802
+ if (completed >= array.length) {
4803
+ resolve(result.toSorted((a, b) => a - b).map((idx) => array[idx]));
4804
+ } else {
4805
+ if (currentIndex < array.length) {
4806
+ processItem(currentIndex++);
4807
+ }
4808
+ }
4809
+ }).catch((error) => {
4810
+ if (!hasError) {
4811
+ hasError = true;
4812
+ reject(error);
4813
+ }
4814
+ });
4815
+ };
4816
+ for (; currentIndex < concurrency; currentIndex++) {
4817
+ processItem(currentIndex);
4818
+ }
4819
+ });
4820
+ }
4821
+
4805
4822
  function asyncFind(array, callbackfn) {
4806
4823
  var i = 0;
4807
4824
  var cooldown = nextTickIteration(10);
@@ -4826,35 +4843,36 @@ function asyncFind(array, callbackfn) {
4826
4843
 
4827
4844
  function asyncForEach(array, callbackfn, { concurrency = 1 } = {}) {
4828
4845
  concurrency = Math.max(concurrency, 1);
4829
- var i = 0;
4846
+ if (array.length === 0) {
4847
+ return Promise.resolve();
4848
+ }
4849
+ var hasError = false;
4850
+ var completed = 0;
4851
+ var currentIndex = 0;
4830
4852
  var cooldown = nextTickIteration(10);
4831
- var tasks = Array(concurrency);
4832
4853
  return new Promise((resolve, reject) => {
4833
- var processNextBatch = () => {
4834
- if (i >= array.length) {
4835
- resolve();
4836
- return;
4837
- }
4838
- cooldown().then(() => {
4839
- for (var idx = 0; idx < concurrency; idx++) {
4840
- tasks[idx] = Promise.resolve(i);
4841
- if (i < array.length) {
4842
- tasks[idx] = tasks[idx].then(
4843
- (itemIndex) => callbackfn(array[itemIndex], itemIndex, array)
4844
- );
4854
+ var processItem = (index) => {
4855
+ if (hasError || index >= array.length) return;
4856
+ cooldown().then(() => callbackfn(array[index], index, array)).then((transformed) => {
4857
+ if (hasError) return;
4858
+ completed++;
4859
+ if (completed >= array.length) {
4860
+ resolve();
4861
+ } else {
4862
+ if (currentIndex < array.length) {
4863
+ processItem(currentIndex++);
4845
4864
  }
4846
- i++;
4847
4865
  }
4848
- return Promise.all(tasks);
4849
- }).then(() => {
4850
- if (i < array.length) {
4851
- setTimeout(processNextBatch, 0);
4852
- } else {
4853
- resolve();
4866
+ }).catch((error) => {
4867
+ if (!hasError) {
4868
+ hasError = true;
4869
+ reject(error);
4854
4870
  }
4855
- }).catch(reject);
4871
+ });
4856
4872
  };
4857
- processNextBatch();
4873
+ for (; currentIndex < concurrency; currentIndex++) {
4874
+ processItem(currentIndex);
4875
+ }
4858
4876
  });
4859
4877
  }
4860
4878
 
@@ -4995,38 +5013,38 @@ class AsyncIterableQueue {
4995
5013
 
4996
5014
  function asyncMap(array, callbackfn, { concurrency = 1 } = {}) {
4997
5015
  concurrency = Math.max(concurrency, 1);
4998
- var result = [];
4999
- var i = 0;
5016
+ if (array.length === 0) {
5017
+ return Promise.resolve([]);
5018
+ }
5019
+ var hasError = false;
5020
+ var result = Array(array.length);
5021
+ var completed = 0;
5022
+ var currentIndex = 0;
5000
5023
  var cooldown = nextTickIteration(10);
5001
- var tasks = Array(concurrency);
5002
5024
  return new Promise((resolve, reject) => {
5003
- var processNextBatch = () => {
5004
- if (i >= array.length) {
5005
- resolve(result);
5006
- } else {
5007
- cooldown().then(() => {
5008
- for (var idx = 0; idx < concurrency; idx++) {
5009
- tasks[idx] = Promise.resolve(i);
5010
- if (i < array.length) {
5011
- tasks[idx] = tasks[idx].then(
5012
- (itemIndex) => callbackfn(array[itemIndex], itemIndex, array)
5013
- ).then((transformed) => {
5014
- result.push(transformed);
5015
- });
5016
- }
5017
- i++;
5018
- }
5019
- return Promise.all(tasks);
5020
- }).then(() => {
5021
- if (i < array.length) {
5022
- setTimeout(processNextBatch, 0);
5023
- } else {
5024
- resolve(result);
5025
+ var processItem = (index) => {
5026
+ if (hasError || index >= array.length) return;
5027
+ cooldown().then(() => callbackfn(array[index], index, array)).then((transformed) => {
5028
+ if (hasError) return;
5029
+ result[index] = transformed;
5030
+ completed++;
5031
+ if (completed >= array.length) {
5032
+ resolve(result);
5033
+ } else {
5034
+ if (currentIndex < array.length) {
5035
+ processItem(currentIndex++);
5025
5036
  }
5026
- }).catch(reject);
5027
- }
5037
+ }
5038
+ }).catch((error) => {
5039
+ if (!hasError) {
5040
+ hasError = true;
5041
+ reject(error);
5042
+ }
5043
+ });
5028
5044
  };
5029
- processNextBatch();
5045
+ for (; currentIndex < concurrency; currentIndex++) {
5046
+ processItem(currentIndex);
5047
+ }
5030
5048
  });
5031
5049
  }
5032
5050