@lowentry/utils 1.24.2 → 1.25.2

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)) {
@@ -3026,13 +3084,9 @@ var LeUtils = {
3026
3084
  * @returns {Uint8Array}
3027
3085
  */
3028
3086
  base64ToBytes: function base64ToBytes(base64string) {
3029
- var binary = LeUtils.atob(base64string.trim());
3030
- var len = binary.length;
3031
- var data = new Uint8Array(len);
3032
- for (var i = 0; i < len; i++) {
3033
- data[i] = binary.charCodeAt(i);
3034
- }
3035
- return data;
3087
+ return Uint8Array.from(LeUtils.atob(base64string.trim()), function (c) {
3088
+ return c.charCodeAt(0);
3089
+ });
3036
3090
  },
3037
3091
  /**
3038
3092
  * Converts bytes into a base64 string.
@@ -3041,13 +3095,7 @@ var LeUtils = {
3041
3095
  * @returns {string}
3042
3096
  */
3043
3097
  bytesToBase64: function bytesToBase64(arraybuffer) {
3044
- var bytes = new Uint8Array(arraybuffer);
3045
- var len = bytes.byteLength;
3046
- var binary = '';
3047
- for (var i = 0; i < len; i++) {
3048
- binary += String.fromCharCode(bytes[i]);
3049
- }
3050
- return LeUtils.btoa(binary);
3098
+ return LeUtils.btoa(String.fromCharCode.apply(String, _toConsumableArray(new Uint8Array(arraybuffer))));
3051
3099
  },
3052
3100
  /**
3053
3101
  * Downloads the given base64 string as a file.
@@ -4098,5 +4146,5 @@ var TreeSet = /*#__PURE__*/function () {
4098
4146
  }]);
4099
4147
  }();
4100
4148
 
4101
- 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 };
4149
+ 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 };
4102
4150
  //# sourceMappingURL=index.js.map