@creejs/commons-lang 2.1.19 → 2.1.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.
@@ -1,3 +1,101 @@
1
+ class _Error extends Error {
2
+ /**
3
+ * Creates and returns a 404 Not Found error instance with the given message.
4
+ * @param {string} message - The error message to include.
5
+ * @returns {_Error} A new Error instance with status code 404.
6
+ */
7
+ static notFound (message) {
8
+ return new _Error(`Not Found: ${message}`, 404)
9
+ }
10
+
11
+ static notImpled () {
12
+ return new _Error('Not Impled Yet', 501)
13
+ }
14
+
15
+ /**
16
+ * Creates and returns a new Error instance for not supported operations.
17
+ * @param {string} message - The error message to include.
18
+ * @returns {_Error} A new Error instance with "Not Supported:" prefix and 505 status code.
19
+ */
20
+ static notSupported (message) {
21
+ return new _Error('Not Supported:' + message, 505)
22
+ }
23
+
24
+ /**
25
+ * Checks if the given error is a "Not Supported" error.
26
+ * @param {Error|_Error|{[key:string]:any}|unknown} err - The error to check
27
+ * @returns {boolean} True if the error is a Not Supported error (code 505), false otherwise
28
+ */
29
+ static isNotSupported (err) {
30
+ // @ts-ignore
31
+ return err?.code === 505
32
+ }
33
+
34
+ /**
35
+ * Error constructor with custom message and code.
36
+ * @param {string} message - Error message
37
+ * @param {number|string} code - Error code
38
+ */
39
+ constructor (message, code) {
40
+ super(message);
41
+ this.code = code;
42
+ }
43
+ }
44
+
45
+ class AggregatedError extends Error {
46
+ /**
47
+ * Checks if the given error is an AggregatedErrorLike
48
+ * 1. have "message:string" property
49
+ * 2. have "errors:Error[]" property
50
+ * @param {any} err - The error to check
51
+ * @returns {boolean} True if the error is an AggregatedError, false otherwise
52
+ */
53
+ static isAggregatedErrorLike (err) {
54
+ return err && Array.isArray(err.errors)
55
+ }
56
+
57
+ /**
58
+ * Checks if the given error is an instance of AggregatedError.
59
+ * @param {Error} err - The error to check.
60
+ * @returns {boolean} True if the error is an AggregatedError, false otherwise.
61
+ */
62
+ static isAggregatedError (err) {
63
+ return err instanceof AggregatedError
64
+ }
65
+
66
+ /**
67
+ * Error constructor with custom message and code.
68
+ * @param {string} message - Error message
69
+ * @param {any[]} [errors] - Errors
70
+ */
71
+ constructor (message, errors) {
72
+ super(message);
73
+ this.errors = errors ?? [];
74
+ }
75
+
76
+ /**
77
+ * Adds an error to the errors collection.
78
+ * @param {any} err - The error object to be added.
79
+ */
80
+ addError (err) {
81
+ this.errors.push(err);
82
+ }
83
+
84
+ /**
85
+ * Removes a specific error from the errors array.
86
+ * @param {Error} err - The error instance to remove.
87
+ * @returns {boolean} True if the error was found and removed, false otherwise.
88
+ */
89
+ removeError (err) {
90
+ const index = this.errors.indexOf(err);
91
+ if (index === -1) {
92
+ return false
93
+ }
94
+ this.errors.splice(index, 1);
95
+ return true
96
+ }
97
+ }
98
+
1
99
  /**
2
100
  * @module LangUtils
3
101
  * @description Language utility functions
@@ -612,7 +710,7 @@ var TypeAssert = {
612
710
  */
613
711
  function assertArray (value, paramName) {
614
712
  if (!Array.isArray(value)) {
615
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Array: type=${typeof value} value=${JSON.stringify(value)}`)
713
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Array: type=${typeof value} value=${safeToString(value)}`)
616
714
  }
617
715
  }
618
716
  /**
@@ -624,7 +722,7 @@ function assertArray (value, paramName) {
624
722
  */
625
723
  function assertString (value, paramName) {
626
724
  if (!isString(value)) {
627
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not String: type=${typeof value} value=${JSON.stringify(value)}`)
725
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not String: type=${typeof value} value=${safeToString(value)}`)
628
726
  }
629
727
  }
630
728
  /**
@@ -636,7 +734,7 @@ function assertString (value, paramName) {
636
734
  */
637
735
  function assertNumber (value, paramName) {
638
736
  if (!isNumber(value)) {
639
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Number: type=${typeof value} value=${JSON.stringify(value)}`)
737
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Number: type=${typeof value} value=${safeToString(value)}`)
640
738
  }
641
739
  }
642
740
 
@@ -648,7 +746,7 @@ function assertNumber (value, paramName) {
648
746
  */
649
747
  function assertPositive (value, paramName) {
650
748
  if (!isPositive(value)) {
651
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Positive: ${value}`)
749
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Positive: ${value}`)
652
750
  }
653
751
  }
654
752
 
@@ -660,7 +758,7 @@ function assertPositive (value, paramName) {
660
758
  */
661
759
  function assertNegative (value, paramName) {
662
760
  if (!isNegative(value)) {
663
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Negative: ${value}`)
761
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Negative: ${value}`)
664
762
  }
665
763
  }
666
764
 
@@ -672,7 +770,7 @@ function assertNegative (value, paramName) {
672
770
  */
673
771
  function assertNotNegative (value, paramName) {
674
772
  if (!isNotNegative(value)) {
675
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not "0 or Positive": ${value}`)
773
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not "0 or Positive": ${value}`)
676
774
  }
677
775
  }
678
776
 
@@ -685,7 +783,7 @@ function assertNotNegative (value, paramName) {
685
783
  */
686
784
  function assertBoolean (value, paramName) {
687
785
  if (!isBoolean(value)) {
688
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Boolean: type=${typeof value} value=${JSON.stringify(value)}`)
786
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Boolean: type=${typeof value} value=${safeToString(value)}`)
689
787
  }
690
788
  }
691
789
  /**
@@ -697,7 +795,7 @@ function assertBoolean (value, paramName) {
697
795
  */
698
796
  function assertObject (value, paramName) {
699
797
  if (!isObject(value)) {
700
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Object: type=${typeof value} value=${JSON.stringify(value)}`)
798
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Object: type=${typeof value} value=${safeToString(value)}`)
701
799
  }
702
800
  }
703
801
  /**
@@ -709,7 +807,7 @@ function assertObject (value, paramName) {
709
807
  */
710
808
  function assertPlainObject (value, paramName) {
711
809
  if (!isPlainObject$1(value)) {
712
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not PlainObject: type=${typeof value} value=${JSON.stringify(value)}`)
810
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not PlainObject: type=${typeof value} value=${safeToString(value)}`)
713
811
  }
714
812
  }
715
813
  /**
@@ -721,7 +819,7 @@ function assertPlainObject (value, paramName) {
721
819
  */
722
820
  function assertSymbol (value, paramName) {
723
821
  if (!isSymbol(value)) {
724
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Symbol: type=${typeof value} value=${JSON.stringify(value)}`)
822
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Symbol: type=${typeof value} value=${safeToString(value)}`)
725
823
  }
726
824
  }
727
825
  /**
@@ -733,7 +831,7 @@ function assertSymbol (value, paramName) {
733
831
  */
734
832
  function assertFunction (value, paramName) {
735
833
  if (!isFunction(value)) {
736
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Function: type=${typeof value} value=${JSON.stringify(value)}`)
834
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Function: type=${typeof value} value=${safeToString(value)}`)
737
835
  }
738
836
  }
739
837
  /**
@@ -745,7 +843,7 @@ function assertFunction (value, paramName) {
745
843
  */
746
844
  function assertInstance (value, paramName) {
747
845
  if (!isInstance(value)) {
748
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Class Instance: type=${typeof value} value=${JSON.stringify(value)}`)
846
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Class Instance: type=${typeof value} value=${safeToString(value)}`)
749
847
  }
750
848
  }
751
849
  /**
@@ -757,7 +855,7 @@ function assertInstance (value, paramName) {
757
855
  */
758
856
  function assertPromise (value, paramName) {
759
857
  if (!isPromise(value)) {
760
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Promise: type=${typeof value} value=${JSON.stringify(value)}`)
858
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Promise: type=${typeof value} value=${safeToString(value)}`)
761
859
  }
762
860
  }
763
861
  /**
@@ -769,7 +867,7 @@ function assertPromise (value, paramName) {
769
867
  */
770
868
  function assertNil (value, paramName) {
771
869
  if (!isNil(value)) {
772
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Neither Null nor Undefined: type=${typeof value} value=${JSON.stringify(value)}`)
870
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Neither Null nor Undefined: type=${typeof value} value=${safeToString(value)}`)
773
871
  }
774
872
  }
775
873
 
@@ -781,7 +879,7 @@ function assertNil (value, paramName) {
781
879
  */
782
880
  function assertNotNil (value, paramName) {
783
881
  if (isNil(value)) {
784
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Should Not Nil`)
882
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Should Not Nil`)
785
883
  }
786
884
  }
787
885
 
@@ -794,7 +892,7 @@ function assertNotNil (value, paramName) {
794
892
  */
795
893
  function assertNull (value, paramName) {
796
894
  if (!isNull(value)) {
797
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Null: type=${typeof value} value=${JSON.stringify(value)}`)
895
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Null: type=${typeof value} value=${safeToString(value)}`)
798
896
  }
799
897
  }
800
898
 
@@ -806,7 +904,7 @@ function assertNull (value, paramName) {
806
904
  */
807
905
  function assertNotNull (value, paramName) {
808
906
  if (isNull(value)) {
809
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Should Not Null`)
907
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Should Not Null`)
810
908
  }
811
909
  }
812
910
  /**
@@ -818,7 +916,7 @@ function assertNotNull (value, paramName) {
818
916
  */
819
917
  function assertUndefined (value, paramName) {
820
918
  if (!isUndefined(value)) {
821
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Undefined: type=${typeof value} value=${JSON.stringify(value)}`)
919
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Undefined: type=${typeof value} value=${safeToString(value)}`)
822
920
  }
823
921
  }
824
922
 
@@ -830,7 +928,7 @@ function assertUndefined (value, paramName) {
830
928
  */
831
929
  function assertStringOrSymbol (value, paramName) {
832
930
  if (!isString(value) && !isSymbol(value)) {
833
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not String or Symbol: type=${typeof value} value=${JSON.stringify(value)}`)
931
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not String or Symbol: type=${typeof value} value=${safeToString(value)}`)
834
932
  }
835
933
  }
836
934
 
@@ -841,7 +939,7 @@ function assertStringOrSymbol (value, paramName) {
841
939
  */
842
940
  function assertTypedArray (value, paramName) {
843
941
  if (isTypedArray(value)) {
844
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not TypedArray`)
942
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not TypedArray`)
845
943
  }
846
944
  }
847
945
  /**
@@ -851,7 +949,7 @@ function assertTypedArray (value, paramName) {
851
949
  */
852
950
  function assertInt8Array (value, paramName) {
853
951
  if (isInt8Array(value)) {
854
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Int8Array`)
952
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Int8Array`)
855
953
  }
856
954
  }
857
955
  /**
@@ -861,7 +959,7 @@ function assertInt8Array (value, paramName) {
861
959
  */
862
960
  function assertUint8Array (value, paramName) {
863
961
  if (isUint8Array(value)) {
864
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Uint8Array`)
962
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Uint8Array`)
865
963
  }
866
964
  }
867
965
  /**
@@ -871,7 +969,7 @@ function assertUint8Array (value, paramName) {
871
969
  */
872
970
  function assertUint8ClampedArray (value, paramName) {
873
971
  if (isUint8ClampedArray(value)) {
874
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Uint8ClampedArray`)
972
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Uint8ClampedArray`)
875
973
  }
876
974
  }
877
975
  /**
@@ -881,7 +979,7 @@ function assertUint8ClampedArray (value, paramName) {
881
979
  */
882
980
  function assertInt16Array (value, paramName) {
883
981
  if (isInt16Array(value)) {
884
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Int16Array`)
982
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Int16Array`)
885
983
  }
886
984
  }
887
985
  /**
@@ -891,7 +989,7 @@ function assertInt16Array (value, paramName) {
891
989
  */
892
990
  function assertUint16Array (value, paramName) {
893
991
  if (isUint16Array(value)) {
894
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Uint16Array`)
992
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Uint16Array`)
895
993
  }
896
994
  }
897
995
  /**
@@ -901,7 +999,7 @@ function assertUint16Array (value, paramName) {
901
999
  */
902
1000
  function assertInt32Array (value, paramName) {
903
1001
  if (isInt32Array(value)) {
904
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Int32Array`)
1002
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Int32Array`)
905
1003
  }
906
1004
  }
907
1005
  /**
@@ -911,7 +1009,7 @@ function assertInt32Array (value, paramName) {
911
1009
  */
912
1010
  function assertUint32Array (value, paramName) {
913
1011
  if (isUint32Array(value)) {
914
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Uint32Array`)
1012
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Uint32Array`)
915
1013
  }
916
1014
  }
917
1015
  /**
@@ -921,7 +1019,7 @@ function assertUint32Array (value, paramName) {
921
1019
  */
922
1020
  function assertFloat32Array (value, paramName) {
923
1021
  if (isFloat32Array(value)) {
924
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Float32Array`)
1022
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Float32Array`)
925
1023
  }
926
1024
  }
927
1025
  /**
@@ -931,7 +1029,7 @@ function assertFloat32Array (value, paramName) {
931
1029
  */
932
1030
  function assertFloat64Array (value, paramName) {
933
1031
  if (isFloat64Array(value)) {
934
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Float64Array`)
1032
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not Float64Array`)
935
1033
  }
936
1034
  }
937
1035
  /**
@@ -941,7 +1039,7 @@ function assertFloat64Array (value, paramName) {
941
1039
  */
942
1040
  function assertBigInt64Array (value, paramName) {
943
1041
  if (isBigInt64Array(value)) {
944
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not BigInt64Array`)
1042
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not BigInt64Array`)
945
1043
  }
946
1044
  }
947
1045
  /**
@@ -951,7 +1049,7 @@ function assertBigInt64Array (value, paramName) {
951
1049
  */
952
1050
  function assertBigUint64Array (value, paramName) {
953
1051
  if (isBigUint64Array(value)) {
954
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not BigUint64Array`)
1052
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not BigUint64Array`)
955
1053
  }
956
1054
  }
957
1055
 
@@ -963,7 +1061,7 @@ function assertBigUint64Array (value, paramName) {
963
1061
  */
964
1062
  function assertArrayBuffer (value, paramName) {
965
1063
  if (!isArrayBuffer(value)) {
966
- throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not ArrayBuffer`)
1064
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Not ArrayBuffer`)
967
1065
  }
968
1066
  }
969
1067
 
@@ -992,7 +1090,8 @@ var StringUtils = {
992
1090
  substringAfterLast,
993
1091
  substringBetween,
994
1092
  substringBetweenGreedy,
995
- substringsBetween
1093
+ substringsBetween,
1094
+ safeToString
996
1095
  };
997
1096
 
998
1097
  /**
@@ -1389,6 +1488,22 @@ function substringsBetween (str, startMarker, endMarker) {
1389
1488
  return substrings
1390
1489
  }
1391
1490
 
1491
+ /**
1492
+ * Safely converts a value to its string representation.
1493
+ * Attempts to use JSON.stringify first, falls back to toString() if stringify fails.
1494
+ * @param {*} value - The value to convert to string
1495
+ * @returns {string} The string representation of the value
1496
+ */
1497
+ function safeToString (value) {
1498
+ let valueStr;
1499
+ try {
1500
+ valueStr = JSON.stringify(value);
1501
+ } catch (e) {
1502
+ valueStr = value.toString();
1503
+ }
1504
+ return valueStr
1505
+ }
1506
+
1392
1507
  /**
1393
1508
  * @module ExecUtils
1394
1509
  * @description Utils about how to execute task functions.
@@ -1466,6 +1581,7 @@ var ExecUtils = {
1466
1581
  * promise: Promise<*>,
1467
1582
  * timerHandler: NodeJS.Timeout,
1468
1583
  * timerCleared: boolean,
1584
+ * pending: boolean,
1469
1585
  * resolved: boolean,
1470
1586
  * rejected: boolean,
1471
1587
  * canceled: boolean,
@@ -1478,7 +1594,8 @@ var ExecUtils = {
1478
1594
  * @typedef {{
1479
1595
  * promise: Promise<*>,
1480
1596
  * timerHandler: NodeJS.Timeout,
1481
- * resolve: (...args:any[])=> void
1597
+ * _resolve: (...args:any[])=> void,
1598
+ * wakeup: ()=> void
1482
1599
  * }} Waiter
1483
1600
  */
1484
1601
 
@@ -1487,6 +1604,7 @@ var ExecUtils = {
1487
1604
  * @description Promise utility functions for enhanced promise handling, including timeout, delay, parallel execution, and series execution.
1488
1605
  */
1489
1606
  var PromiseUtils = {
1607
+ any,
1490
1608
  defer,
1491
1609
  delay,
1492
1610
  timeout,
@@ -1495,6 +1613,7 @@ var PromiseUtils = {
1495
1613
  series,
1496
1614
  seriesAllSettled,
1497
1615
  parallel,
1616
+ parallelAny,
1498
1617
  parallelAllSettled,
1499
1618
  wait
1500
1619
  };
@@ -1510,48 +1629,66 @@ function defer (timeout = -1, timeoutMessage) {
1510
1629
  assertNumber(timeout);
1511
1630
  /** @type {Deferred} */
1512
1631
  const rtnVal = {};
1513
-
1632
+ rtnVal.pending = true;
1633
+ rtnVal.canceled = false;
1634
+ rtnVal.rejected = false;
1635
+ rtnVal.resolved = false;
1514
1636
  /**
1515
1637
  * @type {NodeJS.Timeout}
1516
1638
  */
1517
1639
  let timerHandler;
1518
1640
  if (timeout >= 0) {
1641
+ rtnVal.timerCleared = false;
1519
1642
  rtnVal.timerHandler = timerHandler = setTimeout(() => {
1520
1643
  clearTimeout(timerHandler); // must clear it
1521
- rtnVal.timerCleared = true; // easy to check in test case
1644
+ rtnVal.timerCleared = true;
1522
1645
  rtnVal.reject(new Error(timeoutMessage ?? `Promise Timeout: ${timeout}ms`));
1523
1646
  }, timeout);
1524
1647
  }
1525
1648
 
1526
1649
  rtnVal.promise = new Promise((resolve, reject) => {
1527
1650
  rtnVal.resolve = (arg) => {
1651
+ if (rtnVal.resolved || rtnVal.rejected || rtnVal.canceled) {
1652
+ return // already done, Can Not operate again
1653
+ }
1528
1654
  if (timerHandler != null) {
1529
1655
  clearTimeout(timerHandler); // must clear it
1530
- rtnVal.timerCleared = true; // easy to check in test case
1656
+ rtnVal.timerCleared = true;
1531
1657
  }
1658
+ rtnVal.pending = false;
1659
+ rtnVal.canceled = false;
1660
+ rtnVal.rejected = false;
1532
1661
  rtnVal.resolved = true;
1533
1662
  resolve(arg);
1534
1663
  };
1535
1664
 
1536
1665
  rtnVal.reject = (err) => {
1666
+ if (rtnVal.resolved || rtnVal.rejected || rtnVal.canceled) {
1667
+ return // already done, Can Not operate again
1668
+ }
1537
1669
  if (timerHandler != null) {
1538
1670
  clearTimeout(timerHandler); // must clear it
1539
- rtnVal.timerCleared = true; // easy to check in test case
1671
+ rtnVal.timerCleared = true;
1540
1672
  }
1673
+ rtnVal.pending = false;
1674
+ rtnVal.canceled = false;
1675
+ rtnVal.resolved = false;
1541
1676
  rtnVal.rejected = true;
1542
1677
  reject(err);
1543
1678
  };
1544
1679
  });
1545
1680
  // @ts-ignore
1546
- rtnVal.promise.cancel = () => {
1681
+ rtnVal.cancel = rtnVal.promise.cancel = (reason) => {
1682
+ if (rtnVal.resolved || rtnVal.rejected || rtnVal.canceled) {
1683
+ return // already done, Can Not operate again
1684
+ }
1547
1685
  if (timerHandler != null) {
1548
1686
  clearTimeout(timerHandler); // must clear it
1549
- rtnVal.timerCleared = true; // easy to check in test case
1687
+ rtnVal.timerCleared = true;
1550
1688
  }
1551
- rtnVal.rejected = true; // easy to check in test case
1689
+ rtnVal.reject(reason ?? new Error('Cancelled'));
1552
1690
  // @ts-ignore
1553
- rtnVal.canceled = rtnVal.promise.canceled = true; // easy to check in test case
1554
- rtnVal.reject(new Error('Cancelled'));
1691
+ rtnVal.canceled = rtnVal.promise.canceled = true;
1555
1692
  };
1556
1693
  return rtnVal
1557
1694
  }
@@ -1622,7 +1759,7 @@ async function allSettled (promises) {
1622
1759
  function returnValuePromised (task) {
1623
1760
  try {
1624
1761
  const taskRtnVal = task();
1625
- if (isPromise(taskRtnVal)) {
1762
+ if (TypeUtils.isPromise(taskRtnVal)) {
1626
1763
  return taskRtnVal
1627
1764
  }
1628
1765
  return Promise.resolve(taskRtnVal)
@@ -1643,7 +1780,7 @@ function returnValuePromised (task) {
1643
1780
  * @returns {Promise<*>} A new promise that settles after the delay period
1644
1781
  */
1645
1782
  function delay (promise, ms) {
1646
- if (isNumber(promise)) { // defer(ms)
1783
+ if (TypeUtils.isNumber(promise)) { // defer(ms)
1647
1784
  // @ts-ignore
1648
1785
  ms = promise;
1649
1786
  promise = Promise.resolve();
@@ -1675,21 +1812,78 @@ function delay (promise, ms) {
1675
1812
  });
1676
1813
  return deferred.promise
1677
1814
  }
1815
+ /**
1816
+ * 1. run all tasks
1817
+ * 2. any Task succeed, return its result
1818
+ * * resolve with the result.
1819
+ * * the others tasks will run to its end, and results will be dropped.
1820
+ * 3. If all tasks fail, rejects with an array of errors. the array length is same as the input tasks
1821
+ * @param {Array<Promise<any>|Function>} tasks - Array of promises or async functions to execute
1822
+ * @returns {Promise<any>} A promise that resolves with the result of the first successful task
1823
+ */
1824
+ function any (tasks) {
1825
+ assertArray(tasks);
1826
+ if (tasks.length === 0) {
1827
+ throw new Error('Empty Tasks')
1828
+ }
1829
+ const deferred = defer();
1830
+ /** @type {any[]} */
1831
+ const errors = [];
1832
+ for (let i = 0; i < tasks.length; i++) {
1833
+ const task = tasks[i];
1834
+ /** @type {Promise<any>} */
1835
+ let taskPromise;
1836
+ if (TypeUtils.isPromise(task)) {
1837
+ // @ts-ignore
1838
+ taskPromise = task;
1839
+ } else if (TypeUtils.isFunction(task)) {
1840
+ // @ts-ignore
1841
+ taskPromise = returnValuePromised(task);
1842
+ } else {
1843
+ errors.push(new Error(`Invalid Task at index ${i}/${tasks.length - 1}: ${task}`));
1844
+ continue
1845
+ }
1846
+ taskPromise.then(/** @type {any} */ rtnVal => {
1847
+ deferred.resolve(rtnVal);
1848
+ }).catch(e => {
1849
+ errors.push(e);
1850
+ // all tasks failed
1851
+ if (errors.length >= tasks.length) {
1852
+ deferred.reject(errors);
1853
+ }
1854
+ });
1855
+ }
1856
+ if (errors.length === tasks.length) {
1857
+ deferred.reject(errors);
1858
+ }
1859
+ return deferred.promise
1860
+ }
1678
1861
 
1679
1862
  /**
1680
- * Fast-Fail mode to execute Tasks(functions) in series (one after another) and returns their results in order.
1681
- * 1. export function are executed one by one
1682
- * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
1683
- * 3. if an element is not function, rejects the whole chain with Error(Not Function)
1684
- * @param {Function[]} tasks
1685
- * @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
1686
- */
1863
+ * Execute Tasks(functions) in series (one after another) and returns their results in order.
1864
+ * 1. Tasks are executed one by one
1865
+ * * if task is a function, execute it.
1866
+ * * if task is a promise, wait for it to settle.
1867
+ * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
1868
+ * 3. if an element is not function, rejects the whole chain with Error(Not Function)
1869
+ * 4. All Tasks run successfully, Return Results Array, it's length is same as the input tasks
1870
+ * @param {Array<Promise<any>|Function>} tasks
1871
+ * @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
1872
+ */
1687
1873
  async function series (tasks) {
1688
1874
  assertArray(tasks);
1689
1875
  const results = [];
1690
1876
  for (const task of tasks) {
1691
1877
  assertFunction(task);
1692
- results.push(await task());
1878
+ if (TypeUtils.isFunction(task)) {
1879
+ // @ts-ignore
1880
+ results.push(await returnValuePromised(task));
1881
+ } else if (TypeUtils.isPromise(task)) {
1882
+ // @ts-ignore
1883
+ results.push(await task);
1884
+ } else {
1885
+ throw new Error(`Invalid Task: ${task}`)
1886
+ }
1693
1887
  }
1694
1888
  return results
1695
1889
  }
@@ -1764,7 +1958,7 @@ async function parallel (tasks, maxParallel = 5) {
1764
1958
  * 2. all tasks will be executed, even some of them failed.
1765
1959
  * @param {Function[]} tasks
1766
1960
  * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
1767
- * @returns {Promise<any[]>} Array of resolved values from all promises
1961
+ * @returns {Promise<{ok: boolean, result: any}[]>}
1768
1962
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
1769
1963
  */
1770
1964
  async function parallelAllSettled (tasks, maxParallel = 5) {
@@ -1784,7 +1978,6 @@ async function parallelAllSettled (tasks, maxParallel = 5) {
1784
1978
  // run group by MaxParallel
1785
1979
  const tasksToRun = [];
1786
1980
  for (const task of tasks) {
1787
- assertFunction(task);
1788
1981
  tasksToRun.push(task);
1789
1982
  if (tasksToRun.length >= maxParallel) {
1790
1983
  const resultsForBatch = await allSettled(tasksToRun.map(task => returnValuePromised(task)));
@@ -1800,6 +1993,76 @@ async function parallelAllSettled (tasks, maxParallel = 5) {
1800
1993
  return rtnVal
1801
1994
  }
1802
1995
 
1996
+ /**
1997
+ * Executes multiple async tasks in parallel with limited concurrency,
1998
+ * 1. resolving when any task completes successfully.
1999
+ * 2. Maybe multiple tasks are executed as a bulk block, and all of them resolved.
2000
+ * * only the first fulfilled value is returned
2001
+ * * other results are dropped
2002
+ * @param {Array<Function|Promise<any>>} tasks - Array of async functions to execute
2003
+ * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
2004
+ * @returns {Promise<any>} Resolves with the result of the first successfully completed task
2005
+ */
2006
+ async function parallelAny (tasks, maxParallel = 5) {
2007
+ assertArray(tasks, 'tasks');
2008
+ assertNumber(maxParallel);
2009
+ if (tasks.length === 0) {
2010
+ throw new Error('Empty Tasks')
2011
+ }
2012
+ if (maxParallel <= 0) {
2013
+ throw new Error(`Invalid maxParallel: ${maxParallel}, should > 0`)
2014
+ }
2015
+ /** @type {any[]} */
2016
+ const errors = [];
2017
+ let taskIndex = 0;
2018
+ let runningTasksCount = 0;
2019
+ const deferred = defer();
2020
+ function takeTaskAndRun () {
2021
+ if (taskIndex >= tasks.length) {
2022
+ return // no more task
2023
+ }
2024
+ // reach max parallel, wait for one task to finish
2025
+ if (runningTasksCount > maxParallel) {
2026
+ return
2027
+ }
2028
+ const task = tasks[taskIndex++];
2029
+ runningTasksCount++;
2030
+ /** @type {Promise<any>} */
2031
+ let taskPromise;
2032
+ if (TypeUtils.isPromise(task)) {
2033
+ // @ts-ignore
2034
+ taskPromise = task;
2035
+ } else if (TypeUtils.isFunction(task)) {
2036
+ // @ts-ignore
2037
+ taskPromise = returnValuePromised(task);
2038
+ } else {
2039
+ errors.push(new TypeError(`Invalid task: ${task}`));
2040
+ takeTaskAndRun();
2041
+ return
2042
+ }
2043
+ taskPromise.then(/** @type {any} */ rtnVal => {
2044
+ deferred.resolve(rtnVal);
2045
+ }).catch(e => {
2046
+ errors.push(e);
2047
+ // No task left, and No successful execution, reject with errors
2048
+ if (errors.length >= tasks.length) {
2049
+ if (deferred.pending) {
2050
+ deferred.reject(errors);
2051
+ return
2052
+ }
2053
+ }
2054
+ takeTaskAndRun();
2055
+ }).finally(() => {
2056
+ runningTasksCount--;
2057
+ });
2058
+ }
2059
+ // start tasks until maxParallel
2060
+ while (runningTasksCount < maxParallel) {
2061
+ takeTaskAndRun();
2062
+ }
2063
+ return deferred.promise
2064
+ }
2065
+
1803
2066
  /**
1804
2067
  * Creates a "Waiter" Object
1805
2068
  * 1. wait the specified time
@@ -1818,17 +2081,20 @@ function wait (waitTime) {
1818
2081
  let timerHandler;
1819
2082
  rtnVal.timerHandler = timerHandler = setTimeout(() => {
1820
2083
  clearTimeout(timerHandler); // must clear it
1821
- rtnVal.resolve();
2084
+ rtnVal._resolve();
1822
2085
  }, waitTime);
1823
2086
 
1824
2087
  rtnVal.promise = new Promise((resolve, reject) => {
1825
- rtnVal.resolve = (arg) => {
2088
+ rtnVal._resolve = (arg) => {
1826
2089
  if (timerHandler != null) {
1827
2090
  clearTimeout(timerHandler); // must clear it
1828
2091
  }
1829
2092
  resolve(arg);
1830
2093
  };
1831
2094
  });
2095
+ rtnVal.wakeup = () => {
2096
+ rtnVal._resolve();
2097
+ };
1832
2098
  return rtnVal
1833
2099
  }
1834
2100
 
@@ -2289,6 +2555,7 @@ function timeoutMillis (nanoTimestamp64, millisTimeout) {
2289
2555
 
2290
2556
  var ArrayUtils = {
2291
2557
  first,
2558
+ chunk,
2292
2559
  last,
2293
2560
  equals,
2294
2561
  equalsIgnoreOrder
@@ -2372,13 +2639,35 @@ function equals (arr1, arr2, compareFn) {
2372
2639
  return true
2373
2640
  }
2374
2641
 
2642
+ /**
2643
+ * Splits an array into chunks of the specified size.
2644
+ * @param {any[]} array - The array to be chunked.
2645
+ * @param {number} size - The size of each chunk.
2646
+ * @returns {any[]} An array of arrays containing the chunks.
2647
+ */
2648
+ function chunk (array, size) {
2649
+ assertArray(array, 'array');
2650
+ assertPositive(size, 'size');
2651
+ if (array.length <= size) {
2652
+ return array
2653
+ }
2654
+ const chunked = [];
2655
+ let index = 0;
2656
+ while (index < array.length) {
2657
+ chunked.push(array.slice(index, size + index));
2658
+ index += size;
2659
+ }
2660
+ return chunked
2661
+ }
2662
+
2375
2663
  /**
2376
2664
  * @module Lang
2377
2665
  * @description Core language utilities for type checking, string manipulation, and common operations.
2378
2666
  */
2379
2667
 
2380
-
2381
2668
  var index = {
2669
+ _Error,
2670
+ AggregatedError,
2382
2671
  LangUtils,
2383
2672
  StringUtils,
2384
2673
  TypeUtils,
@@ -2397,5 +2686,5 @@ var index = {
2397
2686
  ArrayUtils
2398
2687
  };
2399
2688
 
2400
- export { ArrayBufferUtils, ArrayUtils, ClassProxyUtils, ExecUtils as Exec, ExecUtils, InstanceProxyUtils, LangUtils as Lang, LangUtils, PromiseUtils, ReflectUtils, StringUtils, TimeUtils, TypeUtils as Type, TypeAssert, TypeUtils, TypedArrayUtils, index as default };
2689
+ export { AggregatedError, ArrayBufferUtils, ArrayUtils, ClassProxyUtils, ExecUtils as Exec, ExecUtils, InstanceProxyUtils, LangUtils as Lang, LangUtils, PromiseUtils, ReflectUtils, StringUtils, TimeUtils, TypeUtils as Type, TypeAssert, TypeUtils, TypedArrayUtils, _Error, index as default };
2401
2690
  //# sourceMappingURL=index-dev.js.map