@lowentry/utils 1.25.2 → 2.0.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/src/LeTypes.js DELETED
@@ -1,254 +0,0 @@
1
- const REGEX_ALL_NON_FLOAT_CHARACTERS = /[^0-9.\-]/g;
2
-
3
-
4
- /**
5
- * Returns true if the value is set (not undefined and not null).
6
- *
7
- * @param {*} value
8
- * @returns {boolean}
9
- */
10
- export const ISSET = (value) => (typeof value !== 'undefined') && (value !== null);
11
-
12
-
13
- /**
14
- * Returns true if the value is an array.
15
- *
16
- * @param {*} value
17
- * @returns {boolean}
18
- */
19
- export const IS_ARRAY = (value) => Array.isArray(value);
20
-
21
- /**
22
- * Ensures the given value is an array (returns the value wrapped in an array if it's not).
23
- *
24
- * @param {*} value
25
- * @returns {*[]}
26
- */
27
- export const ARRAY = (value) => IS_ARRAY(value) ? value : ((typeof value !== 'undefined') ? [value] : []);
28
-
29
-
30
- /**
31
- * Returns true if the value is an object.
32
- *
33
- * @param {*} value
34
- * @returns {boolean}
35
- */
36
- export const IS_OBJECT = (value) => (typeof value === 'object') && (value !== null) && !Array.isArray(value);
37
-
38
- /**
39
- * Ensures the given value is an object (returns an empty object if it's not).
40
- *
41
- * @param value
42
- * @returns {Object}
43
- */
44
- export const OBJECT = (value) => IS_OBJECT(value) ? value : {};
45
-
46
-
47
- /**
48
- * Ensures the given value is a string (casts it to a string if it's not, null and undefined will return an empty string).
49
- *
50
- * @param {*} value
51
- * @returns {string}
52
- */
53
- export const STRING = (value) => ISSET(value) ? ('' + value) : '';
54
-
55
- /**
56
- * Returns the first non-null non-undefined value as a string.
57
- *
58
- * @param {*} values
59
- * @returns {string}
60
- */
61
- export const STRING_ANY = (...values) =>
62
- {
63
- for(let value of values)
64
- {
65
- if(ISSET(value))
66
- {
67
- return '' + value;
68
- }
69
- }
70
- return '';
71
- };
72
-
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
-
141
- /**
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).
143
- *
144
- * @param {*} value
145
- * @returns {number}
146
- */
147
- export const INT = (value) => Math.round(FLOAT(value));
148
-
149
- /**
150
- * Returns the first non-null non-undefined int-castable value as an integer.
151
- *
152
- * @param {*} values
153
- * @returns {number}
154
- */
155
- export const INT_ANY = (...values) => Math.round(FLOAT_ANY(...values));
156
-
157
-
158
- /**
159
- * Ensures the given value is a float (attempts to cast it to a float if it's not, null and undefined will return 0).
160
- *
161
- * @param {*} value
162
- * @returns {number}
163
- */
164
- export const FLOAT = (value) =>
165
- {
166
- const v = +value;
167
- if(!isNaN(v))
168
- {
169
- return v;
170
- }
171
- return 0;
172
- };
173
-
174
- /**
175
- * Returns the first non-null non-undefined float-castable value as a float.
176
- *
177
- * @param {*} values
178
- * @returns {number}
179
- */
180
- export const FLOAT_ANY = (...values) =>
181
- {
182
- for(let value of values)
183
- {
184
- if(value !== null)
185
- {
186
- const v = +value;
187
- if(!isNaN(v))
188
- {
189
- return v;
190
- }
191
- }
192
- }
193
- return 0;
194
- };
195
-
196
-
197
- /**
198
- * Ensures the given value is an integer (attempts to cast it to an integer if it's not, null and undefined will return 0).
199
- * This version is less strict than INT, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
200
- *
201
- * @param {*} value
202
- * @returns {number}
203
- */
204
- export const INT_LAX = (value) => Math.round(FLOAT_LAX(value));
205
-
206
- /**
207
- * Returns the first non-null non-undefined int-castable value as an integer.
208
- * This version is less strict than INT_ANY, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
209
- *
210
- * @param {*} values
211
- * @returns {number}
212
- */
213
- export const INT_LAX_ANY = (...values) => Math.round(FLOAT_LAX_ANY(...values));
214
-
215
-
216
- /**
217
- * Ensures the given value is a float (attempts to cast it to a float if it's not, null and undefined will return 0).
218
- * This version is less strict than FLOAT, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
219
- *
220
- * @param {*} value
221
- * @returns {number}
222
- */
223
- export const FLOAT_LAX = (value) =>
224
- {
225
- const v = (typeof value === 'number') ? value : parseFloat((value + '').replace(REGEX_ALL_NON_FLOAT_CHARACTERS, ''));
226
- if(!isNaN(v))
227
- {
228
- return v;
229
- }
230
- return 0;
231
- };
232
-
233
- /**
234
- * Returns the first non-null non-undefined float-castable value as a float.
235
- * This version is less strict than FLOAT_ANY, as it relies on parseFloat instead of on +value, meaning that it will accept strings that contain a number followed by other characters, which +value doesn't.
236
- *
237
- * @param {*} values
238
- * @returns {number}
239
- */
240
- export const FLOAT_LAX_ANY = (...values) =>
241
- {
242
- for(let value of values)
243
- {
244
- if(value !== null)
245
- {
246
- const v = (typeof value === 'number') ? value : parseFloat((value + '').replace(REGEX_ALL_NON_FLOAT_CHARACTERS, ''));
247
- if(!isNaN(v))
248
- {
249
- return v;
250
- }
251
- }
252
- }
253
- return 0;
254
- };