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