@lowentry/utils 1.10.5 → 1.11.1

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/index.js CHANGED
@@ -245,6 +245,50 @@ var LeUtils = {
245
245
  * @returns {boolean} Returns true if the values are equivalent.
246
246
  */
247
247
  equals: FastDeepEqual,
248
+ /**
249
+ * Returns a deep copy of the given value.
250
+ *
251
+ * @param {*} value
252
+ * @returns {*}
253
+ */
254
+ clone: function clone(value) {
255
+ return CloneDeep(value, true);
256
+ },
257
+ /**
258
+ * Executes the given callback when the document is ready.
259
+ *
260
+ * @param {Function} callback
261
+ * @returns {{remove:Function}}
262
+ */
263
+ onDomReady: function onDomReady(callback) {
264
+ if (typeof window === 'undefined' || !document) {
265
+ // no document, so we can't wait for it to be ready
266
+ return {
267
+ remove: function remove() {}
268
+ };
269
+ }
270
+ if (document.readyState === 'interactive' || document.readyState === 'complete') {
271
+ return LeUtils.setTimeout(callback, 0);
272
+ } else {
273
+ var listening = true;
274
+ var callbackWrapper = function callbackWrapper() {
275
+ if (listening) {
276
+ listening = false;
277
+ document.removeEventListener('DOMContentLoaded', callbackWrapper);
278
+ callback();
279
+ }
280
+ };
281
+ document.addEventListener('DOMContentLoaded', callbackWrapper);
282
+ return {
283
+ remove: function remove() {
284
+ if (listening) {
285
+ listening = false;
286
+ document.removeEventListener('DOMContentLoaded', callbackWrapper);
287
+ }
288
+ }
289
+ };
290
+ }
291
+ },
248
292
  /**
249
293
  * Parses the given version string, and returns an object with the major, minor, and patch numbers, as well as some comparison functions.
250
294
  *
@@ -1083,6 +1127,11 @@ var LeUtils = {
1083
1127
  };
1084
1128
  return setTimeout;
1085
1129
  }(function (callback, ms) {
1130
+ if (typeof window === 'undefined') {
1131
+ return {
1132
+ remove: function remove() {}
1133
+ };
1134
+ }
1086
1135
  ms = FLOAT_LAX(ms);
1087
1136
  var lastTime = performance.now();
1088
1137
  var handler = setTimeout(function () {
@@ -1136,6 +1185,11 @@ var LeUtils = {
1136
1185
  console.error(e);
1137
1186
  }
1138
1187
  }
1188
+ if (typeof window === 'undefined') {
1189
+ return {
1190
+ remove: function remove() {}
1191
+ };
1192
+ }
1139
1193
  var lastTime = performance.now();
1140
1194
  var handler = setInterval(function () {
1141
1195
  var currentTime = performance.now();
@@ -1170,6 +1224,11 @@ var LeUtils = {
1170
1224
  */
1171
1225
  setAnimationFrameTimeout: function setAnimationFrameTimeout(callback) {
1172
1226
  var frames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
1227
+ if (typeof window === 'undefined') {
1228
+ return {
1229
+ remove: function remove() {}
1230
+ };
1231
+ }
1173
1232
  frames = INT_LAX_ANY(frames, 1);
1174
1233
  var run = true;
1175
1234
  var requestAnimationFrameId = null;
@@ -1228,6 +1287,11 @@ var LeUtils = {
1228
1287
  console.error(e);
1229
1288
  }
1230
1289
  }
1290
+ if (typeof window === 'undefined') {
1291
+ return {
1292
+ remove: function remove() {}
1293
+ };
1294
+ }
1231
1295
  var run = true;
1232
1296
  var requestAnimationFrameId = null;
1233
1297
  var lastTime = performance.now();
@@ -1295,6 +1359,77 @@ var LeUtils = {
1295
1359
  return LeUtils.setAnimationFrameTimeout(resolve, frames);
1296
1360
  });
1297
1361
  },
1362
+ /**
1363
+ * Allows you to do a fetch, with built-in retry and abort functionality.
1364
+ *
1365
+ * @param {string} url
1366
+ * @param {Object} [options]
1367
+ * @returns {{promise:Promise<*>, then:Function, catch:Function, finally:Function, remove:Function, isRemoved:Function}}
1368
+ */
1369
+ fetch: function (_fetch) {
1370
+ function fetch(_x12, _x13) {
1371
+ return _fetch.apply(this, arguments);
1372
+ }
1373
+ fetch.toString = function () {
1374
+ return _fetch.toString();
1375
+ };
1376
+ return fetch;
1377
+ }(function (url, options) {
1378
+ var currentRetries = 0;
1379
+ var retries = INT_LAX(options === null || options === void 0 ? void 0 : options.retries);
1380
+ var signal = new AbortController();
1381
+ var promise = new Promise(function (resolve, reject) {
1382
+ var attemptFetch = function attemptFetch() {
1383
+ if (signal.signal.aborted) {
1384
+ reject(new Error('Aborted'));
1385
+ return;
1386
+ }
1387
+ fetch(url, _objectSpread(_objectSpread({
1388
+ signal: signal
1389
+ }, options !== null && options !== void 0 ? options : {}), {}, {
1390
+ retries: undefined,
1391
+ delay: undefined
1392
+ })).then(function (response) {
1393
+ if (!response.ok) {
1394
+ throw new Error('Network request failed: ' + response.status + ' ' + response.statusText);
1395
+ }
1396
+ return response;
1397
+ }).then(function (response) {
1398
+ resolve(response);
1399
+ })["catch"](function (error) {
1400
+ if (currentRetries >= retries) {
1401
+ reject(error);
1402
+ return;
1403
+ }
1404
+ currentRetries++;
1405
+ setTimeout(attemptFetch, typeof (options === null || options === void 0 ? void 0 : options.delay) === 'function' ? INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay(currentRetries), 500) : INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay, 500));
1406
+ });
1407
+ };
1408
+ attemptFetch();
1409
+ });
1410
+ var result = {};
1411
+ result.promise = promise;
1412
+ result.then = function () {
1413
+ promise.then.apply(promise, arguments);
1414
+ return result;
1415
+ };
1416
+ result["catch"] = function () {
1417
+ promise["catch"].apply(promise, arguments);
1418
+ return result;
1419
+ };
1420
+ result["finally"] = function () {
1421
+ promise["finally"].apply(promise, arguments);
1422
+ return result;
1423
+ };
1424
+ result.remove = function () {
1425
+ signal.abort.apply(signal, arguments);
1426
+ return result;
1427
+ };
1428
+ result.isRemoved = function () {
1429
+ return signal.signal.aborted;
1430
+ };
1431
+ return result;
1432
+ }),
1298
1433
  /**
1299
1434
  * Returns true if the user is on a smartphone device (mobile).
1300
1435
  * Will return false if the user is on a tablet or on a desktop.
@@ -1474,10 +1609,21 @@ var LeUtils = {
1474
1609
  * @returns {string}
1475
1610
  */
1476
1611
  purgeErrorMessage: function purgeErrorMessage(error) {
1477
- var _error$message;
1478
- var message = STRING(typeof error === 'string' ? error : (_error$message = error.message) !== null && _error$message !== void 0 ? _error$message : JSON.stringify(error));
1479
- var messageParts = message.split('threw an error:');
1480
- return messageParts[messageParts.length - 1].trim();
1612
+ var _error;
1613
+ if ((_error = error) !== null && _error !== void 0 && _error.message) {
1614
+ error = error.message;
1615
+ }
1616
+ if (typeof error === 'string') {
1617
+ var errorParts = error.split('threw an error:');
1618
+ error = errorParts[errorParts.length - 1];
1619
+ } else {
1620
+ try {
1621
+ error = JSON.stringify(error);
1622
+ } catch (e) {
1623
+ error = 'An unknown error occurred';
1624
+ }
1625
+ }
1626
+ return error.trim();
1481
1627
  },
1482
1628
  /**
1483
1629
  * Generates all permutations of the given names.
@@ -1571,6 +1717,9 @@ var LeUtils = {
1571
1717
  var generateUniqueId = function generateUniqueId() {
1572
1718
  var now;
1573
1719
  try {
1720
+ if (typeof window === 'undefined') {
1721
+ throw new Error();
1722
+ }
1574
1723
  // noinspection JSDeprecatedSymbols
1575
1724
  now = (performance.timeOrigin || performance.timing.navigationStart) + performance.now();
1576
1725
  if (typeof now !== 'number') {
@@ -1646,6 +1795,9 @@ var LeUtils = {
1646
1795
  now = FLOAT_LAX(now);
1647
1796
  } else {
1648
1797
  try {
1798
+ if (typeof window === 'undefined') {
1799
+ throw new Error();
1800
+ }
1649
1801
  // noinspection JSDeprecatedSymbols
1650
1802
  now = (performance.timeOrigin || performance.timing.navigationStart) + performance.now();
1651
1803
  if (typeof now !== 'number') {
@@ -1691,7 +1843,7 @@ var LeUtils = {
1691
1843
  * @returns {Uint8ClampedArray}
1692
1844
  */
1693
1845
  getImagePixels: function getImagePixels(image) {
1694
- if (!document) {
1846
+ if (typeof window === 'undefined' || !document) {
1695
1847
  return new Uint8ClampedArray();
1696
1848
  }
1697
1849
  var canvas = document.createElement('canvas');
@@ -1721,7 +1873,7 @@ var LeUtils = {
1721
1873
  * @returns {string}
1722
1874
  */
1723
1875
  getColoredImage: function getColoredImage(image, color) {
1724
- if (!document) {
1876
+ if (typeof window === 'undefined' || !document) {
1725
1877
  return LeUtils.getEmptyImageSrc();
1726
1878
  }
1727
1879
  var canvas = document.createElement('canvas');
@@ -2039,7 +2191,7 @@ var LeUtils = {
2039
2191
  * @returns {string}
2040
2192
  */
2041
2193
  btoa: function (_btoa) {
2042
- function btoa(_x12) {
2194
+ function btoa(_x14) {
2043
2195
  return _btoa.apply(this, arguments);
2044
2196
  }
2045
2197
  btoa.toString = function () {
@@ -2059,7 +2211,7 @@ var LeUtils = {
2059
2211
  * @returns {string}
2060
2212
  */
2061
2213
  atob: function (_atob) {
2062
- function atob(_x13) {
2214
+ function atob(_x15) {
2063
2215
  return _atob.apply(this, arguments);
2064
2216
  }
2065
2217
  atob.toString = function () {
@@ -2154,6 +2306,9 @@ var LeUtils = {
2154
2306
  * @param {string} [mimeType]
2155
2307
  */
2156
2308
  downloadFile: function downloadFile(base64string, fileName, mimeType) {
2309
+ if (typeof window === 'undefined' || !document) {
2310
+ return;
2311
+ }
2157
2312
  var link = document.createElement('a');
2158
2313
  link.setAttribute('download', typeof fileName === 'string' ? fileName : 'file');
2159
2314
  link.href = 'data:' + mimeType + ';base64,' + base64string;
@@ -2641,6 +2796,14 @@ var LeUtils = {
2641
2796
  * @returns {{worker: Worker, sendMessage: function(Object, {timeout: number|undefined}|undefined): Promise<Object>}}
2642
2797
  */
2643
2798
  createWorkerThread: function createWorkerThread(name) {
2799
+ if (typeof window === 'undefined' || typeof Worker === 'undefined') {
2800
+ return {
2801
+ worker: null,
2802
+ sendMessage: new Promise(function (resolve, reject) {
2803
+ reject('Workers are not supported in this environment');
2804
+ })
2805
+ };
2806
+ }
2644
2807
  var worker = new Worker('/workers/' + name + '.worker.js');
2645
2808
  var listeners = {};
2646
2809
  var addListener = function addListener(id, callback) {
@@ -2700,15 +2863,6 @@ var LeUtils = {
2700
2863
  return workers[workerName].sendMessage(data, options);
2701
2864
  };
2702
2865
  }(),
2703
- /**
2704
- * Returns a deep copy of the given value.
2705
- *
2706
- * @param {*} value
2707
- * @returns {*}
2708
- */
2709
- clone: function clone(value) {
2710
- return CloneDeep(value, true);
2711
- },
2712
2866
  /**
2713
2867
  * Purges the given email address, returning an empty string if it's invalid.
2714
2868
  *
@@ -2728,6 +2882,11 @@ var LeUtils = {
2728
2882
  * @returns {boolean}
2729
2883
  */
2730
2884
  isFocusClear: function () {
2885
+ if (typeof window === 'undefined' || !document) {
2886
+ return function () {
2887
+ return true;
2888
+ };
2889
+ }
2731
2890
  var inputTypes = ['text', 'search', 'email', 'number', 'password', 'tel', 'time', 'url', 'week', 'month', 'date', 'datetime-local'];
2732
2891
  return function () {
2733
2892
  var _document, _document2;
@@ -2743,11 +2902,11 @@ var LeUtils = {
2743
2902
  var userLocale = null;
2744
2903
  return function () {
2745
2904
  if (userLocale === null) {
2746
- userLocale = function () {
2747
- if (typeof window === 'undefined') {
2905
+ userLocale = function (_navigator$languages, _navigator) {
2906
+ if (typeof window === 'undefined' || !navigator) {
2748
2907
  return 'en-US';
2749
2908
  }
2750
- var locales = window.navigator.languages;
2909
+ var locales = (_navigator$languages = (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.languages) !== null && _navigator$languages !== void 0 ? _navigator$languages : [];
2751
2910
  if (!IS_ARRAY(locales) || locales.length <= 0) {
2752
2911
  return 'en-US';
2753
2912
  }
@@ -2780,8 +2939,8 @@ var LeUtils = {
2780
2939
  if (userLocaleDateFormat === null) {
2781
2940
  userLocaleDateFormat = function () {
2782
2941
  var _char = '/';
2783
- if (typeof window !== 'undefined' && typeof Intl !== 'undefined' && typeof Intl.DateTimeFormat !== 'undefined') {
2784
- var formattedDate = new Intl.DateTimeFormat(LeUtils.getUserLocale()).format();
2942
+ if (typeof window !== 'undefined' && typeof window.Intl !== 'undefined' && typeof window.Intl.DateTimeFormat !== 'undefined') {
2943
+ var formattedDate = new window.Intl.DateTimeFormat(LeUtils.getUserLocale()).format();
2785
2944
  if (formattedDate.includes('-')) {
2786
2945
  _char = '-';
2787
2946
  } else if (formattedDate.includes('. ')) {