@creejs/commons-lang 2.1.11 → 2.1.13

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.
@@ -8,7 +8,7 @@ var LangUtils = {
8
8
  defaults,
9
9
  extend,
10
10
  extends: extend,
11
- equals: equals$1,
11
+ equals: equals$2,
12
12
  isBrowser,
13
13
  isNode
14
14
  };
@@ -76,7 +76,7 @@ function extend (target, ...sources) {
76
76
  * @param {*} value2 - Second value to compare
77
77
  * @returns {boolean} True if values are equal, false otherwise
78
78
  */
79
- function equals$1 (value1, value2) {
79
+ function equals$2 (value1, value2) {
80
80
  if (value1 === value2) {
81
81
  return true
82
82
  }
@@ -575,7 +575,7 @@ var TypeAssert = {
575
575
  */
576
576
  function assertArray (value, paramName) {
577
577
  if (!Array.isArray(value)) {
578
- throw new Error(`${paramName ? paramName + '' : ' '}Not Array: type=${typeof value} value=${JSON.stringify(value)}`)
578
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Array: type=${typeof value} value=${JSON.stringify(value)}`)
579
579
  }
580
580
  }
581
581
  /**
@@ -1399,7 +1399,20 @@ var ExecUtils = {
1399
1399
  quietKeepError
1400
1400
  };
1401
1401
 
1402
- // @ts-nocheck
1402
+ // owned
1403
+
1404
+ /**
1405
+ * @typedef {{
1406
+ * promise: Promise<*>,
1407
+ * timerHandler: NodeJS.Timeout,
1408
+ * timerCleared: boolean,
1409
+ * resolved: boolean,
1410
+ * rejected: boolean,
1411
+ * canceled: boolean,
1412
+ * reject: (reason: Error)=> void,
1413
+ * resolve: (...args:any[])=> void
1414
+ * }} Deferred
1415
+ */
1403
1416
 
1404
1417
  /**
1405
1418
  * @module PromiseUtils
@@ -1422,12 +1435,16 @@ var PromiseUtils = {
1422
1435
  * 1. timeout=-1, it means no timeout check
1423
1436
  * @param {number} [timeout=-1] - Timeout duration in milliseconds
1424
1437
  * @param {string} [timeoutMessage]
1425
- * @returns {{promise: Promise<*>, reject: function, resolve: function}}
1438
+ * @returns {Deferred}
1426
1439
  */
1427
1440
  function defer (timeout = -1, timeoutMessage) {
1428
1441
  assertNumber(timeout);
1442
+ /** @type {Deferred} */
1429
1443
  const rtnVal = {};
1430
1444
 
1445
+ /**
1446
+ * @type {NodeJS.Timeout}
1447
+ */
1431
1448
  let timerHandler;
1432
1449
  if (timeout >= 0) {
1433
1450
  rtnVal.timerHandler = timerHandler = setTimeout(() => {
@@ -1435,17 +1452,16 @@ function defer (timeout = -1, timeoutMessage) {
1435
1452
  rtnVal.timerCleared = true; // easy to check in test case
1436
1453
  rtnVal.reject(new Error(timeoutMessage ?? `Promise Timeout: ${timeout}ms`));
1437
1454
  }, timeout);
1438
- rtnVal.timerHandler = timerHandler;
1439
1455
  }
1440
1456
 
1441
1457
  rtnVal.promise = new Promise((resolve, reject) => {
1442
- rtnVal.resolve = (...args) => {
1458
+ rtnVal.resolve = (arg) => {
1443
1459
  if (timerHandler != null) {
1444
1460
  clearTimeout(timerHandler); // must clear it
1445
1461
  rtnVal.timerCleared = true; // easy to check in test case
1446
1462
  }
1447
1463
  rtnVal.resolved = true;
1448
- resolve(...args);
1464
+ resolve(arg);
1449
1465
  };
1450
1466
 
1451
1467
  rtnVal.reject = (err) => {
@@ -1457,12 +1473,14 @@ function defer (timeout = -1, timeoutMessage) {
1457
1473
  reject(err);
1458
1474
  };
1459
1475
  });
1476
+ // @ts-ignore
1460
1477
  rtnVal.promise.cancel = () => {
1461
1478
  if (timerHandler != null) {
1462
1479
  clearTimeout(timerHandler); // must clear it
1463
1480
  rtnVal.timerCleared = true; // easy to check in test case
1464
1481
  }
1465
1482
  rtnVal.rejected = true; // easy to check in test case
1483
+ // @ts-ignore
1466
1484
  rtnVal.canceled = rtnVal.promise.canceled = true; // easy to check in test case
1467
1485
  rtnVal.reject(new Error('Cancelled'));
1468
1486
  };
@@ -1509,8 +1527,8 @@ function timeout (promise, time, message) {
1509
1527
  * 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
1510
1528
  * * the data structure is not consistent when fullfilled or rejected
1511
1529
  * * have to check "string" type of status to know sucess or failure
1512
- * @param {Promise} promises
1513
- * @returns {Array<{ok: boolean, result: any}>}
1530
+ * @param {Promise<*>[]} promises
1531
+ * @returns {Promise<{ok: boolean, result: any}[]>}
1514
1532
  */
1515
1533
  async function allSettled (promises) {
1516
1534
  assertArray(promises);
@@ -1553,13 +1571,14 @@ function returnValuePromised (task) {
1553
1571
  *
1554
1572
  * @param {Promise<*>|number|undefined} [promise] - The input promise to delay
1555
1573
  * @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
1556
- * @returns {Promise} A new promise that settles after the delay period
1574
+ * @returns {Promise<*>} A new promise that settles after the delay period
1557
1575
  */
1558
1576
  function delay (promise, ms) {
1559
- if (isNumber(promise)) {
1577
+ if (isNumber(promise)) { // defer(ms)
1578
+ // @ts-ignore
1560
1579
  ms = promise;
1561
1580
  promise = Promise.resolve();
1562
- } else if (promise == null && ms == null) {
1581
+ } else if (promise == null && ms == null) { // defer(promise, ms)
1563
1582
  ms = 1;
1564
1583
  promise = Promise.resolve();
1565
1584
  }
@@ -1568,16 +1587,16 @@ function delay (promise, ms) {
1568
1587
  assertNumber(ms);
1569
1588
  const deferred = defer();
1570
1589
  const startTs = Date.now();
1571
- promise
1572
- .then((...args) => {
1573
- const escaped = Date.now() - startTs;
1574
- if (escaped < ms) {
1575
- setTimeout(() => deferred.resolve(...args), ms - escaped);
1576
- } else {
1577
- deferred.resolve(...args);
1578
- }
1579
- })
1580
- .catch((err) => {
1590
+ // @ts-ignore
1591
+ promise.then((...args) => {
1592
+ const escaped = Date.now() - startTs;
1593
+ if (escaped < ms) {
1594
+ setTimeout(() => deferred.resolve(...args), ms - escaped);
1595
+ } else {
1596
+ deferred.resolve(...args);
1597
+ }
1598
+ })
1599
+ .catch((/** @type {Error} */ err) => {
1581
1600
  const escaped = Date.now() - startTs;
1582
1601
  if (escaped < ms) {
1583
1602
  setTimeout(() => deferred.reject(err), ms - escaped);
@@ -1593,8 +1612,8 @@ function delay (promise, ms) {
1593
1612
  * 1. export function are executed one by one
1594
1613
  * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
1595
1614
  * 3. if an element is not function, rejects the whole chain with Error(Not Function)
1596
- * @param {Function[]} promises
1597
- * @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
1615
+ * @param {Function[]} tasks
1616
+ * @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
1598
1617
  */
1599
1618
  async function series (tasks) {
1600
1619
  assertArray(tasks);
@@ -1634,7 +1653,7 @@ async function seriesAllSettled (tasks) {
1634
1653
  * 2. rejects whole chain with the first error, when first task fails
1635
1654
  * @param {Function[]} tasks
1636
1655
  * @param {number} [maxParallel=5]
1637
- * @returns {Promise<Array>} Array of resolved values from all promises
1656
+ * @returns {Promise<any[]>} Array of resolved values from all promises
1638
1657
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
1639
1658
  */
1640
1659
  async function parallel (tasks, maxParallel = 5) {
@@ -1676,7 +1695,7 @@ async function parallel (tasks, maxParallel = 5) {
1676
1695
  * 2. all tasks will be executed, even some of them failed.
1677
1696
  * @param {Function[]} tasks
1678
1697
  * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
1679
- * @returns {Promise<Array>} Array of resolved values from all promises
1698
+ * @returns {Promise<any[]>} Array of resolved values from all promises
1680
1699
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
1681
1700
  */
1682
1701
  async function parallelAllSettled (tasks, maxParallel = 5) {
@@ -1904,7 +1923,7 @@ var ReflectUtils = {
1904
1923
  var TypedArrayUtils = {
1905
1924
  startsWith,
1906
1925
  isSameType,
1907
- equals
1926
+ equals: equals$1
1908
1927
  };
1909
1928
 
1910
1929
  /**
@@ -1919,7 +1938,7 @@ function startsWith (src, searching) {
1919
1938
  assertNotNil(searching, 'searching');
1920
1939
 
1921
1940
  const header = src.subarray(0, searching.length);
1922
- return equals(header, searching)
1941
+ return equals$1(header, searching)
1923
1942
  }
1924
1943
 
1925
1944
  /**
@@ -1945,7 +1964,7 @@ function isSameType (src, target) {
1945
1964
  * @returns {boolean} True if the typed arrays are equal, false otherwise.
1946
1965
  * @throws {Error} If either `src` or `target` is null or undefined.
1947
1966
  */
1948
- function equals (src, target) {
1967
+ function equals$1 (src, target) {
1949
1968
  assertNotNil(src, 'src');
1950
1969
  assertNotNil(target, 'target');
1951
1970
  if (!isSameType(src, target)) {
@@ -2090,21 +2109,164 @@ function writeArrayBuffer (target, dataArrayBuffer, targetOffset = 0, dataOffset
2090
2109
  targetView.set(dataView, targetOffset);
2091
2110
  }
2092
2111
 
2112
+ // module vars
2113
+ const ms2ns = 1_000_000;
2114
+ const s2ns = 1_000_000_000;
2115
+
2093
2116
  var TimeUtils = {
2094
- timestamp64
2117
+ s2ns,
2118
+ ms2ns,
2119
+ timestamp64,
2120
+ lapseNano,
2121
+ lapseMillis,
2122
+ timeoutNano,
2123
+ timeoutMillis
2095
2124
  };
2096
2125
 
2097
- const ms2ns = BigInt(1_000_000);
2098
2126
  /**
2099
2127
  * Gets the current timestamp in nanoseconds (if running in Node.js) or milliseconds (in browser).
2100
2128
  * Uses `process.hrtime.bigint()` for high-resolution timestamps in Node.js, falls back to `Date.now()` in browsers.
2101
2129
  * @returns {bigint} Current timestamp as a BigInt
2102
2130
  */
2103
2131
  function timestamp64 () {
2104
- if (typeof performance !== 'undefined') {
2105
- return BigInt(Math.floor(performance.timeOrigin + performance.now())) * ms2ns
2132
+ // sinon can not fake performance.timeOrigin, so we have to check it
2133
+ if (typeof performance !== 'undefined' && typeof performance.timeOrigin === 'number') {
2134
+ // timeOrigin specifies the high resolution millisecond timestamp, eg. 1756350801931.159
2135
+ const base = performance.timeOrigin;
2136
+ // the current high resolution millisecond timestamp, eg.31767926.416357
2137
+ const now = performance.now();
2138
+ return BigInt((base + now) * ms2ns)
2106
2139
  }
2107
- return BigInt(Date.now()) * ms2ns
2140
+ return BigInt(Date.now() * ms2ns)
2141
+ }
2142
+
2143
+ /**
2144
+ * Calculates the time elapsed in nanoseconds between the given timestamp and now.
2145
+ * @param {bigint} start - start timestamp64, in nanoseconds.
2146
+ * @param {bigint} [end] - end timestamp64, in nanoseconds. If not provided, uses current timestamp64.
2147
+ * @returns {bigint} The elapsed time in nanoseconds (current timestamp64 - ts64).
2148
+ */
2149
+ function lapseNano (start, end) {
2150
+ return (end ?? timestamp64()) - start
2151
+ }
2152
+
2153
+ /**
2154
+ * Calculates the time elapsed in milliseconds between the given timestamp and now.
2155
+ * @param {bigint} start - start The timestamp in 64-bit format.
2156
+ * @param {bigint} [end] - end timestamp64, in nanoseconds. If not provided, uses current timestamp64.
2157
+ * @returns {bigint} The elapsed time in milliseconds.
2158
+ */
2159
+ function lapseMillis (start, end) {
2160
+ end = end ?? timestamp64();
2161
+ const lapseNano = end - start;
2162
+ return BigInt(lapseNano) / BigInt(ms2ns)
2163
+ }
2164
+
2165
+ /**
2166
+ * compare current timestamp64 against the given ts64, and check if the elapsed time exceeds the specified timeout.
2167
+ * @param {bigint} nanoTimestamp64 - The timestamp to compare against (in nanoseconds).
2168
+ * @param {bigint|number} nanoTimeout - The timeout threshold (in nanoseconds).
2169
+ * @returns {boolean} True if elapsed time exceeds timeout, false otherwise.
2170
+ */
2171
+ function timeoutNano (nanoTimestamp64, nanoTimeout) {
2172
+ return lapseNano(nanoTimestamp64) > nanoTimeout
2173
+ }
2174
+
2175
+ /**
2176
+ * compare current timestamp64 against the given ts64, and check if the elapsed time exceeds the specified timeout.
2177
+ * @param {bigint} nanoTimestamp64 - The timestamp to compare against (in nanoseconds).
2178
+ * @param {bigint|number} millisTimeout - The timeout threshold (in milliseconds).
2179
+ * @returns {boolean} True if elapsed time exceeds timeout, false otherwise.
2180
+ */
2181
+ function timeoutMillis (nanoTimestamp64, millisTimeout) {
2182
+ return lapseMillis(nanoTimestamp64) > millisTimeout
2183
+ }
2184
+
2185
+ // owned
2186
+
2187
+ var ArrayUtils = {
2188
+ first,
2189
+ last,
2190
+ equals,
2191
+ equalsIgnoreOrder
2192
+ };
2193
+
2194
+ /**
2195
+ * Gets the first element of an array
2196
+ * @param {any[]} arr - The input array
2197
+ * @param {any} [defaultValue] - The value to return if the array is empty or first element is undefined/null.
2198
+ * @returns {any} The first element of the array, or the defaultValue if the array is empty or not an array.
2199
+ * @throws {Error} if arr is not an array
2200
+ */
2201
+ function first (arr, defaultValue) {
2202
+ assertArray(arr, 'arr');
2203
+ return arr[0] ?? defaultValue
2204
+ }
2205
+
2206
+ /**
2207
+ * Gets the last element of an array
2208
+ * @param {any[]} arr - The input array
2209
+ * @param {any} [defaultValue] - The value to return if the array is empty or last element is undefined/null
2210
+ * @returns {any} The last element of the array or the defaultValue
2211
+ * @throws {Error} if arr is not an array
2212
+ */
2213
+ function last (arr, defaultValue) {
2214
+ assertArray(arr, 'arr');
2215
+ return arr[arr.length - 1] ?? defaultValue
2216
+ }
2217
+
2218
+ /**
2219
+ * Checks if two arrays are equal ignoring element order
2220
+ * @param {any[]} arr1 - first array to compare
2221
+ * @param {any[]} arr2 - Second array to compare
2222
+ * @param {(a:any, b:any) => 0|1|-1} [compareFn]
2223
+ * @returns {boolean} True if arrays have same elements (order-independent), false otherwise
2224
+ */
2225
+ function equalsIgnoreOrder (arr1, arr2, compareFn) {
2226
+ if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
2227
+ return false
2228
+ }
2229
+ if (arr1.length !== arr2.length) {
2230
+ return false
2231
+ }
2232
+ arr1.sort(compareFn);
2233
+ arr2.sort(compareFn);
2234
+ for (let i = 0; i < arr1.length; i++) {
2235
+ if (compareFn) {
2236
+ if (compareFn(arr1[i], arr2[i]) !== 0) {
2237
+ return false
2238
+ }
2239
+ } else if (arr1[i] !== arr2[i]) {
2240
+ return false
2241
+ }
2242
+ }
2243
+ return true
2244
+ }
2245
+
2246
+ /**
2247
+ * Checks if two arrays are equal by order
2248
+ * @param {any[]} arr1 - first array to compare
2249
+ * @param {any[]} arr2 - Second array to compare
2250
+ * @param {(a:any, b:any) => 0|1|-1} [compareFn]
2251
+ * @returns {boolean} True if arrays have same elements (order-independent), false otherwise
2252
+ */
2253
+ function equals (arr1, arr2, compareFn) {
2254
+ if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
2255
+ return false
2256
+ }
2257
+ if (arr1.length !== arr2.length) {
2258
+ return false
2259
+ }
2260
+ for (let i = 0; i < arr1.length; i++) {
2261
+ if (compareFn) {
2262
+ if (compareFn(arr1[i], arr2[i]) !== 0) {
2263
+ return false
2264
+ }
2265
+ } else if (arr1[i] !== arr2[i]) {
2266
+ return false
2267
+ }
2268
+ }
2269
+ return true
2108
2270
  }
2109
2271
 
2110
2272
  /**
@@ -2128,8 +2290,9 @@ var index = {
2128
2290
  ReflectUtils,
2129
2291
  TypedArrayUtils,
2130
2292
  ArrayBufferUtils,
2131
- TimeUtils
2293
+ TimeUtils,
2294
+ ArrayUtils
2132
2295
  };
2133
2296
 
2134
- export { ArrayBufferUtils, ClassProxyUtils, ExecUtils as Exec, ExecUtils, InstanceProxyUtils, LangUtils as Lang, LangUtils, PromiseUtils, ReflectUtils, StringUtils, TimeUtils, TypeUtils as Type, TypeAssert, TypeUtils, TypedArrayUtils, index as default };
2297
+ 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 };
2135
2298
  //# sourceMappingURL=index-dev.js.map