@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowentry/utils",
3
- "version": "1.24.2",
3
+ "version": "1.25.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/LeTypes.js CHANGED
@@ -71,6 +71,73 @@ export const STRING_ANY = (...values) =>
71
71
  };
72
72
 
73
73
 
74
+ /**
75
+ * Ensures the given value is a boolean.
76
+ *
77
+ * This will work differently than !!value, as it will try to figure out the most logical boolean value from the given value.
78
+ *
79
+ * @param {*} value
80
+ * @returns {boolean}
81
+ */
82
+ export const BOOL = (value) =>
83
+ {
84
+ return BOOL_ANY(value);
85
+ };
86
+
87
+ /**
88
+ * Returns the first non-null non-undefined boolean-castable value as a boolean.
89
+ *
90
+ * @param {*} values
91
+ * @returns {boolean}
92
+ */
93
+ export const BOOL_ANY = (...values) =>
94
+ {
95
+ for(let value of values)
96
+ {
97
+ if(!ISSET(value))
98
+ {
99
+ continue;
100
+ }
101
+ if(typeof value === 'boolean')
102
+ {
103
+ return value;
104
+ }
105
+ if(typeof value === 'number')
106
+ {
107
+ if(!isNaN(value))
108
+ {
109
+ return (value !== 0);
110
+ }
111
+ return false;
112
+ }
113
+ if(typeof value === 'string')
114
+ {
115
+ value = value.toLowerCase().trim();
116
+ if((value === '') || (value === 'false') || (value === 'no') || (value === 'off') || (value === '0'))
117
+ {
118
+ return false;
119
+ }
120
+ if((value === 'true') || (value === 'yes') || (value === 'on') || (value === '1'))
121
+ {
122
+ return true;
123
+ }
124
+ const float = +value;
125
+ if(!isNaN(float))
126
+ {
127
+ return (float !== 0);
128
+ }
129
+ // unrecognized string, let's try the next value, else we return false (after the loop)
130
+ }
131
+ if(IS_ARRAY(value) || IS_OBJECT(value))
132
+ {
133
+ return !!value;
134
+ }
135
+ // unrecognized type, let's try the next value, else we return false (after the loop)
136
+ }
137
+ return false;
138
+ };
139
+
140
+
74
141
  /**
75
142
  * Ensures the given value is an integer (attempts to cast it to an integer if it's not, null and undefined will return 0).
76
143
  *
package/src/LeUtils.js CHANGED
@@ -2982,14 +2982,7 @@ export const LeUtils = {
2982
2982
  base64ToBytes:
2983
2983
  (base64string) =>
2984
2984
  {
2985
- const binary = LeUtils.atob(base64string.trim());
2986
- const len = binary.length;
2987
- let data = new Uint8Array(len);
2988
- for(let i = 0; i < len; i++)
2989
- {
2990
- data[i] = binary.charCodeAt(i);
2991
- }
2992
- return data;
2985
+ return Uint8Array.from(LeUtils.atob(base64string.trim()), c => c.charCodeAt(0));
2993
2986
  },
2994
2987
 
2995
2988
  /**
@@ -3001,14 +2994,7 @@ export const LeUtils = {
3001
2994
  bytesToBase64:
3002
2995
  (arraybuffer) =>
3003
2996
  {
3004
- const bytes = new Uint8Array(arraybuffer);
3005
- const len = bytes.byteLength;
3006
- let binary = '';
3007
- for(let i = 0; i < len; i++)
3008
- {
3009
- binary += String.fromCharCode(bytes[i]);
3010
- }
3011
- return LeUtils.btoa(binary);
2997
+ return LeUtils.btoa(String.fromCharCode(...new Uint8Array(arraybuffer)));
3012
2998
  },
3013
2999
 
3014
3000
  /**
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export {LeUtils} from './LeUtils.js';
2
- export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
2
+ export {ISSET, IS_ARRAY, ARRAY, IS_OBJECT, OBJECT, STRING, STRING_ANY, BOOL, BOOL_ANY, INT, INT_ANY, FLOAT, FLOAT_ANY, INT_LAX, INT_LAX_ANY, FLOAT_LAX, FLOAT_LAX_ANY} from './LeTypes.js';
3
3
  export {EventEmitter} from './classes/EventEmitter.js';
4
4
  export {LinkedList} from './classes/LinkedList.js';
5
5
  export {SerializableMap} from './classes/SerializableMap.js';