@lowentry/utils 1.24.1 → 1.25.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.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  export declare function ARRAY(value: any): any[];
2
2
 
3
+ export declare function BOOL(value: any): boolean;
4
+
5
+ export declare function BOOL_ANY(...values: any): boolean;
6
+
3
7
  /**
4
8
  * A simple event emitter class that allows you to register listeners for events, emit events, and remove listeners.
5
9
  */
package/index.js CHANGED
@@ -95,6 +95,64 @@ var STRING_ANY = function STRING_ANY() {
95
95
  return '';
96
96
  };
97
97
 
98
+ /**
99
+ * Ensures the given value is a boolean.
100
+ *
101
+ * This will work differently than !!value, as it will try to figure out the most logical boolean value from the given value.
102
+ *
103
+ * @param {*} value
104
+ * @returns {boolean}
105
+ */
106
+ var BOOL = function BOOL(value) {
107
+ return BOOL_ANY(value);
108
+ };
109
+
110
+ /**
111
+ * Returns the first non-null non-undefined boolean-castable value as a boolean.
112
+ *
113
+ * @param {*} values
114
+ * @returns {boolean}
115
+ */
116
+ var BOOL_ANY = function BOOL_ANY() {
117
+ for (var _len2 = arguments.length, values = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
118
+ values[_key2] = arguments[_key2];
119
+ }
120
+ for (var _i2 = 0, _values2 = values; _i2 < _values2.length; _i2++) {
121
+ var value = _values2[_i2];
122
+ if (!ISSET(value)) {
123
+ continue;
124
+ }
125
+ if (typeof value === 'boolean') {
126
+ return value;
127
+ }
128
+ if (typeof value === 'number') {
129
+ if (!isNaN(value)) {
130
+ return value !== 0;
131
+ }
132
+ return false;
133
+ }
134
+ if (typeof value === 'string') {
135
+ value = value.toLowerCase().trim();
136
+ if (value === '' || value === 'false' || value === 'no' || value === 'off' || value === '0') {
137
+ return false;
138
+ }
139
+ if (value === 'true' || value === 'yes' || value === 'on' || value === '1') {
140
+ return true;
141
+ }
142
+ var _float = +value;
143
+ if (!isNaN(_float)) {
144
+ return _float !== 0;
145
+ }
146
+ // unrecognized string, let's try the next value, else we return false (after the loop)
147
+ }
148
+ if (IS_ARRAY(value) || IS_OBJECT(value)) {
149
+ return !!value;
150
+ }
151
+ // unrecognized type, let's try the next value, else we return false (after the loop)
152
+ }
153
+ return false;
154
+ };
155
+
98
156
  /**
99
157
  * Ensures the given value is an integer (attempts to cast it to an integer if it's not, null and undefined will return 0).
100
158
  *
@@ -136,11 +194,11 @@ var FLOAT = function FLOAT(value) {
136
194
  * @returns {number}
137
195
  */
138
196
  var FLOAT_ANY = function FLOAT_ANY() {
139
- for (var _len2 = arguments.length, values = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
140
- values[_key2] = arguments[_key2];
197
+ for (var _len3 = arguments.length, values = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
198
+ values[_key3] = arguments[_key3];
141
199
  }
142
- for (var _i2 = 0, _values2 = values; _i2 < _values2.length; _i2++) {
143
- var value = _values2[_i2];
200
+ for (var _i3 = 0, _values3 = values; _i3 < _values3.length; _i3++) {
201
+ var value = _values3[_i3];
144
202
  if (value !== null) {
145
203
  var v = +value;
146
204
  if (!isNaN(v)) {
@@ -196,11 +254,11 @@ var FLOAT_LAX = function FLOAT_LAX(value) {
196
254
  * @returns {number}
197
255
  */
198
256
  var FLOAT_LAX_ANY = function FLOAT_LAX_ANY() {
199
- for (var _len3 = arguments.length, values = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
200
- values[_key3] = arguments[_key3];
257
+ for (var _len4 = arguments.length, values = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
258
+ values[_key4] = arguments[_key4];
201
259
  }
202
- for (var _i3 = 0, _values3 = values; _i3 < _values3.length; _i3++) {
203
- var value = _values3[_i3];
260
+ for (var _i4 = 0, _values4 = values; _i4 < _values4.length; _i4++) {
261
+ var value = _values4[_i4];
204
262
  if (value !== null) {
205
263
  var v = typeof value === 'number' ? value : parseFloat((value + '').replace(REGEX_ALL_NON_FLOAT_CHARACTERS, ''));
206
264
  if (!isNaN(v)) {
@@ -1867,7 +1925,6 @@ var LeUtils = {
1867
1925
  * @returns {{remove:Function}}
1868
1926
  */
1869
1927
  setAnimationFrameInterval: function setAnimationFrameInterval(callback) {
1870
- var _globalThis$performan17, _globalThis$performan18, _globalThis$performan19;
1871
1928
  var intervalFrames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
1872
1929
  var fireImmediately = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1873
1930
  intervalFrames = INT_LAX_ANY(intervalFrames, 1);
@@ -1886,22 +1943,26 @@ var LeUtils = {
1886
1943
  }
1887
1944
  var run = true;
1888
1945
  var requestAnimationFrameId = null;
1889
- var lastTime = (_globalThis$performan17 = globalThis === null || globalThis === void 0 || (_globalThis$performan18 = globalThis.performance) === null || _globalThis$performan18 === void 0 || (_globalThis$performan19 = _globalThis$performan18.now) === null || _globalThis$performan19 === void 0 ? void 0 : _globalThis$performan19.call(_globalThis$performan18)) !== null && _globalThis$performan17 !== void 0 ? _globalThis$performan17 : 0;
1946
+ var lastTimestamp = 0;
1947
+ var totalTime = 0;
1890
1948
  var frames = intervalFrames;
1891
- var _tick2 = function tick() {
1949
+ var _tick2 = function tick(timestamp) {
1892
1950
  if (run) {
1951
+ if (lastTimestamp === 0) {
1952
+ lastTimestamp = timestamp;
1953
+ }
1954
+ totalTime += timestamp - lastTimestamp;
1955
+ lastTimestamp = timestamp;
1956
+ frames--;
1893
1957
  if (frames <= 0) {
1894
- var _globalThis$performan20, _globalThis$performan21, _globalThis$performan22;
1895
- var currentTime = (_globalThis$performan20 = globalThis === null || globalThis === void 0 || (_globalThis$performan21 = globalThis.performance) === null || _globalThis$performan21 === void 0 || (_globalThis$performan22 = _globalThis$performan21.now) === null || _globalThis$performan22 === void 0 ? void 0 : _globalThis$performan22.call(_globalThis$performan21)) !== null && _globalThis$performan20 !== void 0 ? _globalThis$performan20 : 0;
1896
1958
  try {
1897
- callback((currentTime - lastTime) / 1000);
1959
+ callback(totalTime / 1000);
1898
1960
  } catch (e) {
1899
1961
  console.error(e);
1900
1962
  }
1901
- lastTime = currentTime;
1963
+ totalTime = 0;
1902
1964
  frames = intervalFrames;
1903
1965
  }
1904
- frames--;
1905
1966
  if (run) {
1906
1967
  requestAnimationFrameId = globalThis.requestAnimationFrame(_tick2);
1907
1968
  }
@@ -2478,9 +2539,9 @@ var LeUtils = {
2478
2539
  var generateUniqueId = function generateUniqueId() {
2479
2540
  var now;
2480
2541
  try {
2481
- var _globalThis$performan23, _globalThis$performan24, _globalThis$performan25, _globalThis$performan26, _globalThis$performan27;
2542
+ var _globalThis$performan17, _globalThis$performan18, _globalThis$performan19, _globalThis$performan20, _globalThis$performan21;
2482
2543
  // noinspection JSDeprecatedSymbols
2483
- now = ((globalThis === null || globalThis === void 0 || (_globalThis$performan23 = globalThis.performance) === null || _globalThis$performan23 === void 0 ? void 0 : _globalThis$performan23.timeOrigin) || (globalThis === null || globalThis === void 0 || (_globalThis$performan24 = globalThis.performance) === null || _globalThis$performan24 === void 0 || (_globalThis$performan24 = _globalThis$performan24.timing) === null || _globalThis$performan24 === void 0 ? void 0 : _globalThis$performan24.navigationStart) || 0) + ((_globalThis$performan25 = globalThis === null || globalThis === void 0 || (_globalThis$performan26 = globalThis.performance) === null || _globalThis$performan26 === void 0 || (_globalThis$performan27 = _globalThis$performan26.now) === null || _globalThis$performan27 === void 0 ? void 0 : _globalThis$performan27.call(_globalThis$performan26)) !== null && _globalThis$performan25 !== void 0 ? _globalThis$performan25 : 0);
2544
+ now = ((globalThis === null || globalThis === void 0 || (_globalThis$performan17 = globalThis.performance) === null || _globalThis$performan17 === void 0 ? void 0 : _globalThis$performan17.timeOrigin) || (globalThis === null || globalThis === void 0 || (_globalThis$performan18 = globalThis.performance) === null || _globalThis$performan18 === void 0 || (_globalThis$performan18 = _globalThis$performan18.timing) === null || _globalThis$performan18 === void 0 ? void 0 : _globalThis$performan18.navigationStart) || 0) + ((_globalThis$performan19 = globalThis === null || globalThis === void 0 || (_globalThis$performan20 = globalThis.performance) === null || _globalThis$performan20 === void 0 || (_globalThis$performan21 = _globalThis$performan20.now) === null || _globalThis$performan21 === void 0 ? void 0 : _globalThis$performan21.call(_globalThis$performan20)) !== null && _globalThis$performan19 !== void 0 ? _globalThis$performan19 : 0);
2484
2545
  } catch (e) {}
2485
2546
  now = now || (Date.now ? Date.now() : new Date().getTime());
2486
2547
  now = Math.round(now);
@@ -4095,5 +4156,5 @@ var TreeSet = /*#__PURE__*/function () {
4095
4156
  }]);
4096
4157
  }();
4097
4158
 
4098
- export { ARRAY, EventEmitter, FLOAT, FLOAT_ANY, FLOAT_LAX, FLOAT_LAX_ANY, INT, INT_ANY, INT_LAX, INT_LAX_ANY, ISSET, IS_ARRAY, IS_OBJECT, LeUtils, LinkedList, OBJECT, STRING, STRING_ANY, SerializableMap, TreeSet };
4159
+ export { ARRAY, BOOL, BOOL_ANY, EventEmitter, FLOAT, FLOAT_ANY, FLOAT_LAX, FLOAT_LAX_ANY, INT, INT_ANY, INT_LAX, INT_LAX_ANY, ISSET, IS_ARRAY, IS_OBJECT, LeUtils, LinkedList, OBJECT, STRING, STRING_ANY, SerializableMap, TreeSet };
4099
4160
  //# sourceMappingURL=index.js.map