@valkyriestudios/utils 12.20.0 → 12.22.0
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/README.md +389 -160
- package/array/dedupe.d.ts +0 -2
- package/array/groupBy.d.ts +0 -1
- package/array/is.d.ts +1 -3
- package/array/isNotEmpty.d.ts +1 -3
- package/array/join.d.ts +0 -2
- package/array/join.js +3 -2
- package/array/mapFn.js +1 -1
- package/array/mapPrimitive.d.ts +0 -2
- package/array/sort.d.ts +0 -2
- package/array/split.d.ts +0 -2
- package/boolean/is.d.ts +1 -3
- package/date/addUTC.d.ts +5 -6
- package/date/diff.d.ts +5 -6
- package/date/endOfUTC.d.ts +4 -5
- package/date/format.d.ts +16 -2
- package/date/format.js +110 -42
- package/date/index.d.ts +2 -1
- package/date/index.js +3 -1
- package/date/is.d.ts +1 -3
- package/date/isFormat.d.ts +0 -1
- package/date/isLeap.d.ts +7 -0
- package/date/isLeap.js +11 -0
- package/date/nowUnix.d.ts +0 -2
- package/date/nowUnixMs.d.ts +0 -2
- package/date/setTimeUTC.d.ts +1 -2
- package/date/startOfUTC.d.ts +4 -5
- package/date/toUTC.d.ts +1 -3
- package/date/toUnix.d.ts +1 -3
- package/deep/freeze.d.ts +1 -3
- package/deep/get.d.ts +5 -7
- package/deep/seal.d.ts +1 -3
- package/deep/set.d.ts +0 -1
- package/equal.d.ts +2 -4
- package/formdata/index.d.ts +2 -1
- package/formdata/index.js +3 -1
- package/formdata/is.d.ts +1 -3
- package/formdata/toObject.d.ts +23 -0
- package/formdata/toObject.js +58 -0
- package/function/is.d.ts +1 -3
- package/function/isAsync.d.ts +1 -3
- package/function/noop.d.ts +0 -2
- package/function/noop.js +1 -2
- package/function/noopresolve.d.ts +0 -2
- package/function/noopreturn.d.ts +0 -2
- package/function/sleep.d.ts +1 -3
- package/hash/fnv1A.d.ts +2 -4
- package/hash/guid.d.ts +0 -2
- package/index.d.ts +39 -10
- package/number/is.d.ts +1 -3
- package/number/isAbove.d.ts +2 -4
- package/number/isAboveOrEqual.d.ts +2 -4
- package/number/isBelow.d.ts +2 -4
- package/number/isBelowOrEqual.d.ts +2 -4
- package/number/isBetween.d.ts +3 -5
- package/number/isBetween.js +4 -6
- package/number/isInteger.d.ts +1 -3
- package/number/isIntegerAbove.d.ts +2 -4
- package/number/isIntegerAboveOrEqual.d.ts +2 -4
- package/number/isIntegerBelow.d.ts +2 -4
- package/number/isIntegerBelowOrEqual.d.ts +2 -4
- package/number/isIntegerBetween.d.ts +3 -5
- package/number/isIntegerBetween.js +4 -6
- package/number/isNumericalNaN.d.ts +1 -3
- package/number/randomBetween.d.ts +2 -4
- package/number/randomIntBetween.d.ts +2 -4
- package/number/round.d.ts +2 -4
- package/number/round.js +5 -3
- package/number/toPercentage.d.ts +4 -6
- package/object/define.d.ts +0 -2
- package/object/is.d.ts +1 -3
- package/object/isNotEmpty.d.ts +2 -6
- package/object/merge.d.ts +2 -4
- package/object/merge.js +2 -3
- package/object/pick.d.ts +0 -2
- package/package.json +1 -1
- package/regexp/is.d.ts +1 -3
- package/regexp/sanitize.d.ts +1 -3
- package/regexp/sanitize.js +2 -1
- package/string/humanizeBytes.d.ts +1 -3
- package/string/humanizeNumber.d.ts +2 -4
- package/string/humanizeNumber.js +2 -2
- package/string/is.d.ts +1 -3
- package/string/isBetween.d.ts +4 -6
- package/string/isNotEmpty.d.ts +2 -4
- package/string/shorten.d.ts +0 -2
- package/string/shorten.js +1 -1
package/README.md
CHANGED
|
@@ -13,26 +13,27 @@ Zero-dependency collection of single-function utilities for common tasks
|
|
|
13
13
|
`npm install @valkyriestudios/utils`
|
|
14
14
|
|
|
15
15
|
## Available Functions
|
|
16
|
-
|
|
17
|
-
### array
|
|
18
|
-
- **isArray(val:unknown)**
|
|
16
|
+
### array/is(val:unknown)
|
|
19
17
|
Check if a variable is of type Array
|
|
20
18
|
```typescript
|
|
19
|
+
import is from '@valkyriestudios/utils/array/is';
|
|
21
20
|
isArray({a:1}); // FALSE
|
|
22
21
|
isArray([]); // TRUE
|
|
23
22
|
```
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
### array/isNotEmpty(val:unknown)
|
|
26
25
|
Check if a variable a non-empty array
|
|
27
26
|
```typescript
|
|
27
|
+
import isNotEmptyArray from '@valkyriestudios/utils/array/isNotEmpty';
|
|
28
28
|
isNotEmptyArray({a:1}); // FALSE
|
|
29
29
|
isNotEmptyArray([]); // FALSE
|
|
30
30
|
isNotEmptyArray([0, 1, 2]); // TRUE
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
### array/mapKey(val:Record[], key:string, opts:object={})
|
|
34
34
|
Map a non-primitive object array into an object map by key
|
|
35
35
|
```typescript
|
|
36
|
+
import mapKey from '@valkyriestudios/utils/array/mapKey';
|
|
36
37
|
mapKey([
|
|
37
38
|
{uid: 12, name: 'Peter'},
|
|
38
39
|
{uid: 15, name: 'Jonas'},
|
|
@@ -50,6 +51,7 @@ output:
|
|
|
50
51
|
|
|
51
52
|
Autofilters anything not meeting the spec:
|
|
52
53
|
```typescript
|
|
54
|
+
import mapKey from '@valkyriestudios/utils/array/mapKey';
|
|
53
55
|
mapKey([
|
|
54
56
|
0,
|
|
55
57
|
{uid: 12, name: 'Peter'},
|
|
@@ -74,6 +76,7 @@ output:
|
|
|
74
76
|
|
|
75
77
|
allows merging objects onto existing keys:
|
|
76
78
|
```typescript
|
|
79
|
+
import mapKey from '@valkyriestudios/utils/array/mapKey';
|
|
77
80
|
mapKey([
|
|
78
81
|
0,
|
|
79
82
|
{uid: 12, name: 'Peter'},
|
|
@@ -99,10 +102,11 @@ output:
|
|
|
99
102
|
}
|
|
100
103
|
```
|
|
101
104
|
|
|
102
|
-
|
|
105
|
+
### array/mapFn(val:Record[], key:Function, opts:object={})
|
|
103
106
|
Same behavior as mapKey but instead of a key, a function is passed to generate your own key. Eg:
|
|
104
107
|
|
|
105
108
|
```typescript
|
|
109
|
+
import mapFn from '@valkyriestudios/utils/array/mapFn';
|
|
106
110
|
mapFn([
|
|
107
111
|
{uid: 12, name: 'Peter'},
|
|
108
112
|
{uid: 15, name: 'Jonas'},
|
|
@@ -120,19 +124,21 @@ output:
|
|
|
120
124
|
|
|
121
125
|
options are the same as the mapKey function
|
|
122
126
|
|
|
123
|
-
|
|
127
|
+
### array/mapPrimitive(val:any[], opts:object={valtrim:false,keyround:false,valround:false})
|
|
124
128
|
Map an array of primitives (number/string)
|
|
125
129
|
```typescript
|
|
130
|
+
import mapPrimitive from '@valkyriestudios/utils/array/mapPrimitive';
|
|
126
131
|
mapPrimitive([1,2,3]); // {1: 1, 2: 2, 3: 3}
|
|
127
132
|
mapPrimitive(['hello', 'hello', 'foo', 'bar']); // {hello: 'hello', foo: 'foo', bar: 'bar'}
|
|
128
133
|
mapPrimitive(['hello', ' hello', 'foo', ' foo'], {valtrim: true}); // {hello: 'hello', foo: 'foo'}
|
|
129
134
|
```
|
|
130
135
|
|
|
131
|
-
|
|
136
|
+
### array/groupBy(val:Record[], handler:Function|string)
|
|
132
137
|
Return a grouped object from an array. This function **will automatically filter out any non/empty objects**.
|
|
133
138
|
|
|
134
139
|
Example usage when using a **function** as the handler
|
|
135
140
|
```typescript
|
|
141
|
+
import groupBy from '@valkyriestudios/utils/array/groupBy';
|
|
136
142
|
/* The output of the function will be what the key is on the map */
|
|
137
143
|
const group = groupBy([
|
|
138
144
|
{tally: 20, name: 'Peter'},
|
|
@@ -163,9 +169,9 @@ const group = groupBy([
|
|
|
163
169
|
```
|
|
164
170
|
**Take note**: If the function returns an undefined or empty string the object will be added to a fallback group called '_'
|
|
165
171
|
|
|
166
|
-
|
|
167
172
|
Example usage when using a **string** as the handler to denote a grouping by a certain property name
|
|
168
173
|
```typescript
|
|
174
|
+
import groupBy from '@valkyriestudios/utils/array/groupBy';
|
|
169
175
|
const group = groupBy([
|
|
170
176
|
{role: 'user', name: 'Peter'},
|
|
171
177
|
{role: 'user', name: 'Jake'},
|
|
@@ -184,9 +190,10 @@ const group = groupBy([
|
|
|
184
190
|
**Take note**: any object without the key will be added to a fallback group called '_'
|
|
185
191
|
|
|
186
192
|
|
|
187
|
-
|
|
193
|
+
### array/dedupe(val:Array, opts?:{filter_fn})
|
|
188
194
|
Remove all duplicates from an array, behind the scenes it uses the fnv 1A hash algorithm to performantly do comparisons.
|
|
189
195
|
```typescript
|
|
196
|
+
import dedupe from '@valkyriestudios/utils/array/dedupe';
|
|
190
197
|
dedupe(['a','a','b','c','c']); // ['a', 'b', 'c']
|
|
191
198
|
dedupe(['1',1,'2',2]); // ['1','2']
|
|
192
199
|
dedupe([new RegExp(/ab+c/, 'i'), new RegExp(/ab+c/, 'i')]); // [new RegExp(/ab+c/, 'i')]
|
|
@@ -197,10 +204,11 @@ dedupe(['hello', 'hello', 'world', false, 'world'], {filter_fn: el => isNotEmpty
|
|
|
197
204
|
|
|
198
205
|
Take Note: The filtering is applied while deduping, ensuring O(n) performance, as such this is faster than dedupe(arr.filter(...))
|
|
199
206
|
|
|
200
|
-
|
|
207
|
+
### array/join(val:Array, opts:object={delim:' ',trim:true,valtrim:true,innertrim:true,valround:false})
|
|
201
208
|
Concatenate the values within an array into a string, behind the scenes this will automatically filter out any value that is not a string or numerical value. For strings it will automatically trim (and remove if empty after trimming) before joining.
|
|
202
209
|
|
|
203
210
|
```typescript
|
|
211
|
+
import join from '@valkyriestudios/utils/array/join';
|
|
204
212
|
join(['Valkyrie', 'Studios']); // 'Valkyrie Studios'
|
|
205
213
|
join([5.1, ' years ', 'ago'], {valround: 0}); // '5 years ago'
|
|
206
214
|
join(['peter ', ' valkyrie '], {delim: '@'}); // 'peter@valkyrie'
|
|
@@ -209,19 +217,21 @@ join([' a', 1], {delim: '', valtrim: false, trim: false}); // ' a1'
|
|
|
209
217
|
join([' hello world ', 'this is peter '], {valtrim:true, innertrim: true, delim: ' '}); // 'hello world this is peter'
|
|
210
218
|
```
|
|
211
219
|
|
|
212
|
-
|
|
220
|
+
### array/shuffle(val:Array)
|
|
213
221
|
Shuffle an array (Fisher-Yates) in O(n), take note this changes the passed value
|
|
214
222
|
|
|
215
223
|
```typescript
|
|
224
|
+
import shuffle from '@valkyriestudios/utils/array/shuffle';
|
|
216
225
|
const arr = [1, 2, 3, 4, 5, 6];
|
|
217
226
|
shuffle(arr);
|
|
218
227
|
// [4, 6, 3, 2, 5, 1]
|
|
219
228
|
```
|
|
220
229
|
|
|
221
|
-
|
|
230
|
+
### array/sort(val:Array[object], by:string|Function, dir:Enum(asc,desc), options:Object)
|
|
222
231
|
Sort an array of objects, uses an implementation of [Tony Hoare's quicksort](https://cs.stanford.edu/people/eroberts/courses/soco/projects/2008-09/tony-hoare/quicksort.html)
|
|
223
232
|
|
|
224
233
|
```typescript
|
|
234
|
+
import sort from '@valkyriestudios/utils/array/sort';
|
|
225
235
|
const out = sort([
|
|
226
236
|
{test: 'Peter'},
|
|
227
237
|
{test: 'Jack'},
|
|
@@ -235,6 +245,7 @@ const out = sort([
|
|
|
235
245
|
```
|
|
236
246
|
|
|
237
247
|
```typescript
|
|
248
|
+
import sort from '@valkyriestudios/utils/array/sort';
|
|
238
249
|
const out = sort([
|
|
239
250
|
{test: 'Peter'},
|
|
240
251
|
{test: 'Jack'},
|
|
@@ -250,6 +261,7 @@ const out = sort([
|
|
|
250
261
|
allows passing a function to determine the key to sort by
|
|
251
262
|
|
|
252
263
|
```typescript
|
|
264
|
+
import sort from '@valkyriestudios/utils/array/sort';
|
|
253
265
|
const out = sort([
|
|
254
266
|
{test: 'Peter'},
|
|
255
267
|
{test: 'Jack'},
|
|
@@ -265,6 +277,7 @@ const out = sort([
|
|
|
265
277
|
auto-cleans input to only contains non-empty objects
|
|
266
278
|
|
|
267
279
|
```typescript
|
|
280
|
+
import sort from '@valkyriestudios/utils/array/sort';
|
|
268
281
|
const out = sort([
|
|
269
282
|
{test: 'Peter'},
|
|
270
283
|
{},
|
|
@@ -284,6 +297,7 @@ allows passing custom filter function to clean input
|
|
|
284
297
|
Take note: Sort will still verify that the object is not an empty object, even when passing a custom filter function.
|
|
285
298
|
|
|
286
299
|
```typescript
|
|
300
|
+
import sort from '@valkyriestudios/utils/array/sort';
|
|
287
301
|
const out = sort([
|
|
288
302
|
{test: 'Peter'},
|
|
289
303
|
{},
|
|
@@ -304,6 +318,7 @@ const out = sort([
|
|
|
304
318
|
allows passing custom options to position elements without a proper key (nokey_atend, defaults to true), or hide them (nokey_hide, defaults to false)
|
|
305
319
|
|
|
306
320
|
```typescript
|
|
321
|
+
import sort from '@valkyriestudios/utils/array/sort';
|
|
307
322
|
const arr = [{test: 'Peter'}, {test: undefined}, {test: 'Jack'}, {test: 'Pony'}, {uid: 100}, {test: 'JOHn'}];
|
|
308
323
|
const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_atend: false});
|
|
309
324
|
// [{test: undefined}, {uid: 100}, {test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Jack'}]
|
|
@@ -315,29 +330,31 @@ const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_hide: true});
|
|
|
315
330
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Jack'}]
|
|
316
331
|
```
|
|
317
332
|
|
|
318
|
-
|
|
333
|
+
### array/split(val:any[], size:number, opts?:{filter_fn})
|
|
319
334
|
Splits an array into subarray of provided size with optional filter
|
|
320
335
|
```typescript
|
|
336
|
+
import split from '@valkyriestudios/utils/array/split';
|
|
321
337
|
split([1,2,3,4,5], 2); // [[1,2],[3,4],[5]]
|
|
322
338
|
split([1, 2, false, 4, 5], 2, {filter_fn: isInteger}); // [[1,2],[4,5]]
|
|
323
339
|
```
|
|
324
340
|
|
|
325
341
|
Take Note: The filtering is applied while splitting, ensuring O(n) performance, as such this is faster than split(arr.filter(...), ...)
|
|
326
342
|
|
|
327
|
-
### boolean
|
|
328
|
-
- **isBoolean(val:any)**
|
|
343
|
+
### boolean/is(val:any)
|
|
329
344
|
Check if a variable is of type Boolean
|
|
330
345
|
```typescript
|
|
346
|
+
import isBoolean from '@valkyriestudios/utils/boolean/is';
|
|
331
347
|
isBoolean(null); // FALSE
|
|
332
348
|
isBoolean(false); // TRUE
|
|
333
349
|
isBoolean(true); // TRUE
|
|
334
350
|
```
|
|
335
351
|
|
|
336
|
-
### caching
|
|
337
|
-
- **memoize(fn:Function, resolver:Function=false, memoize_for:number|false)**
|
|
352
|
+
### caching/memoize(fn:Function, resolver:Function=false, memoize_for:number|false)
|
|
338
353
|
memoize the output of a function. An optional resolver function can be passed which allows custom cache key generation.
|
|
339
354
|
|
|
340
355
|
```typescript
|
|
356
|
+
import memoize from '@valkyriestudios/utils/caching/memoize';
|
|
357
|
+
|
|
341
358
|
const memoized_function = memoize((a) => {
|
|
342
359
|
return fnv1A(a);
|
|
343
360
|
});
|
|
@@ -345,6 +362,8 @@ const memoized_function = memoize((a) => {
|
|
|
345
362
|
|
|
346
363
|
Take Note: Also supports async functions and cache busting, eg:
|
|
347
364
|
```typescript
|
|
365
|
+
import memoize from '@valkyriestudios/utils/caching/memoize';
|
|
366
|
+
|
|
348
367
|
async function retrieveUser (userId:string) {
|
|
349
368
|
...
|
|
350
369
|
}
|
|
@@ -364,17 +383,26 @@ await memoized('123456'); /* Original function will not be called and memoized c
|
|
|
364
383
|
await memoized('123456'); /* Original function will be called and re-cached */
|
|
365
384
|
```
|
|
366
385
|
|
|
367
|
-
### date
|
|
368
|
-
|
|
369
|
-
Check if a variable is of type Date
|
|
386
|
+
### date/is(val:unknown)
|
|
387
|
+
Check if a variable is of type Date and valid
|
|
370
388
|
```typescript
|
|
389
|
+
import isDate from '@valkyriestudios/utils/date/is';
|
|
371
390
|
isDate(new Date('December 17, 1995 03:24:00')); // TRUE
|
|
372
391
|
isDate('December 17, 1995 03:24:00'); // FALSE
|
|
373
392
|
```
|
|
374
393
|
|
|
375
|
-
|
|
394
|
+
### date/isLeap(val:Date)
|
|
395
|
+
Check if a date is in a leap year or not
|
|
396
|
+
```typescript
|
|
397
|
+
import isLeap from '@valkyriestudios/utils/date/isLeap';
|
|
398
|
+
isLeap(new Date("2022-02-07T14:30:59.000Z")); // false
|
|
399
|
+
isLeap(new Date("2024-02-07T14:30:59.000Z")); // true
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### date/isFormat(val:unknown, spec:string)
|
|
376
403
|
Check if a variable is a string in a particular date format
|
|
377
404
|
```typescript
|
|
405
|
+
import isFormat from '@valkyriestudios/utils/date/isFormat';
|
|
378
406
|
isFormat('2024-02-07', 'YYYY-MM-DD'); // TRUE
|
|
379
407
|
isFormat('2024-2-07', 'YYYY-MM-DD'); // FALSE
|
|
380
408
|
isFormat('12:30 AM', 'HH:mm A'); // TRUE
|
|
@@ -403,11 +431,12 @@ Available tokens for usage in spec:
|
|
|
403
431
|
Note: The `ISO` token is a shorthand for `YYYY-MM-DDTHH:mm:ss.SSSZ`
|
|
404
432
|
Note: You can escape characters by surrounding them with `[...]` in your spec, eg: `YYYY-[Q]Q` would check for example `2024-Q1`
|
|
405
433
|
|
|
406
|
-
|
|
434
|
+
### date/diff(val_a:Date, val_b:Date, key:string)
|
|
407
435
|
Take two incoming dates and return the difference between them in a certain unit. Possible key options(week,day,hour,minute,second,millisecond).
|
|
408
436
|
|
|
409
437
|
Note: Does not touch the passed date objects, if no key is passed will default to millisecond
|
|
410
438
|
```typescript
|
|
439
|
+
import diff from '@valkyriestudios/utils/date/diff';
|
|
411
440
|
diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-11-05T13:12:11+06:00"), 'week'); // -4.404761904761905
|
|
412
441
|
diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:12:11+02:00"), 'day'); // 30.83333333333333332
|
|
413
442
|
diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:12:11+02:00"), 'hour'); // 740
|
|
@@ -417,16 +446,17 @@ diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-10-05T17:43:09.344+06
|
|
|
417
446
|
diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:25:43.898+02:00")); // 2663187102
|
|
418
447
|
```
|
|
419
448
|
|
|
420
|
-
|
|
449
|
+
### date/format(val:Date, spec:string, locale?:string, zone?:string, startOfWeek?:'mon'|'sun'|'sat'):string
|
|
421
450
|
Format a date according to a spec/locale and zone
|
|
422
451
|
|
|
423
|
-
Note
|
|
452
|
+
**Take Note**:
|
|
453
|
+
- The locale is by default set to `en-US`
|
|
454
|
+
- The zone is by default detected as the time zone of the client
|
|
455
|
+
- If we fail to detect the time zone we default to `UTC`
|
|
456
|
+
- The start of week is by default set to `mon`, the supported values are `mon`, `sun`, `sat`
|
|
457
|
+
- You can escape characters by surrounding them with `[...]` in your spec, eg: `YYYY-[Q]Q` would for example become `2024-Q1`
|
|
424
458
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
Note: You can escape characters by surrounding them with `[...]` in your spec, eg: `YYYY-[Q]Q` would for example become `2024-Q1`
|
|
428
|
-
|
|
429
|
-
Available tokens for usage in spec:
|
|
459
|
+
**Available Tokens**:
|
|
430
460
|
| Token | Description | Example |
|
|
431
461
|
|:---------|:--------------------------|:---------------|
|
|
432
462
|
| `YYYY` | Full Year | 2021 |
|
|
@@ -435,6 +465,8 @@ Available tokens for usage in spec:
|
|
|
435
465
|
| `MMM` | Month as 3 char | Jan Feb ... Nov Dec |
|
|
436
466
|
| `MM` | Month as 2 char | 01 02 .. 11 12 |
|
|
437
467
|
| `M` | Month as pure digit | 1 2 .. 11 12 |
|
|
468
|
+
| `WW` | Week Number as 2 char | 01 02 .. 52 53 |
|
|
469
|
+
| `W` | Week Number as pure digit | 1 2 .. 52 53 |
|
|
438
470
|
| `DD` | Day of month as 2 char | 01 02 .. 30 31 |
|
|
439
471
|
| `D` | Day of month as 1 char | 1 2 .. 30 31 |
|
|
440
472
|
| `dddd` | Day of week as 3 char | Sun Mon ... Fri Sat |
|
|
@@ -455,7 +487,22 @@ Available tokens for usage in spec:
|
|
|
455
487
|
| `t` | Locale-specific short time | 10:28 AM |
|
|
456
488
|
| `T` | Locale-specific time with seconds | 10:28:30 AM |
|
|
457
489
|
|
|
490
|
+
**Additional**:
|
|
491
|
+
Format has several additional functions defined which help usage inside of an ecosystem (eg: webapp) by **overriding the global defaults** used by format.
|
|
492
|
+
| Function | Description | Example |
|
|
493
|
+
|:---------|:--------------------------|:---------------|
|
|
494
|
+
| setZone | Sets the global timezone used by format | `format.setZone("Europe/Brussels")` |
|
|
495
|
+
| getZone | Returns the global timezone used by format ||
|
|
496
|
+
| setLocale | Sets the global locale used by format | `format.setLocale("nl-BE")` |
|
|
497
|
+
| getLocale | Returns the global locale used by format ||
|
|
498
|
+
| setStartOfWeek | Sets the global start of week used by format | `format.setStartOfWeek("sun")` |
|
|
499
|
+
| getStartOfWeek | Returns the global start of week used by format ||
|
|
500
|
+
|
|
501
|
+
**Usage**:
|
|
502
|
+
|
|
458
503
|
```typescript
|
|
504
|
+
import format from '@valkyriestudios/utils/date/format';
|
|
505
|
+
|
|
459
506
|
format(new Date('2023-01-10T14:30:00Z'), '[Today is] dddd, MMMM D, YYYY [at] h:mm A', 'en', 'Europe/Brussels');
|
|
460
507
|
// 'Today is Tuesday, January 10, 2023 at 2:30 PM'
|
|
461
508
|
|
|
@@ -467,25 +514,38 @@ format(new Date('2022-07-14T16:40:30Z'), 'YYYY-MM-DD', 'fr', 'Asia/Singapore');
|
|
|
467
514
|
|
|
468
515
|
format(new Date('2022-07-14T16:40:30Z'), 'YYYY-MM-DD', 'fr', 'Europe/Brussels');
|
|
469
516
|
// 2022-07-14
|
|
517
|
+
|
|
518
|
+
// ... (somewhere else in your code)
|
|
519
|
+
|
|
520
|
+
format.setLocale('fr');
|
|
521
|
+
format.setZone('Asia/Singapore');
|
|
522
|
+
|
|
523
|
+
// ... (somewhere else in your code)
|
|
524
|
+
|
|
525
|
+
format(new Date('2022-07-14T16:40:30Z'), 'dddd, [Year] Q Q M D [à] hh:mm A [string]');
|
|
526
|
+
// 'vendredi, Year 3 3 7 15 à 12:40 AM string'
|
|
527
|
+
format(new Date('2022-07-14T19:40:30Z'), 'dddd, YYYY-MM-DD');
|
|
528
|
+
// 'vendredi, 2022-07-15'
|
|
470
529
|
```
|
|
471
530
|
|
|
472
|
-
|
|
531
|
+
### date/toUTC(val:Date)
|
|
473
532
|
Takes the passed date object and returns a new date object set for utc
|
|
474
533
|
|
|
475
|
-
|
|
534
|
+
### date/toUnix(val:Date)
|
|
476
535
|
Takes the passed date object and returns its unix timestamp in seconds
|
|
477
536
|
|
|
478
|
-
|
|
537
|
+
### date/nowUnix()
|
|
479
538
|
Returns the current unix timestamp in seconds
|
|
480
539
|
|
|
481
|
-
|
|
540
|
+
### date/nowUnixMs()
|
|
482
541
|
Returns the current unix timestamp in milliseconds
|
|
483
542
|
|
|
484
|
-
|
|
543
|
+
### date/setTimeUTC(val:Date, props:{hour?:number;minute?:number;second?:number;millisecond?:number})
|
|
485
544
|
Take the incoming date and return a date where the time portion is set to the values in the provided props
|
|
486
545
|
|
|
487
546
|
Note: Does not touch the date object passed
|
|
488
547
|
```typescript
|
|
548
|
+
import setTimeUTC from '@valkyriestudios/utils/date/setTimeUTC';
|
|
489
549
|
setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {hour: 5}); // new Date("2023-05-04T05:04:27.432Z")
|
|
490
550
|
setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {hour: 5, minute: 30}); // new Date("2023-05-04T05:30:27.432Z")
|
|
491
551
|
setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {hour: 5, minute: 30, second: 0}); // new Date("2023-05-04T05:30:00.432Z")
|
|
@@ -494,11 +554,12 @@ setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {minute: 30, second: 0, millise
|
|
|
494
554
|
setTimeUTC(new Date("2023-05-04T12:04:27.432Z"), {second: 9, millisecond: 0}); // new Date("2023-05-04T12:04:09.000Z")
|
|
495
555
|
```
|
|
496
556
|
|
|
497
|
-
|
|
557
|
+
### date/startOfUTC(val:Date, key:string)
|
|
498
558
|
Take the incoming date and return a date set to the start of passed key. Possible key options(year,quarter,month,week,week_sun,week_mon,week_tue,week_wed,week_thu,week_fri,week_sat,day,hour,minute,second).
|
|
499
559
|
|
|
500
560
|
Note: Does not touch the date object passed
|
|
501
561
|
```typescript
|
|
562
|
+
import startOfUTC from '@valkyriestudios/utils/date/startOfUTC';
|
|
502
563
|
startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'year'); // new Date("2023-01-01T00:00:00.000Z")
|
|
503
564
|
startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'quarter'); // new Date("2023-04-01T00:00:00.000Z")
|
|
504
565
|
startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'month'); // new Date("2023-05-01T00:00:00.000Z")
|
|
@@ -514,11 +575,12 @@ startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'minute'); // new Date("2023-0
|
|
|
514
575
|
startOfUTC(new Date("2023-05-04T12:04:27.043+02:00"), 'second'); // new Date("2023-05-04T10:04:27.000Z")
|
|
515
576
|
```
|
|
516
577
|
|
|
517
|
-
|
|
578
|
+
### date/endOfUTC(val:Date, key:string)
|
|
518
579
|
Take the incoming date and return a date set to the end of passed key. Possible key options(year,quarter,month,week,week_sun,week_mon,week_tue,week_wed,week_thu,week_fri,week_sat,day,hour,minute,second).
|
|
519
580
|
|
|
520
581
|
Note: Does not touch the date object passed
|
|
521
582
|
```typescript
|
|
583
|
+
import endOfUTC from '@valkyriestudios/utils/date/endOfUTC';
|
|
522
584
|
endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'year'); // new Date("2023-12-31T23:59:59.999Z")
|
|
523
585
|
endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'quarter'); // new Date("2023-06-30T23:59:59.999Z")
|
|
524
586
|
endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'month'); // new Date("2023-05-31T23:59:59.999Z")
|
|
@@ -538,11 +600,12 @@ endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'minute'); // new Date("2023-05-
|
|
|
538
600
|
endOfUTC(new Date("2023-05-04T12:04:27.043+02:00"), 'second'); // new Date("2023-05-04T10:04:27.999Z")
|
|
539
601
|
```
|
|
540
602
|
|
|
541
|
-
|
|
603
|
+
### date/addUTC(val:Date, amount:integer, key:string)
|
|
542
604
|
Take the incoming date and add a certain amount of the passed key. Possible key options(year,years,month,months,day,days,hour,hours,minute,minutes,second,seconds,millisecond,milliseconds).
|
|
543
605
|
|
|
544
606
|
Note: Does not touch the date object passed
|
|
545
607
|
```typescript
|
|
608
|
+
import addUTC from '@valkyriestudios/utils/date/addUTC';
|
|
546
609
|
addUTC(new Date("2022-10-05T13:12:11+02:00"), 10, 'year'); // new Date("2032-10-05T11:12:11.000Z")
|
|
547
610
|
addUTC(new Date("2022-10-05T13:12:11+02:00"), -10, 'year'); // new Date("2012-10-05T11:12:11.000Z")
|
|
548
611
|
addUTC(new Date("2022-10-05T13:12:11+02:00"), 10, 'month'); // new Date("2023-08-05T11:12:11.000Z")
|
|
@@ -561,35 +624,36 @@ addUTC(new Date("2022-10-05T13:12:11+02:00"), 336000 * 60, 'second'); // new Dat
|
|
|
561
624
|
addUTC(new Date("2022-10-05T13:12:11+02:00"), -10, 'second'); // new Date("2022-10-05T11:12:01.000Z")
|
|
562
625
|
```
|
|
563
626
|
|
|
564
|
-
### deep
|
|
565
|
-
- **deepFreeze(val:Object)**
|
|
627
|
+
### deep/freeze(val:Object)
|
|
566
628
|
Recursively freezes all properties of an object
|
|
567
629
|
```typescript
|
|
630
|
+
import deepFreeze from '@valkyriestudios/utils/deep/freeze';
|
|
568
631
|
const myObj = deepFreeze({
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
632
|
+
a: 2,
|
|
633
|
+
b: {
|
|
634
|
+
c: 3,
|
|
635
|
+
d: {
|
|
636
|
+
e: 'hello',
|
|
637
|
+
}
|
|
638
|
+
}
|
|
576
639
|
});
|
|
577
640
|
Object.isFrozen(myObj); // TRUE
|
|
578
641
|
Object.isFrozen(myObj.b); // TRUE
|
|
579
642
|
Object.isFrozen(myObj.b.d); // TRUE
|
|
580
643
|
```
|
|
581
644
|
|
|
582
|
-
|
|
645
|
+
### deep/seal(val:Object)
|
|
583
646
|
Recursively freezes all properties of an object
|
|
584
647
|
```typescript
|
|
648
|
+
import deepSeal from '@valkyriestudios/utils/deep/seal';
|
|
585
649
|
const myObj = deepSeal({
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
650
|
+
a: 2,
|
|
651
|
+
b: {
|
|
652
|
+
c: 3,
|
|
653
|
+
d: {
|
|
654
|
+
e: 'hello',
|
|
655
|
+
}
|
|
656
|
+
}
|
|
593
657
|
});
|
|
594
658
|
Object.isSealed(myObj); // TRUE
|
|
595
659
|
Object.isSealed(myObj.b); // TRUE
|
|
@@ -597,66 +661,62 @@ Object.isSealed(myObj.b.d); // TRUE
|
|
|
597
661
|
Object.isFrozen(myObj.b.d); // FALSE
|
|
598
662
|
```
|
|
599
663
|
|
|
600
|
-
|
|
664
|
+
### deep/set(obj:Object, path:string, value:any=null, define:boolean=false)
|
|
601
665
|
Sets a property and its value deep in the structure of an object
|
|
602
666
|
```typescript
|
|
667
|
+
import deepSet from '@valkyriestudios/utils/deep/set';
|
|
603
668
|
const myObj = {
|
|
604
|
-
|
|
669
|
+
a: 2,
|
|
605
670
|
};
|
|
606
671
|
deepSet(myObj, 'b.c.d.e', 4);
|
|
607
672
|
myObj.b.c.d.e; // 4
|
|
608
|
-
```
|
|
609
673
|
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
],
|
|
674
|
+
const myObj2 = {
|
|
675
|
+
a: 2,
|
|
676
|
+
b: [
|
|
677
|
+
{ price : 2 },
|
|
678
|
+
{ price : 4 },
|
|
679
|
+
],
|
|
617
680
|
};
|
|
618
|
-
deepSet(
|
|
619
|
-
deepSet(
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
```
|
|
681
|
+
deepSet(myObj2, 'b[0].price', 100);
|
|
682
|
+
deepSet(myObj2, 'b[1].price', 500);
|
|
683
|
+
myObj2.b[0].price; // 100
|
|
684
|
+
myObj2.b[1].price; // 500
|
|
623
685
|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
a: 2,
|
|
686
|
+
const myObj3 = {
|
|
687
|
+
a: 2,
|
|
627
688
|
};
|
|
628
|
-
deepSet(
|
|
629
|
-
|
|
689
|
+
deepSet(myObj3, 'b.c', { value: function () => {...} }, true);
|
|
690
|
+
myObj3.b.c; // Function
|
|
630
691
|
```
|
|
631
692
|
|
|
632
|
-
|
|
693
|
+
### deep/get(obj:Object, path:string, get_parent:boolean=false)
|
|
633
694
|
Retrieves a value based on a path in a deeply nested object
|
|
634
695
|
```typescript
|
|
696
|
+
import deepGet from '@valkyriestudios/utils/deep/get';
|
|
635
697
|
const myObj = {
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
698
|
+
a: 2,
|
|
699
|
+
b: [
|
|
700
|
+
{ price : 2 },
|
|
701
|
+
{ price : 4 },
|
|
702
|
+
],
|
|
641
703
|
};
|
|
642
704
|
deepGet(myObj, 'b[0].price', true); // [{price: 2}, {price: 4}]
|
|
643
|
-
```
|
|
644
705
|
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
],
|
|
706
|
+
const myObj2 = {
|
|
707
|
+
a: 2,
|
|
708
|
+
b: [
|
|
709
|
+
{ price : 2 },
|
|
710
|
+
{ price : 4 },
|
|
711
|
+
],
|
|
652
712
|
};
|
|
653
|
-
deepGet(
|
|
713
|
+
deepGet(myObj2, 'b[0].price'); // 2
|
|
654
714
|
```
|
|
655
715
|
|
|
656
|
-
### equal
|
|
657
|
-
- **equal(a:any, b:any)**
|
|
716
|
+
### equal(a:any, b:any)
|
|
658
717
|
Check if a variable is equal to another one
|
|
659
718
|
```typescript
|
|
719
|
+
import equal from '@valkyriestudios/utils/equal';
|
|
660
720
|
equal(5, 6); // FALSE
|
|
661
721
|
equal(1, 1); // TRUE
|
|
662
722
|
equal([0, 1, 2], [1, 2]); // FALSE
|
|
@@ -666,47 +726,131 @@ equal(new Date('2012-20-09'), '2012-20-09'); // TRUE ( check is being done on un
|
|
|
666
726
|
equal(new RegExp(/ab+c/, 'i'), /ab+c/i); // TRUE
|
|
667
727
|
```
|
|
668
728
|
|
|
669
|
-
### function
|
|
670
|
-
- **debounce(val:Fn, wait:number)**
|
|
729
|
+
### function/debounce(val:Fn, wait:number)
|
|
671
730
|
Wrap a function in a debounce proxy that waits for X uninterrupted milliseconds before running callback function
|
|
731
|
+
```typescript
|
|
732
|
+
const log = (message: string) => console.log(message);
|
|
733
|
+
const debouncedLog = debounce(log, 2000);
|
|
734
|
+
debouncedLog("Hello, World!"); // 2 seconds later we see log
|
|
735
|
+
|
|
736
|
+
debouncedLog("Hello, World!");
|
|
737
|
+
debouncedLog.cancel(); // No log as we cancelled
|
|
738
|
+
|
|
739
|
+
debouncedLog("Hello, World!");
|
|
740
|
+
debouncedLog.flush(); // Immediate log no 2 second debounce
|
|
741
|
+
|
|
742
|
+
debouncedLog("Hello, World!");
|
|
743
|
+
debouncedLog.cancel();
|
|
744
|
+
debouncedLog.flush(); // Nothing happens as timeout was killed through cancel
|
|
745
|
+
````
|
|
672
746
|
|
|
673
|
-
|
|
747
|
+
### function/is(val:unknown)
|
|
674
748
|
Check if a variable is a Function
|
|
749
|
+
```typescript
|
|
750
|
+
import isFunction from '@valkyriestudios/utils/function/is';
|
|
751
|
+
isFunction(() => console.log('Hello')); // TRUE
|
|
752
|
+
isFunction('December 17, 1995 03:24:00'); // FALSE
|
|
753
|
+
```
|
|
675
754
|
|
|
676
|
-
|
|
755
|
+
### function/isAsync(val:unknown):boolean
|
|
677
756
|
Check if a variable is an async function
|
|
757
|
+
```typescript
|
|
758
|
+
import isAsync from '@valkyriestudios/utils/function/isAsync';
|
|
759
|
+
isAsync(() => console.log('Hello')); // FALSE
|
|
760
|
+
isAsync(async () => {
|
|
761
|
+
await sleep(1000);
|
|
762
|
+
console.log('Hello');
|
|
763
|
+
}); // TRUE
|
|
764
|
+
```
|
|
678
765
|
|
|
679
|
-
|
|
766
|
+
### function/noop()
|
|
680
767
|
An empty function that can be used in (for example) piping
|
|
681
768
|
|
|
682
|
-
|
|
769
|
+
### function/noopreturn(val:any)
|
|
683
770
|
An empty function that will pass back the variable that it was passed
|
|
684
771
|
|
|
685
|
-
|
|
772
|
+
### function/noopresolve(val:any)
|
|
686
773
|
An empty function that returns a promise that will immediately resolve itself and pass back any variable that was passed to it
|
|
687
774
|
|
|
688
|
-
|
|
775
|
+
### function/sleep(val:int)
|
|
689
776
|
An empty function that returns a promise that will resolve after X milliseconds, default is set to 1000ms.
|
|
690
|
-
|
|
777
|
+
```typescript
|
|
778
|
+
import sleep from '@valkyriestudios/utils/function/sleep';
|
|
779
|
+
await sleep(1000); // sleeps for 1 second
|
|
780
|
+
````
|
|
691
781
|
|
|
692
|
-
### formdata
|
|
693
|
-
- **isFormData(val:any)**
|
|
782
|
+
### formdata/is(val:any)
|
|
694
783
|
Check if a variable is of type FormData
|
|
695
784
|
```typescript
|
|
785
|
+
import isFormData from '@valkyriestudios/utils/formdata/is';
|
|
696
786
|
isFormData(new FormData()); // TRUE
|
|
697
787
|
isFormData({hi: 'there'}); // FALSE
|
|
698
788
|
```
|
|
699
789
|
|
|
700
|
-
###
|
|
701
|
-
|
|
790
|
+
### formdata/toObject(val:FormData, {raw?:string[]} = {})
|
|
791
|
+
Converts an instance of FormData to an object
|
|
792
|
+
```typescript
|
|
793
|
+
import toObject from '@valkyriestudios/utils/formdata/toObject';
|
|
794
|
+
const form = new FormData();
|
|
795
|
+
form.append('name', 'Alice');
|
|
796
|
+
form.append('hobbies', 'reading');
|
|
797
|
+
form.append('hobbies', 'writing');
|
|
798
|
+
form.append('emptyField', '');
|
|
799
|
+
|
|
800
|
+
toObject(form); // {name: 'Alice', hobbies: ['reading', 'writing'], emptyField: ''}
|
|
801
|
+
```
|
|
802
|
+
|
|
803
|
+
Automatically converts strings to numbers and booleans, and nests objects/arrays based on key structures:
|
|
804
|
+
```typescript
|
|
805
|
+
const form = new FormData();
|
|
806
|
+
form.append('user[0].name', 'Alice');
|
|
807
|
+
form.append('user[1].age', '25');
|
|
808
|
+
form.append('enabled', 'false');
|
|
809
|
+
form.append('config.isGood', 'true');
|
|
810
|
+
form.append('config.amount', ' 50 ');
|
|
811
|
+
|
|
812
|
+
toObject(form); /* {
|
|
813
|
+
user: [
|
|
814
|
+
{name: 'Alice'},
|
|
815
|
+
{age: 25},
|
|
816
|
+
],
|
|
817
|
+
enabled: false,
|
|
818
|
+
config: {
|
|
819
|
+
isGood: true,
|
|
820
|
+
amount: 50,
|
|
821
|
+
},
|
|
822
|
+
} */
|
|
823
|
+
```
|
|
824
|
+
|
|
825
|
+
Allows blacklisting keys that should not be normalized into numbers/booleans but should remain as they are:
|
|
826
|
+
```typescript
|
|
827
|
+
const form = new FormData();
|
|
828
|
+
form.append('pincode', '0123');
|
|
829
|
+
form.append('enabled', 'false');
|
|
830
|
+
form.append('config.isGood', 'true');
|
|
831
|
+
form.append('config.amount', ' 50 ');
|
|
832
|
+
|
|
833
|
+
toObject(form, {raw: ['pincode']}); /* {
|
|
834
|
+
pincode: '0123',
|
|
835
|
+
enabled: false,
|
|
836
|
+
config: {
|
|
837
|
+
isGood: true,
|
|
838
|
+
amount: 50,
|
|
839
|
+
},
|
|
840
|
+
} */
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
### hash/guid()
|
|
702
844
|
Generate a unique identifier (guid) according to RFC4122
|
|
703
845
|
```typescript
|
|
846
|
+
import guid from '@valkyriestudios/utils/hash/guid';
|
|
704
847
|
guid(); // 245caf1a-86af-11e7-bb31-be2e44b06b34
|
|
705
848
|
```
|
|
706
849
|
|
|
707
|
-
|
|
850
|
+
### hash/fnv1A(val:unknown)
|
|
708
851
|
Generate a fnv1A hash from an object, using a 32-bit prime/offset
|
|
709
852
|
```typescript
|
|
853
|
+
import fnv1A from '@valkyriestudios/utils/hash/fnv1A';
|
|
710
854
|
fnv1A('hello world'); // -2023343616
|
|
711
855
|
fnv1A({a:1,b:2}); // 361168128
|
|
712
856
|
fnv1A(4); // 1630425728
|
|
@@ -714,54 +858,114 @@ fnv1A(new RegExp(/ab+c/, 'i')); // 2131692544
|
|
|
714
858
|
fnv1A(new Date('2012-02-02')); // 1655579136
|
|
715
859
|
```
|
|
716
860
|
|
|
717
|
-
###
|
|
718
|
-
|
|
861
|
+
### Is
|
|
862
|
+
The utility found at `@valkyriestudios/utils/is` combines and exposes a barrel export of several other functions found within the library. This does not extend those utils but simply acts as an easy single import for many utils all at once.
|
|
863
|
+
|
|
864
|
+
These functions are the following:
|
|
865
|
+
- **Is.Array**
|
|
866
|
+
- **Is.NeArray**
|
|
867
|
+
- **Is.NotEmptyArray**
|
|
868
|
+
- **Is.Boolean**
|
|
869
|
+
- **Is.Date**
|
|
870
|
+
- **Is.Formdata**
|
|
871
|
+
- **Is.Function**
|
|
872
|
+
- **Is.AsyncFunction**
|
|
873
|
+
- **Is.Num**
|
|
874
|
+
- **Is.NumBetween**
|
|
875
|
+
- **Is.NumAbove**
|
|
876
|
+
- **Is.NumAboveOrEqual**
|
|
877
|
+
- **Is.NumBelow**
|
|
878
|
+
- **Is.NumBelowOrEqual**
|
|
879
|
+
- **Is.NumGt**
|
|
880
|
+
- **Is.NumGte**
|
|
881
|
+
- **Is.NumLt**
|
|
882
|
+
- **Is.NumLte**
|
|
883
|
+
- **Is.Number**
|
|
884
|
+
- **Is.NumberBetween**
|
|
885
|
+
- **Is.NumberAbove**
|
|
886
|
+
- **Is.NumberAboveOrEqual**
|
|
887
|
+
- **Is.NumberBelow**
|
|
888
|
+
- **Is.NumberBelowOrEqual**
|
|
889
|
+
- **Is.Int**
|
|
890
|
+
- **Is.IntBetween**
|
|
891
|
+
- **Is.IntAbove**
|
|
892
|
+
- **Is.IntAboveOrEqual**
|
|
893
|
+
- **Is.IntBelow**
|
|
894
|
+
- **Is.IntBelowOrEqual**
|
|
895
|
+
- **Is.IntGt**
|
|
896
|
+
- **Is.IntGte**
|
|
897
|
+
- **Is.IntLt**
|
|
898
|
+
- **Is.IntLte**
|
|
899
|
+
- **Is.Integer**
|
|
900
|
+
- **Is.IntegerBetween**
|
|
901
|
+
- **Is.IntegerBelow**
|
|
902
|
+
- **Is.IntegerBelowOrEqual**
|
|
903
|
+
- **Is.IntegerAbove**
|
|
904
|
+
- **Is.IntegerAboveOrEqual**
|
|
905
|
+
- **Is.RegExp**
|
|
906
|
+
- **Is.Object**
|
|
907
|
+
- **Is.NeObject**
|
|
908
|
+
- **Is.NotEmptyObject**
|
|
909
|
+
- **Is.String**
|
|
910
|
+
- **Is.StringBetween**
|
|
911
|
+
- **Is.NeString**
|
|
912
|
+
- **Is.NotEmptyString**
|
|
913
|
+
- **Is.Equal**
|
|
914
|
+
- **Is.Eq**
|
|
915
|
+
|
|
916
|
+
### number/is(val:unknown)
|
|
719
917
|
Check if a variable is a number
|
|
720
918
|
```typescript
|
|
919
|
+
import isNumber from '@valkyriestudios/utils/number/is';
|
|
721
920
|
isNumber('foo'); // FALSE
|
|
722
921
|
isNumber(4); // TRUE
|
|
723
922
|
isNumber(0.5); // TRUE
|
|
724
923
|
```
|
|
725
924
|
|
|
726
|
-
|
|
925
|
+
### number/isAbove(val:number, comp:number)
|
|
727
926
|
Check if a variable is a number above a certain bound
|
|
728
927
|
```typescript
|
|
928
|
+
import isNumberAbove from '@valkyriestudios/utils/number/isAbove';
|
|
729
929
|
isNumberAbove(5, 0); // TRUE
|
|
730
930
|
isNumberAbove(.1, 0); // TRUE
|
|
731
931
|
isNumberAbove(-1, -1); // FALSE
|
|
732
932
|
isNumberAbove(-10, -9); // FALSE
|
|
733
933
|
```
|
|
734
934
|
|
|
735
|
-
|
|
935
|
+
### number/isAboveOrEqual(val:number, comp:number)
|
|
736
936
|
Check if a variable is a number above or equal to a certain bound
|
|
737
937
|
```typescript
|
|
938
|
+
import isNumberAboveOrEqual from '@valkyriestudios/utils/number/isAboveOrEqual';
|
|
738
939
|
isNumberAboveOrEqual(5, 0); // TRUE
|
|
739
940
|
isNumberAboveOrEqual(.1, 0); // TRUE
|
|
740
941
|
isNumberAboveOrEqual(-1, -1); // TRUE
|
|
741
942
|
isNumberAboveOrEqual(-10, -9); // FALSE
|
|
742
943
|
```
|
|
743
944
|
|
|
744
|
-
|
|
945
|
+
### number/isBelow(val:number, comp:number)
|
|
745
946
|
Check if a variable is a number below a certain bound
|
|
746
947
|
```typescript
|
|
948
|
+
import isNumberBelow from '@valkyriestudios/utils/number/isBelow';
|
|
747
949
|
isNumberBelow(0, 5); // TRUE
|
|
748
950
|
isNumberBelow(0, .1); // TRUE
|
|
749
951
|
isNumberBelow(-1, -1); // FALSE
|
|
750
952
|
isNumberBelow(-9, -10); // FALSE
|
|
751
953
|
```
|
|
752
954
|
|
|
753
|
-
|
|
955
|
+
### number/isBelowOrEqual(val:number, comp:number)
|
|
754
956
|
Check if a variable is a number below or equal a certain bound
|
|
755
957
|
```typescript
|
|
958
|
+
import isNumberBelowOrEqual from '@valkyriestudios/utils/number/isBelowOrEqual';
|
|
756
959
|
isNumberBelowOrEqual(0, 5); // TRUE
|
|
757
960
|
isNumberBelowOrEqual(0, .1); // TRUE
|
|
758
961
|
isNumberBelowOrEqual(-1, -1); // TRUE
|
|
759
962
|
isNumberBelowOrEqual(-9, -10); // FALSE
|
|
760
963
|
```
|
|
761
964
|
|
|
762
|
-
|
|
965
|
+
### number/isBetween(val:number, min:number, max:number)
|
|
763
966
|
Check if a variable is a number between a range of numbers
|
|
764
967
|
```typescript
|
|
968
|
+
import isNumberBetween from '@valkyriestudios/utils/number/isBetween';
|
|
765
969
|
isNumberBetween(5, 0, 10); // TRUE
|
|
766
970
|
isNumberBetween(.1, 0, 1); // TRUE
|
|
767
971
|
isNumberBetween(-.1, -1, 0); // TRUE
|
|
@@ -769,35 +973,39 @@ isNumberBetween(0, 0, 1); // TRUE
|
|
|
769
973
|
isNumberBetween(-1, 0, 1); // FALSE
|
|
770
974
|
```
|
|
771
975
|
|
|
772
|
-
|
|
976
|
+
### number/isInteger(val:unknown)
|
|
773
977
|
Check if a variable is an integer
|
|
774
978
|
```typescript
|
|
979
|
+
import isInteger from '@valkyriestudios/utils/number/isInteger';
|
|
775
980
|
isInteger('foo'); // FALSE
|
|
776
981
|
isInteger(4); // TRUE
|
|
777
982
|
isInteger(0.5); // FALSE
|
|
778
983
|
```
|
|
779
984
|
|
|
780
|
-
|
|
985
|
+
### number/isIntegerAbove(val:number, comp:number)
|
|
781
986
|
Check if a variable is an integer above a certain bound
|
|
782
987
|
```typescript
|
|
988
|
+
import isIntegerAbove from '@valkyriestudios/utils/number/isIntegerAbove';
|
|
783
989
|
isIntegerAbove(5, 0); // TRUE
|
|
784
990
|
isIntegerAbove(.1, 0); // FALSE
|
|
785
991
|
isIntegerAbove(-1, -1); // FALSE
|
|
786
992
|
isIntegerAbove(-10, -9); // FALSE
|
|
787
993
|
```
|
|
788
994
|
|
|
789
|
-
|
|
995
|
+
### number/isIntegerAboveOrEqual(val:number, comp:number)
|
|
790
996
|
Check if a variable is an integer above or equal to a certain bound
|
|
791
997
|
```typescript
|
|
998
|
+
import isIntegerAboveOrEqual from '@valkyriestudios/utils/number/isIntegerAboveOrEqual';
|
|
792
999
|
isIntegerAboveOrEqual(5, 0); // TRUE
|
|
793
1000
|
isIntegerAboveOrEqual(.1, 0); // FALSE
|
|
794
1001
|
isIntegerAboveOrEqual(-1, -1); // TRUE
|
|
795
1002
|
isIntegerAboveOrEqual(-10, -9); // FALSE
|
|
796
1003
|
```
|
|
797
1004
|
|
|
798
|
-
|
|
1005
|
+
### number/isIntegerBelow(val:number, comp:number)
|
|
799
1006
|
Check if a variable is an integer below a certain bound
|
|
800
1007
|
```typescript
|
|
1008
|
+
import isIntegerBelow from '@valkyriestudios/utils/number/isIntegerBelow';
|
|
801
1009
|
isIntegerBelow(0, 5); // TRUE
|
|
802
1010
|
isIntegerBelow(0, .1); // TRUE
|
|
803
1011
|
isIntegerBelow(.4, 5); // FALSE
|
|
@@ -805,9 +1013,10 @@ isIntegerBelow(-1, -1); // FALSE
|
|
|
805
1013
|
isIntegerBelow(-9, -10); // FALSE
|
|
806
1014
|
```
|
|
807
1015
|
|
|
808
|
-
|
|
1016
|
+
### number/isIntegerBelowOrEqual(val:number, comp:number)
|
|
809
1017
|
Check if a variable is an integer below or equal to a certain bound
|
|
810
1018
|
```typescript
|
|
1019
|
+
import isIntegerBelowOrEqual from '@valkyriestudios/utils/number/isIntegerBelowOrEqual';
|
|
811
1020
|
isIntegerBelowOrEqual(0, 5); // TRUE
|
|
812
1021
|
isIntegerBelowOrEqual(0, .1); // TRUE
|
|
813
1022
|
isIntegerBelowOrEqual(.4, 5); // FALSE
|
|
@@ -815,9 +1024,10 @@ isIntegerBelowOrEqual(-1, -1); // TRUE
|
|
|
815
1024
|
isIntegerBelowOrEqual(-9, -10); // FALSE
|
|
816
1025
|
```
|
|
817
1026
|
|
|
818
|
-
|
|
1027
|
+
### number/isIntegerBetween(val:number, min:number, max:number)
|
|
819
1028
|
Check if a variable is an integer between a range of numbers
|
|
820
1029
|
```typescript
|
|
1030
|
+
import isIntegerBetween from '@valkyriestudios/utils/number/isIntegerBetween';
|
|
821
1031
|
isIntegerBetween(5, 0, 10); // TRUE
|
|
822
1032
|
isIntegerBetween(.1, 0, 1); // FALSE
|
|
823
1033
|
isIntegerBetween(-.1, -1, 0); // FALSE
|
|
@@ -825,69 +1035,77 @@ isIntegerBetween(0, 0, 1); // TRUE
|
|
|
825
1035
|
isIntegerBetween(-1, 0, 1); // FALSE
|
|
826
1036
|
```
|
|
827
1037
|
|
|
828
|
-
|
|
1038
|
+
### number/isNumericalNaN(val:unknown)
|
|
829
1039
|
Check if a variable is a numerical nan ( a number that is a NaN, this distinguishment is made since both a string or a number can be NaN)
|
|
830
1040
|
```typescript
|
|
1041
|
+
import isNumericalNaN from '@valkyriestudios/utils/number/isNumericalNaN';
|
|
831
1042
|
isNumericalNaN('foo'); // FALSE
|
|
832
1043
|
isNumericalNaN(NaN); // TRUE
|
|
833
1044
|
```
|
|
834
1045
|
|
|
835
|
-
|
|
1046
|
+
### number/toPercentage(val:Number,precision:Number=0,min:Number=0,max:Number=1)
|
|
836
1047
|
Calculate the percentage of a specific value in a range
|
|
837
1048
|
```typescript
|
|
1049
|
+
import toPercentage from '@valkyriestudios/utils/number/toPercentage';
|
|
838
1050
|
toPercentage(0.50106579, 5); // 50.11658
|
|
839
1051
|
toPercentage(-356, 0, -1000, 1000); // 32
|
|
840
1052
|
toPercentage(0.5); // 50
|
|
841
1053
|
```
|
|
842
1054
|
|
|
843
|
-
|
|
1055
|
+
### number/round(val:Number,precision:Number=0)
|
|
844
1056
|
Round a numeric value to a specific amount of decimals
|
|
845
1057
|
```typescript
|
|
1058
|
+
import round from '@valkyriestudios/utils/number/round';
|
|
846
1059
|
round(5.123456789, 0); // 5
|
|
847
1060
|
round(5.123456789, 2); // 5.12
|
|
848
1061
|
round(5.123456789, 5); // 5.12346
|
|
849
1062
|
```
|
|
850
1063
|
|
|
851
|
-
|
|
1064
|
+
### number/randomBetween(min:Number=0,max:Number=10)
|
|
852
1065
|
Generate a random numeric value between a min and max range
|
|
853
1066
|
```typescript
|
|
1067
|
+
import randomBetween from '@valkyriestudios/utils/number/randomBetween';
|
|
854
1068
|
randomBetween(); // Will generate a random between 0 and 10
|
|
855
1069
|
randomBetween(25, 100); // Will generate a random between 25 and 100
|
|
856
1070
|
```
|
|
857
1071
|
|
|
858
|
-
|
|
1072
|
+
### number/randomIntBetween(min:Number=0,max:Number=10)
|
|
859
1073
|
Generate a random numeric value between a min and max range (max not inclusive)
|
|
860
1074
|
```typescript
|
|
1075
|
+
import randomIntBetween from '@valkyriestudios/utils/number/randomIntBetween';
|
|
861
1076
|
randomIntBetween(); // Will generate a random between 0 and 10 (10 not inclusive)
|
|
862
1077
|
randomIntBetween(25, 100); // Will generate a random between 25 and 100 (100 not inclusive)
|
|
863
1078
|
```
|
|
864
1079
|
|
|
865
|
-
### object
|
|
866
|
-
- **isObject(val:unknown)**
|
|
1080
|
+
### object/is(val:unknown)
|
|
867
1081
|
Check if a variable is of type Object
|
|
868
1082
|
```typescript
|
|
1083
|
+
import isObject from '@valkyriestudios/utils/object/is';
|
|
869
1084
|
isObject({a: 1}); // TRUE
|
|
870
1085
|
isObject(1); // FALSE
|
|
871
1086
|
```
|
|
872
1087
|
|
|
873
|
-
|
|
1088
|
+
### object/isNotEmpty(val:unknown)
|
|
874
1089
|
Check if a variable a non-empty object
|
|
875
1090
|
```typescript
|
|
1091
|
+
import isNotEmptyObject from '@valkyriestudios/utils/object/isNotEmpty';
|
|
876
1092
|
isNotEmptyObject({a:1}); // TRUE
|
|
877
1093
|
isNotEmptyObject({}); // FALSE
|
|
878
1094
|
isNotEmptyObject('Hi'); // FALSE
|
|
879
1095
|
```
|
|
880
1096
|
|
|
881
|
-
|
|
1097
|
+
### object/pick(obj:Object={}, keys:Array[string]=[])
|
|
882
1098
|
Copies the keys passed in the 'keys' array from the passed object to a new object and returns that object.**
|
|
883
1099
|
<small>If a key wasn't found it will be set as undefined</small>
|
|
884
1100
|
```typescript
|
|
1101
|
+
import pick from '@valkyriestudios/utils/object/pick';
|
|
885
1102
|
pick({a: 1, b: 2, c: 3}, ['a','b']); // {a: 1, b: 2}
|
|
886
1103
|
```
|
|
887
1104
|
|
|
888
|
-
|
|
1105
|
+
### object/merge(target:Object={},obj:Object|Object[]={}, opts?:{union?:boolean})
|
|
889
1106
|
Merges two objects together, with the preference over the second object.
|
|
890
1107
|
```typescript
|
|
1108
|
+
import merge from '@valkyriestudios/utils/object/merge';
|
|
891
1109
|
merge({a: 1, b: false}, {a: 900, c: 50}, {union: true}); // {a: 900, b: false, c: 50}
|
|
892
1110
|
merge({a: 1, b: false}, {a: 900, c: 50}, {union: false}); // {a: 900, b: false}
|
|
893
1111
|
merge({a: 1, c: {bar: 'foo'}}, [{b: 2}, {c: {foo: 'bar'}}], {union: true}); // {a: 1, b: 2, c: {bar: 'foo', foo: 'bar'}}
|
|
@@ -896,68 +1114,73 @@ merge({a: 1, c: {bar: 'foo'}}, [{b: 2}, {c: {foo: 'bar'}}], {union: true}); // {
|
|
|
896
1114
|
Take Note: The default behavior is to not have union, this means that ONLY the keys in the target object
|
|
897
1115
|
are going to be available in the response of this function.
|
|
898
1116
|
|
|
899
|
-
|
|
1117
|
+
### object/define(props:Object, obj:Object={})
|
|
900
1118
|
Creates an object with the passed accessors set on it
|
|
901
1119
|
```typescript
|
|
1120
|
+
import define from '@valkyriestudios/utils/object/define';
|
|
902
1121
|
define(
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
1122
|
+
{
|
|
1123
|
+
a: {
|
|
1124
|
+
enumerable: false,
|
|
1125
|
+
value : function () { ... }
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
{ b: 2 }
|
|
910
1129
|
);
|
|
911
1130
|
// { a : () => ..., b: 2 }
|
|
912
1131
|
```
|
|
913
1132
|
|
|
914
1133
|
```typescript
|
|
1134
|
+
import define from '@valkyriestudios/utils/object/define';
|
|
915
1135
|
define({
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
1136
|
+
a : {
|
|
1137
|
+
enumerable: false,
|
|
1138
|
+
value : function () { ... }
|
|
1139
|
+
}
|
|
920
1140
|
}); // { a : () => ... }
|
|
921
1141
|
```
|
|
922
1142
|
|
|
923
|
-
### regexp
|
|
924
|
-
- **isRegExp(val:unknown)**
|
|
1143
|
+
### regexp/is(val:unknown)
|
|
925
1144
|
Check if a variable is an instance of RegExp
|
|
926
1145
|
```typescript
|
|
1146
|
+
import isRegExp from '@valkyriestudios/utils/regexp/is';
|
|
927
1147
|
isRegExp('foo'); // FALSE
|
|
928
1148
|
isRegExp(new RegExp('ab+c', 'i')); // TRUE
|
|
929
1149
|
isRegExp(new RegExp(/ab+c/, 'i')); // TRUE
|
|
930
1150
|
isRegExp(/ab+c/i); // FALSE
|
|
931
1151
|
```
|
|
932
1152
|
|
|
933
|
-
|
|
1153
|
+
### regexp/sanitize(val:string)
|
|
934
1154
|
Escapes special characters in a string and returns a sanitized version safe for usage in RegExp instances
|
|
935
1155
|
```typescript
|
|
936
|
-
|
|
1156
|
+
import sanitize from '@valkyriestudios/utils/regexp/sanitize';
|
|
1157
|
+
sanitize('contact@valkyriestudios.be'); // contact@valkyriestudios\\.be
|
|
937
1158
|
```
|
|
938
1159
|
|
|
939
|
-
### string
|
|
940
|
-
- **isString(val:unknown)**
|
|
1160
|
+
### string/is(val:unknown)
|
|
941
1161
|
Check if a variable is a string
|
|
942
1162
|
```typescript
|
|
1163
|
+
import isString from '@valkyriestudios/utils/string/is';
|
|
943
1164
|
isString('foo'); // TRUE
|
|
944
1165
|
isString(4); // FALSE
|
|
945
1166
|
```
|
|
946
1167
|
|
|
947
|
-
|
|
1168
|
+
### string/isBetween(val:string, min:number, max:number, trimmed:boolean=true)
|
|
948
1169
|
Check if a variable is between a range of numbers
|
|
949
1170
|
```typescript
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
1171
|
+
import isBetween from '@valkyriestudios/utils/string/isBetween';
|
|
1172
|
+
isBetween('Peter', 4, 10); // TRUE
|
|
1173
|
+
isBetween('Jeff', 4, 10); // TRUE
|
|
1174
|
+
isBetween('Moe', 4, 10); // FALSE
|
|
1175
|
+
isBetween('Hello', 6, 1); // FALSE
|
|
1176
|
+
isBetween(' Joe', 1, 3); // TRUE
|
|
1177
|
+
isBetween(' Joe', 1, 3, false); // FALSE
|
|
956
1178
|
```
|
|
957
1179
|
|
|
958
|
-
|
|
1180
|
+
### string/isNotEmpty(val:unknown, trimmed:boolean=true)
|
|
959
1181
|
Check if a variable a non-empty string
|
|
960
1182
|
```typescript
|
|
1183
|
+
import isNotEmptyString from '@valkyriestudios/utils/string/isNotEmpty';
|
|
961
1184
|
isNotEmptyString({a:1}); // FALSE
|
|
962
1185
|
isNotEmptyString(''); // FALSE
|
|
963
1186
|
isNotEmptyString(' '); // FALSE
|
|
@@ -965,9 +1188,10 @@ isNotEmptyString(' ', false); // TRUE
|
|
|
965
1188
|
isNotEmptyString('Hi'); // TRUE
|
|
966
1189
|
```
|
|
967
1190
|
|
|
968
|
-
|
|
1191
|
+
### string/shorten(val:string, length:integer, postfix:string=..., truncate_words=true)
|
|
969
1192
|
Shorten a string and add a postfix if string went over length
|
|
970
1193
|
```typescript
|
|
1194
|
+
import shorten from '@valkyriestudios/utils/string/shorten';
|
|
971
1195
|
shorten('To the moon and beyond', 11, '..'); // 'To the moon..'
|
|
972
1196
|
shorten('Hi', 250); // 'Hi'
|
|
973
1197
|
shorten('To the moon and beyond'); // 'To the moon...'
|
|
@@ -977,21 +1201,26 @@ shorten('To the moon and beyond', 11, ' '); // 'To the moon '
|
|
|
977
1201
|
shorten('To the moon and beyond', 11, '...', false);
|
|
978
1202
|
```
|
|
979
1203
|
|
|
980
|
-
|
|
1204
|
+
### string/humanizeBytes(val:number|string)
|
|
981
1205
|
Humanize an amount of bytes
|
|
1206
|
+
|
|
1207
|
+
allows passing options to control the output, following options are possible:
|
|
982
1208
|
-- option:delim (default:','): Override the delimiter used, eg: `20000 -> 20,000`
|
|
983
1209
|
-- option:separator (default:'.'): Override the separator used for floats, eg: '20.034' -> '20,034'
|
|
984
1210
|
-- option:precision (default:2): Override decimal precision for floats: eg: '20.0344233' with precision 2 -> '20.03'
|
|
985
1211
|
-- option:units (default:[' byes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']): Override units used, eg: `4893290423489 with units [' Jedi', ' Darth', ' Vader', ' Force'] and precision of 0` -> `'4,893 Force'`
|
|
986
1212
|
```typescript
|
|
1213
|
+
import humanizeBytes from '@valkyriestudios/utils/string/humanizeBytes';
|
|
987
1214
|
humanizeBytes(1504230); // '1.4 MB'
|
|
988
1215
|
humanizeBytes(23); // '23 bytes'
|
|
989
1216
|
humanizeBytes(-374237489237); // '-348.5 GB'
|
|
990
1217
|
humanizeBytes('-1504230'); // '-1.4 MB'
|
|
991
1218
|
```
|
|
992
1219
|
|
|
993
|
-
|
|
1220
|
+
### string/humanizeNumber(val:number|string, options:Object)
|
|
994
1221
|
Humanize a number
|
|
1222
|
+
|
|
1223
|
+
allows passing options to control the output, following options are possible:
|
|
995
1224
|
-- option:delim (default:','): Override the delimiter used, eg: `20000 -> 20,000`
|
|
996
1225
|
-- option:separator (default:'.'): Override the separator used for floats, eg: '20.034' -> '20,034'
|
|
997
1226
|
-- option:precision (default:2): Override decimal precision for floats: eg: '20.0344233' with precision 2 -> '20.03'
|
|
@@ -1000,6 +1229,7 @@ Humanize a number
|
|
|
1000
1229
|
-- option:divider (default:1000): Override default divider used for units (used internally for humanizeBytes with 1024 as divider)
|
|
1001
1230
|
|
|
1002
1231
|
```typescript
|
|
1232
|
+
import humanizeBytes from '@valkyriestudios/utils/string/humanizeBytes';
|
|
1003
1233
|
humanizeNumber(4327963279469432); // '4.33q'
|
|
1004
1234
|
humanizeNumber(1504230); // '1.5m'
|
|
1005
1235
|
humanizeNumber(-432443); // '-432.44k'
|
|
@@ -1007,7 +1237,6 @@ humanizeNumber('-1500'); // '-1.5k'
|
|
|
1007
1237
|
humanizeNumber(47328748923747923479); // '47,328.75q'
|
|
1008
1238
|
```
|
|
1009
1239
|
|
|
1010
|
-
allows passing options to control the output, following options are possible:
|
|
1011
|
-
|
|
1012
1240
|
## Contributors
|
|
1013
1241
|
- [Peter Vermeulen](https://www.linkedin.com/in/petervermeulen1/)
|
|
1242
|
+
- [Xander Berkein](https://github.com/xanderberkein)
|