@lowentry/utils 1.10.4 → 1.10.6

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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@lowentry/utils",
3
- "version": "1.10.4",
3
+ "version": "1.10.6",
4
4
  "private": false,
5
5
  "type": "module",
6
+ "main": "./index.js",
6
7
  "description": "Provides utilities for general JavaScript development.",
7
8
  "author": "Low Entry",
8
9
  "license": "MIT",
package/src/LeUtils.js CHANGED
@@ -45,6 +45,67 @@ export const LeUtils = {
45
45
  */
46
46
  equals:FastDeepEqual,
47
47
 
48
+ /**
49
+ * Returns a deep copy of the given value.
50
+ *
51
+ * @param {*} value
52
+ * @returns {*}
53
+ */
54
+ clone:
55
+ (value) => CloneDeep(value, true),
56
+
57
+ /**
58
+ * Executes the given callback when the document is ready.
59
+ *
60
+ * @param {Function} callback
61
+ * @returns {{remove:Function}}
62
+ */
63
+ onDomReady:
64
+ (callback) =>
65
+ {
66
+ if((typeof window === 'undefined') || !document)
67
+ {
68
+ // no document, so we can't wait for it to be ready
69
+ return {
70
+ remove:() =>
71
+ {
72
+ },
73
+ };
74
+ }
75
+
76
+ if((document.readyState === 'interactive') || (document.readyState === 'complete'))
77
+ {
78
+ return LeUtils.setTimeout(callback, 0);
79
+ }
80
+ else
81
+ {
82
+ let listening = true;
83
+ const callbackWrapper = () =>
84
+ {
85
+ if(listening)
86
+ {
87
+ listening = false;
88
+ document.removeEventListener('DOMContentLoaded', callbackWrapper);
89
+ callback();
90
+ }
91
+ };
92
+
93
+ document.addEventListener('DOMContentLoaded', callbackWrapper);
94
+
95
+ return {
96
+ remove:
97
+ () =>
98
+ {
99
+ if(listening)
100
+ {
101
+ listening = false;
102
+ document.removeEventListener('DOMContentLoaded', callbackWrapper);
103
+ }
104
+ },
105
+ };
106
+ }
107
+ },
108
+
48
109
  /**
49
110
  * Parses the given version string, and returns an object with the major, minor, and patch numbers, as well as some comparison functions.
50
111
  *
@@ -925,6 +986,15 @@ export const LeUtils = {
925
986
  setTimeout:
926
987
  (callback, ms) =>
927
988
  {
989
+ if(typeof window === 'undefined')
990
+ {
991
+ return {
992
+ remove:() =>
993
+ {
994
+ },
995
+ };
996
+ }
997
+
928
998
  ms = FLOAT_LAX(ms);
929
999
 
930
1000
  let lastTime = performance.now();
@@ -986,6 +1056,15 @@ export const LeUtils = {
986
1056
  }
987
1057
  }
988
1058
 
1059
+ if(typeof window === 'undefined')
1060
+ {
1061
+ return {
1062
+ remove:() =>
1063
+ {
1064
+ },
1065
+ };
1066
+ }
1067
+
989
1068
  let lastTime = performance.now();
990
1069
  let handler = setInterval(() =>
991
1070
  {
@@ -1030,6 +1109,15 @@ export const LeUtils = {
1030
1109
  setAnimationFrameTimeout:
1031
1110
  (callback, frames = 1) =>
1032
1111
  {
1112
+ if(typeof window === 'undefined')
1113
+ {
1114
+ return {
1115
+ remove:() =>
1116
+ {
1117
+ },
1118
+ };
1119
+ }
1120
+
1033
1121
  frames = INT_LAX_ANY(frames, 1);
1034
1122
 
1035
1123
  let run = true;
@@ -1106,6 +1194,15 @@ export const LeUtils = {
1106
1194
  }
1107
1195
  }
1108
1196
 
1197
+ if(typeof window === 'undefined')
1198
+ {
1199
+ return {
1200
+ remove:() =>
1201
+ {
1202
+ },
1203
+ };
1204
+ }
1205
+
1109
1206
  let run = true;
1110
1207
  let requestAnimationFrameId = null;
1111
1208
  let lastTime = performance.now();
@@ -1535,6 +1632,10 @@ export const LeUtils = {
1535
1632
  let now;
1536
1633
  try
1537
1634
  {
1635
+ if(typeof window === 'undefined')
1636
+ {
1637
+ throw new Error();
1638
+ }
1538
1639
  // noinspection JSDeprecatedSymbols
1539
1640
  now = (performance.timeOrigin || performance.timing.navigationStart) + performance.now();
1540
1641
  if(typeof now !== 'number')
@@ -1639,6 +1740,10 @@ export const LeUtils = {
1639
1740
  {
1640
1741
  try
1641
1742
  {
1743
+ if(typeof window === 'undefined')
1744
+ {
1745
+ throw new Error();
1746
+ }
1642
1747
  // noinspection JSDeprecatedSymbols
1643
1748
  now = (performance.timeOrigin || performance.timing.navigationStart) + performance.now();
1644
1749
  if(typeof now !== 'number')
@@ -1698,7 +1803,7 @@ export const LeUtils = {
1698
1803
  getImagePixels:
1699
1804
  (image) =>
1700
1805
  {
1701
- if(!document)
1806
+ if((typeof window === 'undefined') || !document)
1702
1807
  {
1703
1808
  return new Uint8ClampedArray();
1704
1809
  }
@@ -1738,7 +1843,7 @@ export const LeUtils = {
1738
1843
  getColoredImage:
1739
1844
  (image, color) =>
1740
1845
  {
1741
- if(!document)
1846
+ if((typeof window === 'undefined') || !document)
1742
1847
  {
1743
1848
  return LeUtils.getEmptyImageSrc();
1744
1849
  }
@@ -2257,6 +2362,10 @@ export const LeUtils = {
2257
2362
  downloadFile:
2258
2363
  (base64string, fileName, mimeType) =>
2259
2364
  {
2365
+ if((typeof window === 'undefined') || !document)
2366
+ {
2367
+ return;
2368
+ }
2260
2369
  const link = document.createElement('a');
2261
2370
  link.setAttribute('download', (typeof fileName === 'string') ? fileName : 'file');
2262
2371
  link.href = 'data:' + mimeType + ';base64,' + base64string;
@@ -2824,6 +2933,17 @@ export const LeUtils = {
2824
2933
  createWorkerThread:
2825
2934
  (name) =>
2826
2935
  {
2936
+ if((typeof window === 'undefined') || (typeof Worker === 'undefined'))
2937
+ {
2938
+ return {
2939
+ worker: null,
2940
+ sendMessage:new Promise((resolve, reject) =>
2941
+ {
2942
+ reject('Workers are not supported in this environment');
2943
+ }),
2944
+ };
2945
+ }
2946
+
2827
2947
  const worker = new Worker('/workers/' + name + '.worker.js');
2828
2948
  let listeners = {};
2829
2949
 
@@ -2901,15 +3021,6 @@ export const LeUtils = {
2901
3021
  };
2902
3022
  })(),
2903
3023
 
2904
- /**
2905
- * Returns a deep copy of the given value.
2906
- *
2907
- * @param {*} value
2908
- * @returns {*}
2909
- */
2910
- clone:
2911
- (value) => CloneDeep(value, true),
2912
-
2913
3024
  /**
2914
3025
  * Purges the given email address, returning an empty string if it's invalid.
2915
3026
  *
@@ -2934,6 +3045,10 @@ export const LeUtils = {
2934
3045
  */
2935
3046
  isFocusClear:(() =>
2936
3047
  {
3048
+ if((typeof window === 'undefined') || !document)
3049
+ {
3050
+ return () => true;
3051
+ }
2937
3052
  const inputTypes = ['text', 'search', 'email', 'number', 'password', 'tel', 'time', 'url', 'week', 'month', 'date', 'datetime-local'];
2938
3053
  return () => !((document?.activeElement?.tagName?.toLowerCase() === 'input') && inputTypes.includes(document?.activeElement?.type?.toLowerCase()));
2939
3054
  })(),
@@ -2952,11 +3067,11 @@ export const LeUtils = {
2952
3067
  {
2953
3068
  userLocale = (() =>
2954
3069
  {
2955
- if(typeof window === 'undefined')
3070
+ if((typeof window === 'undefined') || !navigator)
2956
3071
  {
2957
3072
  return 'en-US';
2958
3073
  }
2959
- let locales = window.navigator.languages;
3074
+ let locales = navigator?.languages ?? [];
2960
3075
  if(!IS_ARRAY(locales) || (locales.length <= 0))
2961
3076
  {
2962
3077
  return 'en-US';
@@ -2993,9 +3108,9 @@ export const LeUtils = {
2993
3108
  userLocaleDateFormat = (() =>
2994
3109
  {
2995
3110
  let char = '/';
2996
- if((typeof window !== 'undefined') && (typeof Intl !== 'undefined') && (typeof Intl.DateTimeFormat !== 'undefined'))
3111
+ if((typeof window !== 'undefined') && (typeof window.Intl !== 'undefined') && (typeof window.Intl.DateTimeFormat !== 'undefined'))
2997
3112
  {
2998
- const formattedDate = new Intl.DateTimeFormat(LeUtils.getUserLocale()).format();
3113
+ const formattedDate = new window.Intl.DateTimeFormat(LeUtils.getUserLocale()).format();
2999
3114
  if(formattedDate.includes('-'))
3000
3115
  {
3001
3116
  char = '-';