@creejs/commons-lang 2.1.10 → 2.1.12

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.
@@ -12,7 +12,7 @@ var LangUtils = {
12
12
  defaults,
13
13
  extend,
14
14
  extends: extend,
15
- equals: equals$1,
15
+ equals: equals$2,
16
16
  isBrowser,
17
17
  isNode
18
18
  };
@@ -80,7 +80,7 @@ function extend (target, ...sources) {
80
80
  * @param {*} value2 - Second value to compare
81
81
  * @returns {boolean} True if values are equal, false otherwise
82
82
  */
83
- function equals$1 (value1, value2) {
83
+ function equals$2 (value1, value2) {
84
84
  if (value1 === value2) {
85
85
  return true
86
86
  }
@@ -579,7 +579,7 @@ var TypeAssert = {
579
579
  */
580
580
  function assertArray (value, paramName) {
581
581
  if (!Array.isArray(value)) {
582
- throw new Error(`${paramName ? paramName + '' : ' '}Not Array: type=${typeof value} value=${JSON.stringify(value)}`)
582
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Array: type=${typeof value} value=${JSON.stringify(value)}`)
583
583
  }
584
584
  }
585
585
  /**
@@ -1908,7 +1908,7 @@ var ReflectUtils = {
1908
1908
  var TypedArrayUtils = {
1909
1909
  startsWith,
1910
1910
  isSameType,
1911
- equals
1911
+ equals: equals$1
1912
1912
  };
1913
1913
 
1914
1914
  /**
@@ -1923,7 +1923,7 @@ function startsWith (src, searching) {
1923
1923
  assertNotNil(searching, 'searching');
1924
1924
 
1925
1925
  const header = src.subarray(0, searching.length);
1926
- return equals(header, searching)
1926
+ return equals$1(header, searching)
1927
1927
  }
1928
1928
 
1929
1929
  /**
@@ -1949,7 +1949,7 @@ function isSameType (src, target) {
1949
1949
  * @returns {boolean} True if the typed arrays are equal, false otherwise.
1950
1950
  * @throws {Error} If either `src` or `target` is null or undefined.
1951
1951
  */
1952
- function equals (src, target) {
1952
+ function equals$1 (src, target) {
1953
1953
  assertNotNil(src, 'src');
1954
1954
  assertNotNil(target, 'target');
1955
1955
  if (!isSameType(src, target)) {
@@ -2094,6 +2094,166 @@ function writeArrayBuffer (target, dataArrayBuffer, targetOffset = 0, dataOffset
2094
2094
  targetView.set(dataView, targetOffset);
2095
2095
  }
2096
2096
 
2097
+ // module vars
2098
+ const ms2ns = 1_000_000;
2099
+ const s2ns = 1_000_000_000;
2100
+
2101
+ var TimeUtils = {
2102
+ s2ns,
2103
+ ms2ns,
2104
+ timestamp64,
2105
+ lapseNano,
2106
+ lapseMillis,
2107
+ timeoutNano,
2108
+ timeoutMillis
2109
+ };
2110
+
2111
+ /**
2112
+ * Gets the current timestamp in nanoseconds (if running in Node.js) or milliseconds (in browser).
2113
+ * Uses `process.hrtime.bigint()` for high-resolution timestamps in Node.js, falls back to `Date.now()` in browsers.
2114
+ * @returns {bigint} Current timestamp as a BigInt
2115
+ */
2116
+ function timestamp64 () {
2117
+ // sinon can not fake performance.timeOrigin, so we have to check it
2118
+ if (typeof performance !== 'undefined' && typeof performance.timeOrigin === 'number') {
2119
+ // timeOrigin specifies the high resolution millisecond timestamp, eg. 1756350801931.159
2120
+ const base = performance.timeOrigin;
2121
+ // the current high resolution millisecond timestamp, eg.31767926.416357
2122
+ const now = performance.now();
2123
+ return BigInt((base + now) * ms2ns)
2124
+ }
2125
+ return BigInt(Date.now() * ms2ns)
2126
+ }
2127
+
2128
+ /**
2129
+ * Calculates the time elapsed in nanoseconds between the given timestamp and now.
2130
+ * @param {bigint} start - start timestamp64, in nanoseconds.
2131
+ * @param {bigint} [end] - end timestamp64, in nanoseconds. If not provided, uses current timestamp64.
2132
+ * @returns {bigint} The elapsed time in nanoseconds (current timestamp64 - ts64).
2133
+ */
2134
+ function lapseNano (start, end) {
2135
+ return (end ?? timestamp64()) - start
2136
+ }
2137
+
2138
+ /**
2139
+ * Calculates the time elapsed in milliseconds between the given timestamp and now.
2140
+ * @param {bigint} start - start The timestamp in 64-bit format.
2141
+ * @param {bigint} [end] - end timestamp64, in nanoseconds. If not provided, uses current timestamp64.
2142
+ * @returns {bigint} The elapsed time in milliseconds.
2143
+ */
2144
+ function lapseMillis (start, end) {
2145
+ end = end ?? timestamp64();
2146
+ const lapseNano = end - start;
2147
+ return BigInt(lapseNano) / BigInt(ms2ns)
2148
+ }
2149
+
2150
+ /**
2151
+ * compare current timestamp64 against the given ts64, and check if the elapsed time exceeds the specified timeout.
2152
+ * @param {bigint} nanoTimestamp64 - The timestamp to compare against (in nanoseconds).
2153
+ * @param {bigint|number} nanoTimeout - The timeout threshold (in nanoseconds).
2154
+ * @returns {boolean} True if elapsed time exceeds timeout, false otherwise.
2155
+ */
2156
+ function timeoutNano (nanoTimestamp64, nanoTimeout) {
2157
+ return lapseNano(nanoTimestamp64) > nanoTimeout
2158
+ }
2159
+
2160
+ /**
2161
+ * compare current timestamp64 against the given ts64, and check if the elapsed time exceeds the specified timeout.
2162
+ * @param {bigint} nanoTimestamp64 - The timestamp to compare against (in nanoseconds).
2163
+ * @param {bigint|number} millisTimeout - The timeout threshold (in milliseconds).
2164
+ * @returns {boolean} True if elapsed time exceeds timeout, false otherwise.
2165
+ */
2166
+ function timeoutMillis (nanoTimestamp64, millisTimeout) {
2167
+ return lapseMillis(nanoTimestamp64) > millisTimeout
2168
+ }
2169
+
2170
+ // owned
2171
+
2172
+ var ArrayUtils = {
2173
+ first,
2174
+ last,
2175
+ equals,
2176
+ equalsIgnoreOrder
2177
+ };
2178
+
2179
+ /**
2180
+ * Gets the first element of an array
2181
+ * @param {any[]} arr - The input array
2182
+ * @param {any} [defaultValue] - The value to return if the array is empty or first element is undefined/null.
2183
+ * @returns {any} The first element of the array, or the defaultValue if the array is empty or not an array.
2184
+ * @throws {Error} if arr is not an array
2185
+ */
2186
+ function first (arr, defaultValue) {
2187
+ assertArray(arr, 'arr');
2188
+ return arr[0] ?? defaultValue
2189
+ }
2190
+
2191
+ /**
2192
+ * Gets the last element of an array
2193
+ * @param {any[]} arr - The input array
2194
+ * @param {any} [defaultValue] - The value to return if the array is empty or last element is undefined/null
2195
+ * @returns {any} The last element of the array or the defaultValue
2196
+ * @throws {Error} if arr is not an array
2197
+ */
2198
+ function last (arr, defaultValue) {
2199
+ assertArray(arr, 'arr');
2200
+ return arr[arr.length - 1] ?? defaultValue
2201
+ }
2202
+
2203
+ /**
2204
+ * Checks if two arrays are equal ignoring element order
2205
+ * @param {any[]} arr1 - first array to compare
2206
+ * @param {any[]} arr2 - Second array to compare
2207
+ * @param {(a:any, b:any) => 0|1|-1} [compareFn]
2208
+ * @returns {boolean} True if arrays have same elements (order-independent), false otherwise
2209
+ */
2210
+ function equalsIgnoreOrder (arr1, arr2, compareFn) {
2211
+ if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
2212
+ return false
2213
+ }
2214
+ if (arr1.length !== arr2.length) {
2215
+ return false
2216
+ }
2217
+ arr1.sort(compareFn);
2218
+ arr2.sort(compareFn);
2219
+ for (let i = 0; i < arr1.length; i++) {
2220
+ if (compareFn) {
2221
+ if (compareFn(arr1[i], arr2[i]) !== 0) {
2222
+ return false
2223
+ }
2224
+ } else if (arr1[i] !== arr2[i]) {
2225
+ return false
2226
+ }
2227
+ }
2228
+ return true
2229
+ }
2230
+
2231
+ /**
2232
+ * Checks if two arrays are equal by order
2233
+ * @param {any[]} arr1 - first array to compare
2234
+ * @param {any[]} arr2 - Second array to compare
2235
+ * @param {(a:any, b:any) => 0|1|-1} [compareFn]
2236
+ * @returns {boolean} True if arrays have same elements (order-independent), false otherwise
2237
+ */
2238
+ function equals (arr1, arr2, compareFn) {
2239
+ if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
2240
+ return false
2241
+ }
2242
+ if (arr1.length !== arr2.length) {
2243
+ return false
2244
+ }
2245
+ for (let i = 0; i < arr1.length; i++) {
2246
+ if (compareFn) {
2247
+ if (compareFn(arr1[i], arr2[i]) !== 0) {
2248
+ return false
2249
+ }
2250
+ } else if (arr1[i] !== arr2[i]) {
2251
+ return false
2252
+ }
2253
+ }
2254
+ return true
2255
+ }
2256
+
2097
2257
  /**
2098
2258
  * @module Lang
2099
2259
  * @description Core language utilities for type checking, string manipulation, and common operations.
@@ -2114,10 +2274,13 @@ var index = {
2114
2274
  InstanceProxyUtils,
2115
2275
  ReflectUtils,
2116
2276
  TypedArrayUtils,
2117
- ArrayBufferUtils
2277
+ ArrayBufferUtils,
2278
+ TimeUtils,
2279
+ ArrayUtils
2118
2280
  };
2119
2281
 
2120
2282
  exports.ArrayBufferUtils = ArrayBufferUtils;
2283
+ exports.ArrayUtils = ArrayUtils;
2121
2284
  exports.ClassProxyUtils = ClassProxyUtils;
2122
2285
  exports.Exec = ExecUtils;
2123
2286
  exports.ExecUtils = ExecUtils;
@@ -2127,6 +2290,7 @@ exports.LangUtils = LangUtils;
2127
2290
  exports.PromiseUtils = PromiseUtils;
2128
2291
  exports.ReflectUtils = ReflectUtils;
2129
2292
  exports.StringUtils = StringUtils;
2293
+ exports.TimeUtils = TimeUtils;
2130
2294
  exports.Type = TypeUtils;
2131
2295
  exports.TypeAssert = TypeAssert;
2132
2296
  exports.TypeUtils = TypeUtils;