@valkyriestudios/utils 12.20.0 → 12.21.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 +349 -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 +16 -0
- package/formdata/toObject.js +23 -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 +36 -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
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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`
|
|
428
458
|
|
|
429
|
-
Available
|
|
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,91 @@ 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
|
|
672
738
|
|
|
673
|
-
|
|
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
|
+
````
|
|
746
|
+
|
|
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)
|
|
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
|
+
### hash/guid()
|
|
702
804
|
Generate a unique identifier (guid) according to RFC4122
|
|
703
805
|
```typescript
|
|
806
|
+
import guid from '@valkyriestudios/utils/hash/guid';
|
|
704
807
|
guid(); // 245caf1a-86af-11e7-bb31-be2e44b06b34
|
|
705
808
|
```
|
|
706
809
|
|
|
707
|
-
|
|
810
|
+
### hash/fnv1A(val:unknown)
|
|
708
811
|
Generate a fnv1A hash from an object, using a 32-bit prime/offset
|
|
709
812
|
```typescript
|
|
813
|
+
import fnv1A from '@valkyriestudios/utils/hash/fnv1A';
|
|
710
814
|
fnv1A('hello world'); // -2023343616
|
|
711
815
|
fnv1A({a:1,b:2}); // 361168128
|
|
712
816
|
fnv1A(4); // 1630425728
|
|
@@ -714,54 +818,114 @@ fnv1A(new RegExp(/ab+c/, 'i')); // 2131692544
|
|
|
714
818
|
fnv1A(new Date('2012-02-02')); // 1655579136
|
|
715
819
|
```
|
|
716
820
|
|
|
717
|
-
###
|
|
718
|
-
|
|
821
|
+
### Is
|
|
822
|
+
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.
|
|
823
|
+
|
|
824
|
+
These functions are the following:
|
|
825
|
+
- **Is.Array**
|
|
826
|
+
- **Is.NeArray**
|
|
827
|
+
- **Is.NotEmptyArray**
|
|
828
|
+
- **Is.Boolean**
|
|
829
|
+
- **Is.Date**
|
|
830
|
+
- **Is.Formdata**
|
|
831
|
+
- **Is.Function**
|
|
832
|
+
- **Is.AsyncFunction**
|
|
833
|
+
- **Is.Num**
|
|
834
|
+
- **Is.NumBetween**
|
|
835
|
+
- **Is.NumAbove**
|
|
836
|
+
- **Is.NumAboveOrEqual**
|
|
837
|
+
- **Is.NumBelow**
|
|
838
|
+
- **Is.NumBelowOrEqual**
|
|
839
|
+
- **Is.NumGt**
|
|
840
|
+
- **Is.NumGte**
|
|
841
|
+
- **Is.NumLt**
|
|
842
|
+
- **Is.NumLte**
|
|
843
|
+
- **Is.Number**
|
|
844
|
+
- **Is.NumberBetween**
|
|
845
|
+
- **Is.NumberAbove**
|
|
846
|
+
- **Is.NumberAboveOrEqual**
|
|
847
|
+
- **Is.NumberBelow**
|
|
848
|
+
- **Is.NumberBelowOrEqual**
|
|
849
|
+
- **Is.Int**
|
|
850
|
+
- **Is.IntBetween**
|
|
851
|
+
- **Is.IntAbove**
|
|
852
|
+
- **Is.IntAboveOrEqual**
|
|
853
|
+
- **Is.IntBelow**
|
|
854
|
+
- **Is.IntBelowOrEqual**
|
|
855
|
+
- **Is.IntGt**
|
|
856
|
+
- **Is.IntGte**
|
|
857
|
+
- **Is.IntLt**
|
|
858
|
+
- **Is.IntLte**
|
|
859
|
+
- **Is.Integer**
|
|
860
|
+
- **Is.IntegerBetween**
|
|
861
|
+
- **Is.IntegerBelow**
|
|
862
|
+
- **Is.IntegerBelowOrEqual**
|
|
863
|
+
- **Is.IntegerAbove**
|
|
864
|
+
- **Is.IntegerAboveOrEqual**
|
|
865
|
+
- **Is.RegExp**
|
|
866
|
+
- **Is.Object**
|
|
867
|
+
- **Is.NeObject**
|
|
868
|
+
- **Is.NotEmptyObject**
|
|
869
|
+
- **Is.String**
|
|
870
|
+
- **Is.StringBetween**
|
|
871
|
+
- **Is.NeString**
|
|
872
|
+
- **Is.NotEmptyString**
|
|
873
|
+
- **Is.Equal**
|
|
874
|
+
- **Is.Eq**
|
|
875
|
+
|
|
876
|
+
### number/is(val:unknown)
|
|
719
877
|
Check if a variable is a number
|
|
720
878
|
```typescript
|
|
879
|
+
import isNumber from '@valkyriestudios/utils/number/is';
|
|
721
880
|
isNumber('foo'); // FALSE
|
|
722
881
|
isNumber(4); // TRUE
|
|
723
882
|
isNumber(0.5); // TRUE
|
|
724
883
|
```
|
|
725
884
|
|
|
726
|
-
|
|
885
|
+
### number/isAbove(val:number, comp:number)
|
|
727
886
|
Check if a variable is a number above a certain bound
|
|
728
887
|
```typescript
|
|
888
|
+
import isNumberAbove from '@valkyriestudios/utils/number/isAbove';
|
|
729
889
|
isNumberAbove(5, 0); // TRUE
|
|
730
890
|
isNumberAbove(.1, 0); // TRUE
|
|
731
891
|
isNumberAbove(-1, -1); // FALSE
|
|
732
892
|
isNumberAbove(-10, -9); // FALSE
|
|
733
893
|
```
|
|
734
894
|
|
|
735
|
-
|
|
895
|
+
### number/isAboveOrEqual(val:number, comp:number)
|
|
736
896
|
Check if a variable is a number above or equal to a certain bound
|
|
737
897
|
```typescript
|
|
898
|
+
import isNumberAboveOrEqual from '@valkyriestudios/utils/number/isAboveOrEqual';
|
|
738
899
|
isNumberAboveOrEqual(5, 0); // TRUE
|
|
739
900
|
isNumberAboveOrEqual(.1, 0); // TRUE
|
|
740
901
|
isNumberAboveOrEqual(-1, -1); // TRUE
|
|
741
902
|
isNumberAboveOrEqual(-10, -9); // FALSE
|
|
742
903
|
```
|
|
743
904
|
|
|
744
|
-
|
|
905
|
+
### number/isBelow(val:number, comp:number)
|
|
745
906
|
Check if a variable is a number below a certain bound
|
|
746
907
|
```typescript
|
|
908
|
+
import isNumberBelow from '@valkyriestudios/utils/number/isBelow';
|
|
747
909
|
isNumberBelow(0, 5); // TRUE
|
|
748
910
|
isNumberBelow(0, .1); // TRUE
|
|
749
911
|
isNumberBelow(-1, -1); // FALSE
|
|
750
912
|
isNumberBelow(-9, -10); // FALSE
|
|
751
913
|
```
|
|
752
914
|
|
|
753
|
-
|
|
915
|
+
### number/isBelowOrEqual(val:number, comp:number)
|
|
754
916
|
Check if a variable is a number below or equal a certain bound
|
|
755
917
|
```typescript
|
|
918
|
+
import isNumberBelowOrEqual from '@valkyriestudios/utils/number/isBelowOrEqual';
|
|
756
919
|
isNumberBelowOrEqual(0, 5); // TRUE
|
|
757
920
|
isNumberBelowOrEqual(0, .1); // TRUE
|
|
758
921
|
isNumberBelowOrEqual(-1, -1); // TRUE
|
|
759
922
|
isNumberBelowOrEqual(-9, -10); // FALSE
|
|
760
923
|
```
|
|
761
924
|
|
|
762
|
-
|
|
925
|
+
### number/isBetween(val:number, min:number, max:number)
|
|
763
926
|
Check if a variable is a number between a range of numbers
|
|
764
927
|
```typescript
|
|
928
|
+
import isNumberBetween from '@valkyriestudios/utils/number/isBetween';
|
|
765
929
|
isNumberBetween(5, 0, 10); // TRUE
|
|
766
930
|
isNumberBetween(.1, 0, 1); // TRUE
|
|
767
931
|
isNumberBetween(-.1, -1, 0); // TRUE
|
|
@@ -769,35 +933,39 @@ isNumberBetween(0, 0, 1); // TRUE
|
|
|
769
933
|
isNumberBetween(-1, 0, 1); // FALSE
|
|
770
934
|
```
|
|
771
935
|
|
|
772
|
-
|
|
936
|
+
### number/isInteger(val:unknown)
|
|
773
937
|
Check if a variable is an integer
|
|
774
938
|
```typescript
|
|
939
|
+
import isInteger from '@valkyriestudios/utils/number/isInteger';
|
|
775
940
|
isInteger('foo'); // FALSE
|
|
776
941
|
isInteger(4); // TRUE
|
|
777
942
|
isInteger(0.5); // FALSE
|
|
778
943
|
```
|
|
779
944
|
|
|
780
|
-
|
|
945
|
+
### number/isIntegerAbove(val:number, comp:number)
|
|
781
946
|
Check if a variable is an integer above a certain bound
|
|
782
947
|
```typescript
|
|
948
|
+
import isIntegerAbove from '@valkyriestudios/utils/number/isIntegerAbove';
|
|
783
949
|
isIntegerAbove(5, 0); // TRUE
|
|
784
950
|
isIntegerAbove(.1, 0); // FALSE
|
|
785
951
|
isIntegerAbove(-1, -1); // FALSE
|
|
786
952
|
isIntegerAbove(-10, -9); // FALSE
|
|
787
953
|
```
|
|
788
954
|
|
|
789
|
-
|
|
955
|
+
### number/isIntegerAboveOrEqual(val:number, comp:number)
|
|
790
956
|
Check if a variable is an integer above or equal to a certain bound
|
|
791
957
|
```typescript
|
|
958
|
+
import isIntegerAboveOrEqual from '@valkyriestudios/utils/number/isIntegerAboveOrEqual';
|
|
792
959
|
isIntegerAboveOrEqual(5, 0); // TRUE
|
|
793
960
|
isIntegerAboveOrEqual(.1, 0); // FALSE
|
|
794
961
|
isIntegerAboveOrEqual(-1, -1); // TRUE
|
|
795
962
|
isIntegerAboveOrEqual(-10, -9); // FALSE
|
|
796
963
|
```
|
|
797
964
|
|
|
798
|
-
|
|
965
|
+
### number/isIntegerBelow(val:number, comp:number)
|
|
799
966
|
Check if a variable is an integer below a certain bound
|
|
800
967
|
```typescript
|
|
968
|
+
import isIntegerBelow from '@valkyriestudios/utils/number/isIntegerBelow';
|
|
801
969
|
isIntegerBelow(0, 5); // TRUE
|
|
802
970
|
isIntegerBelow(0, .1); // TRUE
|
|
803
971
|
isIntegerBelow(.4, 5); // FALSE
|
|
@@ -805,9 +973,10 @@ isIntegerBelow(-1, -1); // FALSE
|
|
|
805
973
|
isIntegerBelow(-9, -10); // FALSE
|
|
806
974
|
```
|
|
807
975
|
|
|
808
|
-
|
|
976
|
+
### number/isIntegerBelowOrEqual(val:number, comp:number)
|
|
809
977
|
Check if a variable is an integer below or equal to a certain bound
|
|
810
978
|
```typescript
|
|
979
|
+
import isIntegerBelowOrEqual from '@valkyriestudios/utils/number/isIntegerBelowOrEqual';
|
|
811
980
|
isIntegerBelowOrEqual(0, 5); // TRUE
|
|
812
981
|
isIntegerBelowOrEqual(0, .1); // TRUE
|
|
813
982
|
isIntegerBelowOrEqual(.4, 5); // FALSE
|
|
@@ -815,9 +984,10 @@ isIntegerBelowOrEqual(-1, -1); // TRUE
|
|
|
815
984
|
isIntegerBelowOrEqual(-9, -10); // FALSE
|
|
816
985
|
```
|
|
817
986
|
|
|
818
|
-
|
|
987
|
+
### number/isIntegerBetween(val:number, min:number, max:number)
|
|
819
988
|
Check if a variable is an integer between a range of numbers
|
|
820
989
|
```typescript
|
|
990
|
+
import isIntegerBetween from '@valkyriestudios/utils/number/isIntegerBetween';
|
|
821
991
|
isIntegerBetween(5, 0, 10); // TRUE
|
|
822
992
|
isIntegerBetween(.1, 0, 1); // FALSE
|
|
823
993
|
isIntegerBetween(-.1, -1, 0); // FALSE
|
|
@@ -825,69 +995,77 @@ isIntegerBetween(0, 0, 1); // TRUE
|
|
|
825
995
|
isIntegerBetween(-1, 0, 1); // FALSE
|
|
826
996
|
```
|
|
827
997
|
|
|
828
|
-
|
|
998
|
+
### number/isNumericalNaN(val:unknown)
|
|
829
999
|
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
1000
|
```typescript
|
|
1001
|
+
import isNumericalNaN from '@valkyriestudios/utils/number/isNumericalNaN';
|
|
831
1002
|
isNumericalNaN('foo'); // FALSE
|
|
832
1003
|
isNumericalNaN(NaN); // TRUE
|
|
833
1004
|
```
|
|
834
1005
|
|
|
835
|
-
|
|
1006
|
+
### number/toPercentage(val:Number,precision:Number=0,min:Number=0,max:Number=1)
|
|
836
1007
|
Calculate the percentage of a specific value in a range
|
|
837
1008
|
```typescript
|
|
1009
|
+
import toPercentage from '@valkyriestudios/utils/number/toPercentage';
|
|
838
1010
|
toPercentage(0.50106579, 5); // 50.11658
|
|
839
1011
|
toPercentage(-356, 0, -1000, 1000); // 32
|
|
840
1012
|
toPercentage(0.5); // 50
|
|
841
1013
|
```
|
|
842
1014
|
|
|
843
|
-
|
|
1015
|
+
### number/round(val:Number,precision:Number=0)
|
|
844
1016
|
Round a numeric value to a specific amount of decimals
|
|
845
1017
|
```typescript
|
|
1018
|
+
import round from '@valkyriestudios/utils/number/round';
|
|
846
1019
|
round(5.123456789, 0); // 5
|
|
847
1020
|
round(5.123456789, 2); // 5.12
|
|
848
1021
|
round(5.123456789, 5); // 5.12346
|
|
849
1022
|
```
|
|
850
1023
|
|
|
851
|
-
|
|
1024
|
+
### number/randomBetween(min:Number=0,max:Number=10)
|
|
852
1025
|
Generate a random numeric value between a min and max range
|
|
853
1026
|
```typescript
|
|
1027
|
+
import randomBetween from '@valkyriestudios/utils/number/randomBetween';
|
|
854
1028
|
randomBetween(); // Will generate a random between 0 and 10
|
|
855
1029
|
randomBetween(25, 100); // Will generate a random between 25 and 100
|
|
856
1030
|
```
|
|
857
1031
|
|
|
858
|
-
|
|
1032
|
+
### number/randomIntBetween(min:Number=0,max:Number=10)
|
|
859
1033
|
Generate a random numeric value between a min and max range (max not inclusive)
|
|
860
1034
|
```typescript
|
|
1035
|
+
import randomIntBetween from '@valkyriestudios/utils/number/randomIntBetween';
|
|
861
1036
|
randomIntBetween(); // Will generate a random between 0 and 10 (10 not inclusive)
|
|
862
1037
|
randomIntBetween(25, 100); // Will generate a random between 25 and 100 (100 not inclusive)
|
|
863
1038
|
```
|
|
864
1039
|
|
|
865
|
-
### object
|
|
866
|
-
- **isObject(val:unknown)**
|
|
1040
|
+
### object/is(val:unknown)
|
|
867
1041
|
Check if a variable is of type Object
|
|
868
1042
|
```typescript
|
|
1043
|
+
import isObject from '@valkyriestudios/utils/object/is';
|
|
869
1044
|
isObject({a: 1}); // TRUE
|
|
870
1045
|
isObject(1); // FALSE
|
|
871
1046
|
```
|
|
872
1047
|
|
|
873
|
-
|
|
1048
|
+
### object/isNotEmpty(val:unknown)
|
|
874
1049
|
Check if a variable a non-empty object
|
|
875
1050
|
```typescript
|
|
1051
|
+
import isNotEmptyObject from '@valkyriestudios/utils/object/isNotEmpty';
|
|
876
1052
|
isNotEmptyObject({a:1}); // TRUE
|
|
877
1053
|
isNotEmptyObject({}); // FALSE
|
|
878
1054
|
isNotEmptyObject('Hi'); // FALSE
|
|
879
1055
|
```
|
|
880
1056
|
|
|
881
|
-
|
|
1057
|
+
### object/pick(obj:Object={}, keys:Array[string]=[])
|
|
882
1058
|
Copies the keys passed in the 'keys' array from the passed object to a new object and returns that object.**
|
|
883
1059
|
<small>If a key wasn't found it will be set as undefined</small>
|
|
884
1060
|
```typescript
|
|
1061
|
+
import pick from '@valkyriestudios/utils/object/pick';
|
|
885
1062
|
pick({a: 1, b: 2, c: 3}, ['a','b']); // {a: 1, b: 2}
|
|
886
1063
|
```
|
|
887
1064
|
|
|
888
|
-
|
|
1065
|
+
### object/merge(target:Object={},obj:Object|Object[]={}, opts?:{union?:boolean})
|
|
889
1066
|
Merges two objects together, with the preference over the second object.
|
|
890
1067
|
```typescript
|
|
1068
|
+
import merge from '@valkyriestudios/utils/object/merge';
|
|
891
1069
|
merge({a: 1, b: false}, {a: 900, c: 50}, {union: true}); // {a: 900, b: false, c: 50}
|
|
892
1070
|
merge({a: 1, b: false}, {a: 900, c: 50}, {union: false}); // {a: 900, b: false}
|
|
893
1071
|
merge({a: 1, c: {bar: 'foo'}}, [{b: 2}, {c: {foo: 'bar'}}], {union: true}); // {a: 1, b: 2, c: {bar: 'foo', foo: 'bar'}}
|
|
@@ -896,68 +1074,73 @@ merge({a: 1, c: {bar: 'foo'}}, [{b: 2}, {c: {foo: 'bar'}}], {union: true}); // {
|
|
|
896
1074
|
Take Note: The default behavior is to not have union, this means that ONLY the keys in the target object
|
|
897
1075
|
are going to be available in the response of this function.
|
|
898
1076
|
|
|
899
|
-
|
|
1077
|
+
### object/define(props:Object, obj:Object={})
|
|
900
1078
|
Creates an object with the passed accessors set on it
|
|
901
1079
|
```typescript
|
|
1080
|
+
import define from '@valkyriestudios/utils/object/define';
|
|
902
1081
|
define(
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
1082
|
+
{
|
|
1083
|
+
a: {
|
|
1084
|
+
enumerable: false,
|
|
1085
|
+
value : function () { ... }
|
|
1086
|
+
}
|
|
1087
|
+
},
|
|
1088
|
+
{ b: 2 }
|
|
910
1089
|
);
|
|
911
1090
|
// { a : () => ..., b: 2 }
|
|
912
1091
|
```
|
|
913
1092
|
|
|
914
1093
|
```typescript
|
|
1094
|
+
import define from '@valkyriestudios/utils/object/define';
|
|
915
1095
|
define({
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
1096
|
+
a : {
|
|
1097
|
+
enumerable: false,
|
|
1098
|
+
value : function () { ... }
|
|
1099
|
+
}
|
|
920
1100
|
}); // { a : () => ... }
|
|
921
1101
|
```
|
|
922
1102
|
|
|
923
|
-
### regexp
|
|
924
|
-
- **isRegExp(val:unknown)**
|
|
1103
|
+
### regexp/is(val:unknown)
|
|
925
1104
|
Check if a variable is an instance of RegExp
|
|
926
1105
|
```typescript
|
|
1106
|
+
import isRegExp from '@valkyriestudios/utils/regexp/is';
|
|
927
1107
|
isRegExp('foo'); // FALSE
|
|
928
1108
|
isRegExp(new RegExp('ab+c', 'i')); // TRUE
|
|
929
1109
|
isRegExp(new RegExp(/ab+c/, 'i')); // TRUE
|
|
930
1110
|
isRegExp(/ab+c/i); // FALSE
|
|
931
1111
|
```
|
|
932
1112
|
|
|
933
|
-
|
|
1113
|
+
### regexp/sanitize(val:string)
|
|
934
1114
|
Escapes special characters in a string and returns a sanitized version safe for usage in RegExp instances
|
|
935
1115
|
```typescript
|
|
936
|
-
|
|
1116
|
+
import sanitize from '@valkyriestudios/utils/regexp/sanitize';
|
|
1117
|
+
sanitize('contact@valkyriestudios.be'); // contact@valkyriestudios\\.be
|
|
937
1118
|
```
|
|
938
1119
|
|
|
939
|
-
### string
|
|
940
|
-
- **isString(val:unknown)**
|
|
1120
|
+
### string/is(val:unknown)
|
|
941
1121
|
Check if a variable is a string
|
|
942
1122
|
```typescript
|
|
1123
|
+
import isString from '@valkyriestudios/utils/string/is';
|
|
943
1124
|
isString('foo'); // TRUE
|
|
944
1125
|
isString(4); // FALSE
|
|
945
1126
|
```
|
|
946
1127
|
|
|
947
|
-
|
|
1128
|
+
### string/isBetween(val:string, min:number, max:number, trimmed:boolean=true)
|
|
948
1129
|
Check if a variable is between a range of numbers
|
|
949
1130
|
```typescript
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
1131
|
+
import isBetween from '@valkyriestudios/utils/string/isBetween';
|
|
1132
|
+
isBetween('Peter', 4, 10); // TRUE
|
|
1133
|
+
isBetween('Jeff', 4, 10); // TRUE
|
|
1134
|
+
isBetween('Moe', 4, 10); // FALSE
|
|
1135
|
+
isBetween('Hello', 6, 1); // FALSE
|
|
1136
|
+
isBetween(' Joe', 1, 3); // TRUE
|
|
1137
|
+
isBetween(' Joe', 1, 3, false); // FALSE
|
|
956
1138
|
```
|
|
957
1139
|
|
|
958
|
-
|
|
1140
|
+
### string/isNotEmpty(val:unknown, trimmed:boolean=true)
|
|
959
1141
|
Check if a variable a non-empty string
|
|
960
1142
|
```typescript
|
|
1143
|
+
import isNotEmptyString from '@valkyriestudios/utils/string/isNotEmpty';
|
|
961
1144
|
isNotEmptyString({a:1}); // FALSE
|
|
962
1145
|
isNotEmptyString(''); // FALSE
|
|
963
1146
|
isNotEmptyString(' '); // FALSE
|
|
@@ -965,9 +1148,10 @@ isNotEmptyString(' ', false); // TRUE
|
|
|
965
1148
|
isNotEmptyString('Hi'); // TRUE
|
|
966
1149
|
```
|
|
967
1150
|
|
|
968
|
-
|
|
1151
|
+
### string/shorten(val:string, length:integer, postfix:string=..., truncate_words=true)
|
|
969
1152
|
Shorten a string and add a postfix if string went over length
|
|
970
1153
|
```typescript
|
|
1154
|
+
import shorten from '@valkyriestudios/utils/string/shorten';
|
|
971
1155
|
shorten('To the moon and beyond', 11, '..'); // 'To the moon..'
|
|
972
1156
|
shorten('Hi', 250); // 'Hi'
|
|
973
1157
|
shorten('To the moon and beyond'); // 'To the moon...'
|
|
@@ -977,21 +1161,26 @@ shorten('To the moon and beyond', 11, ' '); // 'To the moon '
|
|
|
977
1161
|
shorten('To the moon and beyond', 11, '...', false);
|
|
978
1162
|
```
|
|
979
1163
|
|
|
980
|
-
|
|
1164
|
+
### string/humanizeBytes(val:number|string)
|
|
981
1165
|
Humanize an amount of bytes
|
|
1166
|
+
|
|
1167
|
+
allows passing options to control the output, following options are possible:
|
|
982
1168
|
-- option:delim (default:','): Override the delimiter used, eg: `20000 -> 20,000`
|
|
983
1169
|
-- option:separator (default:'.'): Override the separator used for floats, eg: '20.034' -> '20,034'
|
|
984
1170
|
-- option:precision (default:2): Override decimal precision for floats: eg: '20.0344233' with precision 2 -> '20.03'
|
|
985
1171
|
-- 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
1172
|
```typescript
|
|
1173
|
+
import humanizeBytes from '@valkyriestudios/utils/string/humanizeBytes';
|
|
987
1174
|
humanizeBytes(1504230); // '1.4 MB'
|
|
988
1175
|
humanizeBytes(23); // '23 bytes'
|
|
989
1176
|
humanizeBytes(-374237489237); // '-348.5 GB'
|
|
990
1177
|
humanizeBytes('-1504230'); // '-1.4 MB'
|
|
991
1178
|
```
|
|
992
1179
|
|
|
993
|
-
|
|
1180
|
+
### string/humanizeNumber(val:number|string, options:Object)
|
|
994
1181
|
Humanize a number
|
|
1182
|
+
|
|
1183
|
+
allows passing options to control the output, following options are possible:
|
|
995
1184
|
-- option:delim (default:','): Override the delimiter used, eg: `20000 -> 20,000`
|
|
996
1185
|
-- option:separator (default:'.'): Override the separator used for floats, eg: '20.034' -> '20,034'
|
|
997
1186
|
-- option:precision (default:2): Override decimal precision for floats: eg: '20.0344233' with precision 2 -> '20.03'
|
|
@@ -1000,6 +1189,7 @@ Humanize a number
|
|
|
1000
1189
|
-- option:divider (default:1000): Override default divider used for units (used internally for humanizeBytes with 1024 as divider)
|
|
1001
1190
|
|
|
1002
1191
|
```typescript
|
|
1192
|
+
import humanizeBytes from '@valkyriestudios/utils/string/humanizeBytes';
|
|
1003
1193
|
humanizeNumber(4327963279469432); // '4.33q'
|
|
1004
1194
|
humanizeNumber(1504230); // '1.5m'
|
|
1005
1195
|
humanizeNumber(-432443); // '-432.44k'
|
|
@@ -1007,7 +1197,6 @@ humanizeNumber('-1500'); // '-1.5k'
|
|
|
1007
1197
|
humanizeNumber(47328748923747923479); // '47,328.75q'
|
|
1008
1198
|
```
|
|
1009
1199
|
|
|
1010
|
-
allows passing options to control the output, following options are possible:
|
|
1011
|
-
|
|
1012
1200
|
## Contributors
|
|
1013
1201
|
- [Peter Vermeulen](https://www.linkedin.com/in/petervermeulen1/)
|
|
1202
|
+
- [Xander Berkein](https://github.com/xanderberkein)
|