@kizmann/pico-js 0.4.15 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,427 @@
1
+ # Any Utility
2
+
3
+ The `Any` class provides a wide range of utility functions for various data type checks, conversions, and operations such as debounce and throttle. This class is part of a larger utility library and can be imported as shown below.
4
+
5
+ Here is a list of all headlines with anchor links:
6
+
7
+ - [isEmpty](#isempty)
8
+ - [isNull](#isnull)
9
+ - [isEqual](#isequal)
10
+ - [isString](#isstring)
11
+ - [isNumber](#isnumber)
12
+ - [isBool](#isbool)
13
+ - [isFunction](#isfunction)
14
+ - [isObject](#isobject)
15
+ - [isPlain](#isplain)
16
+ - [isArray](#isarray)
17
+ - [isDate](#isdate)
18
+ - [string](#string)
19
+ - [convertString](#convertstring)
20
+ - [integer](#integer)
21
+ - [float](#float)
22
+ - [bool](#bool)
23
+ - [boolean](#boolean)
24
+ - [convertBool](#convertbool)
25
+ - [convertBoolean](#convertboolean)
26
+ - [convertDatetime](#convertdatetime)
27
+ - [vals](#vals)
28
+ - [keys](#keys)
29
+ - [async](#async)
30
+ - [delay](#delay)
31
+ - [debounce](#debounce)
32
+ - [throttle](#throttle)
33
+ - [framerate](#framerate)
34
+ - [form](#form)
35
+
36
+ You can click on any of these links to navigate to the respective section in the documentation.
37
+
38
+ ## Import
39
+
40
+ ```javascript
41
+ import { Any } from "../index.js";
42
+ ```
43
+
44
+ ## Methods
45
+
46
+ ### [isEmpty](#isempty)
47
+
48
+ Checks if a value is empty.
49
+
50
+ - **Parameters**:
51
+ - `val`: The value to check.
52
+ - **Returns**: `true` if the value is empty, `false` otherwise.
53
+
54
+ ```javascript
55
+ Any.isEmpty(''); // true
56
+ Any.isEmpty([]); // true
57
+ Any.isEmpty({}); // true
58
+ Any.isEmpty(0); // false
59
+ ```
60
+
61
+ ### [isNull](#isnull)
62
+
63
+ Checks if a value is `null`.
64
+
65
+ - **Parameters**:
66
+ - `val`: The value to check.
67
+ - **Returns**: `true` if the value is `null`, `false` otherwise.
68
+
69
+ ```javascript
70
+ Any.isNull(null); // true
71
+ Any.isNull(undefined); // false
72
+ ```
73
+
74
+ ### [isEqual](#isequal)
75
+
76
+ Checks if two values are equal.
77
+
78
+ - **Parameters**:
79
+ - `obj`: The first value.
80
+ - `val`: The second value.
81
+ - **Returns**: `true` if the values are equal, `false` otherwise.
82
+
83
+ ```javascript
84
+ Any.isEqual(1, '1'); // false
85
+ Any.isEqual({a: 1}, {a: 1}); // true
86
+ ```
87
+
88
+ ### [isString](#isstring)
89
+
90
+ Checks if a value is a string.
91
+
92
+ - **Parameters**:
93
+ - `val`: The value to check.
94
+ - **Returns**: `true` if the value is a string, `false` otherwise.
95
+
96
+ ```javascript
97
+ Any.isString('hello'); // true
98
+ Any.isString(123); // false
99
+ ```
100
+
101
+ ### [isNumber](#isnumber)
102
+
103
+ Checks if a value is a number or a numeric string.
104
+
105
+ - **Parameters**:
106
+ - `val`: The value to check.
107
+ - **Returns**: `true` if the value is a number or numeric string, `false` otherwise.
108
+
109
+ ```javascript
110
+ Any.isNumber(123); // true
111
+ Any.isNumber('123'); // true
112
+ Any.isNumber('abc'); // false
113
+ ```
114
+
115
+ ### [isBool](#isbool)
116
+
117
+ Checks if a value is a boolean or a boolean string.
118
+
119
+ - **Parameters**:
120
+ - `val`: The value to check.
121
+ - **Returns**: `true` if the value is a boolean or boolean string, `false` otherwise.
122
+
123
+ ```javascript
124
+ Any.isBool(true); // true
125
+ Any.isBool('false'); // true
126
+ Any.isBool('yes'); // false
127
+ ```
128
+
129
+ ### [isFunction](#isfunction)
130
+
131
+ Checks if a value is a function.
132
+
133
+ - **Parameters**:
134
+ - `val`: The value to check.
135
+ - **Returns**: `true` if the value is a function, `false` otherwise.
136
+
137
+ ```javascript
138
+ Any.isFunction(function() {}); // true
139
+ Any.isFunction(() => {}); // true
140
+ ```
141
+
142
+ ### [isObject](#isobject)
143
+
144
+ Checks if a value is an object.
145
+
146
+ - **Parameters**:
147
+ - `val`: The value to check.
148
+ - **Returns**: `true` if the value is an object, `false` otherwise.
149
+
150
+ ```javascript
151
+ Any.isObject({}); // true
152
+ Any.isObject(null); // false
153
+ ```
154
+
155
+ ### [isPlain](#isplain)
156
+
157
+ Checks if a value is a plain object.
158
+
159
+ - **Parameters**:
160
+ - `val`: The value to check.
161
+ - **Returns**: `true` if the value is a plain object, `false` otherwise.
162
+
163
+ ```javascript
164
+ Any.isPlain({}); // true
165
+ Any.isPlain(new Date()); // false
166
+ ```
167
+
168
+ ### [isArray](#isarray)
169
+
170
+ Checks if a value is an array.
171
+
172
+ - **Parameters**:
173
+ - `val`: The value to check.
174
+ - **Returns**: `true` if the value is an array, `false` otherwise.
175
+
176
+ ```javascript
177
+ Any.isArray([]); // true
178
+ Any.isArray({}); // false
179
+ ```
180
+
181
+ ### [isDate](#isdate)
182
+
183
+ Checks if a value is a `Date` object.
184
+
185
+ - **Parameters**:
186
+ - `val`: The value to check.
187
+ - **Returns**: `true` if the value is a `Date` object, `false` otherwise.
188
+
189
+ ```javascript
190
+ Any.isDate(new Date()); // true
191
+ Any.isDate('2021-01-01'); // false
192
+ ```
193
+
194
+ ### [string](#string)
195
+
196
+ Converts a value to a string.
197
+
198
+ - **Parameters**:
199
+ - `val`: The value to convert.
200
+ - **Returns**: The value as a string.
201
+
202
+ ```javascript
203
+ Any.string(123); // '123'
204
+ ```
205
+
206
+ ### [convertString](#convertstring)
207
+
208
+ Converts a value to a string or returns a default string if the value is empty.
209
+
210
+ - **Parameters**:
211
+ - `val`: The value to convert.
212
+ - `empty`: The default string if the value is empty (default is `'-'`).
213
+ - **Returns**: The value as a string or the default string if empty.
214
+
215
+ ```javascript
216
+ Any.convertString(123); // '123'
217
+ Any.convertString('', '-'); // '-'
218
+ ```
219
+
220
+ ### [integer](#integer)
221
+
222
+ Converts a value to an integer.
223
+
224
+ - **Parameters**:
225
+ - `val`: The value to convert.
226
+ - **Returns**: The value as an integer.
227
+
228
+ ```javascript
229
+ Any.integer('123'); // 123
230
+ ```
231
+
232
+ ### [float](#float)
233
+
234
+ Converts a value to a float.
235
+
236
+ - **Parameters**:
237
+ - `val`: The value to convert.
238
+ - **Returns**: The value as a float.
239
+
240
+ ```javascript
241
+ Any.float('123.45'); // 123.45
242
+ ```
243
+
244
+ ### [bool](#bool)
245
+
246
+ Converts a value to a boolean.
247
+
248
+ - **Parameters**:
249
+ - `val`: The value to convert.
250
+ - **Returns**: The value as a boolean.
251
+
252
+ ```javascript
253
+ Any.bool('true'); // true
254
+ Any.bool(0); // false
255
+ ```
256
+
257
+ ### [boolean](#boolean)
258
+
259
+ Alias for `bool`.
260
+
261
+ - **Parameters**:
262
+ - `val`: The value to convert.
263
+ - **Returns**: The value as a boolean.
264
+
265
+ ```javascript
266
+ Any.boolean('yes'); // true
267
+ Any.boolean('no'); // false
268
+ ```
269
+
270
+ ### [convertBool](#convertbool)
271
+
272
+ Converts a value to a boolean string representation.
273
+
274
+ - **Parameters**:
275
+ - `val`: The value to convert.
276
+ - `yes`: The string to return if the value is truthy (default is `'Yes'`).
277
+ - `no`: The string to return if the value is falsy (default is `'No'`).
278
+ - **Returns**: The string representation of the boolean value.
279
+
280
+ ```javascript
281
+ Any.convertBool(true); // 'Yes'
282
+ Any.convertBool(false); // 'No'
283
+ ```
284
+
285
+ ### [convertBoolean](#convertboolean)
286
+
287
+ Alias for `convertBool`.
288
+
289
+ - **Parameters**:
290
+ - `val`: The value to convert.
291
+ - `yes`: The string to return if the value is truthy (default is `'Yes'`).
292
+ - `no`: The string to return if the value is falsy (default is `'No'`).
293
+ - **Returns**: The string representation of the boolean value.
294
+
295
+ ```javascript
296
+ Any.convertBoolean('1'); // 'Yes'
297
+ Any.convertBoolean('0'); // 'No'
298
+ ```
299
+
300
+ ### [convertDatetime](#convertdatetime)
301
+
302
+ Converts a value to a formatted datetime string.
303
+
304
+ - **Parameters**:
305
+ - `val`: The value to convert.
306
+ - `format`: The datetime format (default is `'YYYY-MM-DD hh:mm:ss'`).
307
+ - `empty`: The default string if the value is empty (default is `'-'`).
308
+ - **Returns**: The formatted datetime string or the default string if empty.
309
+
310
+ ```javascript
311
+ Any.convertDatetime(new Date(), 'MM/DD/YYYY'); // '05/28/2024'
312
+ ```
313
+
314
+ ### [vals](#vals)
315
+
316
+ Gets the values of an object.
317
+
318
+ - **Parameters**:
319
+ - `obj`: The object to get values from.
320
+ - **Returns**: An array of the object's values.
321
+
322
+ ```javascript
323
+ Any.vals({a: 1, b: 2}); // [1, 2]
324
+ ```
325
+
326
+ ### [keys](#keys)
327
+
328
+ Gets the keys of an object.
329
+
330
+ - **Parameters**:
331
+ - `obj`: The object to get keys from.
332
+ - **Returns**: An array of the object's keys.
333
+
334
+ ```javascript
335
+ Any.keys({a: 1, b: 2}); // ['a', 'b']
336
+ ```
337
+
338
+ ### [async](#async)
339
+
340
+ Executes a callback asynchronously.
341
+
342
+ - **Parameters**:
343
+ - `callback`: The callback function.
344
+ - `...args`: The arguments to pass to the callback.
345
+ - **Returns**: The `Any` class.
346
+
347
+ ```javascript
348
+ Any.async(() => console.log('Async call'));
349
+ ```
350
+
351
+ ### [delay](#delay)
352
+
353
+ Executes a callback after a delay.
354
+
355
+ - **Parameters**:
356
+ - `callback`: The callback function.
357
+ - `delay`: The delay in milliseconds (default is `100`).
358
+ - `...args`: The arguments to pass to the callback.
359
+ - **Returns**: The `Any` class.
360
+
361
+ ```javascript
362
+ Any.delay(() => console.log('Delayed call'), 500);
363
+ ```
364
+
365
+ ### [debounce](#debounce)
366
+
367
+ Creates a debounced function that delays invoking the callback
368
+
369
+ until after the specified delay.
370
+
371
+ - **Parameters**:
372
+ - `callback`: The callback function.
373
+ - `delay`: The delay in milliseconds (default is `100`).
374
+ - `ref`: Optional reference to store the debounce timer.
375
+ - **Returns**: A debounced function.
376
+
377
+ ```javascript
378
+ const debouncedFn = Any.debounce(() => console.log('Debounced call'), 300);
379
+ debouncedFn();
380
+ ```
381
+
382
+ ### [throttle](#throttle)
383
+
384
+ Creates a throttled function that only invokes the callback at most once per every specified delay.
385
+
386
+ - **Parameters**:
387
+ - `callback`: The callback function.
388
+ - `delay`: The delay in milliseconds (default is `100`).
389
+ - `ref`: Optional reference to store the throttle state.
390
+ - **Returns**: A throttled function.
391
+
392
+ ```javascript
393
+ const throttledFn = Any.throttle(() => console.log('Throttled call'), 300);
394
+ throttledFn();
395
+ ```
396
+
397
+ ### [framerate](#framerate)
398
+
399
+ Creates a function that invokes the callback at a specified frame rate.
400
+
401
+ - **Parameters**:
402
+ - `callback`: The callback function.
403
+ - `rate`: The frame rate in frames per second (default is `30`).
404
+ - `ref`: Optional reference to store the last invocation time.
405
+ - **Returns**: A function that enforces the specified frame rate.
406
+
407
+ ```javascript
408
+ const frameRateFn = Any.framerate(() => console.log('Frame rate call'), 60);
409
+ frameRateFn();
410
+ ```
411
+
412
+ ### [form](#form)
413
+
414
+ Creates a `FormData` object from a plain object.
415
+
416
+ - **Parameters**:
417
+ - `obj`: The plain object to convert.
418
+ - **Returns**: A `FormData` object.
419
+
420
+ ```javascript
421
+ const formData = Any.form({a: 1, b: 2});
422
+ ```
423
+
424
+ ## License
425
+
426
+ This documentation is based on the source code of the `Any` class. The `Any` class is part of a utility library. Please refer to the library's license for more information.
427
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "0.4.15",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -240,6 +240,10 @@ export class Arr
240
240
 
241
241
  static includes(arr, val)
242
242
  {
243
+ if ( Any.isString(val) ) {
244
+ val = [val];
245
+ }
246
+
243
247
  let result = false;
244
248
 
245
249
  for ( let key of Any.vals(arr) ) {
@@ -98,7 +98,7 @@ export class Num
98
98
  let value = num.toString();
99
99
 
100
100
  if ( fixed !== null && fixed !== - 1 ) {
101
- value = num.toFixed(fixed);
101
+ value = Any.float(num).toFixed(fixed);
102
102
  }
103
103
 
104
104
  let totals = value.replace(/\.[0-9]+$/, ''),