@valkyriestudios/utils 12.9.0 → 12.11.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 +103 -81
- package/array/dedupe.js +7 -7
- package/array/groupBy.js +3 -4
- package/array/index.js +1 -1
- package/array/is.js +3 -4
- package/array/isNotEmpty.js +3 -4
- package/array/join.d.ts +6 -0
- package/array/join.js +16 -11
- package/array/mapFn.js +7 -7
- package/array/mapKey.js +7 -8
- package/array/mapPrimitive.js +12 -18
- package/array/shuffle.js +3 -4
- package/array/sort.d.ts +4 -5
- package/array/sort.js +18 -23
- package/boolean/index.js +1 -1
- package/boolean/is.js +3 -4
- package/caching/index.js +1 -1
- package/caching/memoize.d.ts +2 -1
- package/caching/memoize.js +40 -14
- package/date/addUTC.js +3 -4
- package/date/diff.js +3 -6
- package/date/endOfUTC.js +3 -4
- package/date/format.js +17 -14
- package/date/index.js +1 -1
- package/date/is.js +3 -4
- package/date/nowUnix.js +3 -4
- package/date/nowUnixMs.js +3 -4
- package/date/startOfUTC.js +3 -4
- package/date/toUTC.js +3 -4
- package/date/toUnix.js +3 -4
- package/deep/freeze.js +4 -5
- package/deep/get.js +4 -5
- package/deep/index.js +1 -1
- package/deep/seal.js +4 -5
- package/deep/set.js +3 -4
- package/equal.js +3 -4
- package/formdata/index.js +1 -1
- package/formdata/is.js +3 -4
- package/function/index.js +1 -1
- package/function/is.js +3 -4
- package/function/isAsync.js +3 -4
- package/function/noop.js +3 -4
- package/function/noopresolve.d.ts +1 -1
- package/function/noopresolve.js +3 -4
- package/function/noopreturn.d.ts +1 -1
- package/function/noopreturn.js +3 -4
- package/function/sleep.js +3 -4
- package/hash/fnv1A.js +3 -4
- package/hash/guid.js +3 -4
- package/hash/index.js +1 -1
- package/index.d.ts +16 -16
- package/is.js +1 -1
- package/number/index.js +1 -1
- package/number/is.js +3 -4
- package/number/isAbove.js +3 -4
- package/number/isAboveOrEqual.js +3 -4
- package/number/isBelow.js +3 -4
- package/number/isBelowOrEqual.js +3 -4
- package/number/isBetween.js +3 -4
- package/number/isInteger.js +3 -4
- package/number/isIntegerAbove.js +3 -4
- package/number/isIntegerAboveOrEqual.js +3 -4
- package/number/isIntegerBelow.js +3 -4
- package/number/isIntegerBelowOrEqual.js +3 -4
- package/number/isIntegerBetween.js +3 -4
- package/number/isNumericalNaN.js +3 -4
- package/number/randomBetween.js +3 -4
- package/number/randomIntBetween.js +3 -4
- package/number/round.js +3 -4
- package/number/toPercentage.js +3 -4
- package/object/define.js +3 -4
- package/object/index.js +1 -1
- package/object/is.js +3 -4
- package/object/isNotEmpty.js +3 -4
- package/object/merge.js +4 -7
- package/object/pick.js +5 -5
- package/package.json +1 -1
- package/regexp/index.js +1 -1
- package/regexp/is.js +3 -4
- package/regexp/sanitize.js +3 -4
- package/string/humanizeBytes.d.ts +2 -2
- package/string/humanizeBytes.js +8 -9
- package/string/humanizeNumber.d.ts +1 -1
- package/string/humanizeNumber.js +15 -30
- package/string/index.js +1 -1
- package/string/is.js +3 -4
- package/string/isBetween.js +3 -4
- package/string/isNotEmpty.js +3 -4
- package/string/shorten.js +3 -4
package/README.md
CHANGED
|
@@ -17,14 +17,14 @@ Zero-dependency collection of single-function utilities for common tasks
|
|
|
17
17
|
### array
|
|
18
18
|
- **isArray(val:any)**
|
|
19
19
|
Check if a variable is of type Array
|
|
20
|
-
```
|
|
20
|
+
```typescript
|
|
21
21
|
isArray({a:1}); // FALSE
|
|
22
22
|
isArray([]); // TRUE
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
- **isNotEmptyArray(val:any)**
|
|
26
26
|
Check if a variable a non-empty array
|
|
27
|
-
```
|
|
27
|
+
```typescript
|
|
28
28
|
isNotEmptyArray({a:1}); // FALSE
|
|
29
29
|
isNotEmptyArray([]); // FALSE
|
|
30
30
|
isNotEmptyArray([0, 1, 2]); // TRUE
|
|
@@ -32,14 +32,14 @@ isNotEmptyArray([0, 1, 2]); // TRUE
|
|
|
32
32
|
|
|
33
33
|
- **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
36
|
mapKey([
|
|
37
37
|
{uid: 12, name: 'Peter'},
|
|
38
38
|
{uid: 15, name: 'Jonas'},
|
|
39
39
|
{uid: 87, name: 'Josh'},
|
|
40
40
|
], 'uid');
|
|
41
41
|
|
|
42
|
-
output:
|
|
42
|
+
output:
|
|
43
43
|
|
|
44
44
|
{
|
|
45
45
|
12: {uid: 12, name: 'Peter'},
|
|
@@ -49,7 +49,7 @@ output:
|
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
Autofilters anything not meeting the spec:
|
|
52
|
-
```
|
|
52
|
+
```typescript
|
|
53
53
|
mapKey([
|
|
54
54
|
0,
|
|
55
55
|
{uid: 12, name: 'Peter'},
|
|
@@ -72,8 +72,8 @@ output:
|
|
|
72
72
|
}
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
allows merging objects onto existing keys:
|
|
76
|
-
```
|
|
75
|
+
allows merging objects onto existing keys:
|
|
76
|
+
```typescript
|
|
77
77
|
mapKey([
|
|
78
78
|
0,
|
|
79
79
|
{uid: 12, name: 'Peter'},
|
|
@@ -102,7 +102,7 @@ output:
|
|
|
102
102
|
- **mapFn(val:Record[], key:Function, opts:object={})**
|
|
103
103
|
Same behavior as mapKey but instead of a key, a function is passed to generate your own key. Eg:
|
|
104
104
|
|
|
105
|
-
```
|
|
105
|
+
```typescript
|
|
106
106
|
mapFn([
|
|
107
107
|
{uid: 12, name: 'Peter'},
|
|
108
108
|
{uid: 15, name: 'Jonas'},
|
|
@@ -122,7 +122,7 @@ options are the same as the mapKey function
|
|
|
122
122
|
|
|
123
123
|
- **mapPrimitive(val:any[], opts:object={valtrim:false,keyround:false,valround:false})**
|
|
124
124
|
Map an array of primitives (number/string)
|
|
125
|
-
```
|
|
125
|
+
```typescript
|
|
126
126
|
mapPrimitive([1,2,3]); // {1: 1, 2: 2, 3: 3}
|
|
127
127
|
mapPrimitive(['hello', 'hello', 'foo', 'bar']); // {hello: 'hello', foo: 'foo', bar: 'bar'}
|
|
128
128
|
mapPrimitive(['hello', ' hello', 'foo', ' foo'], {valtrim: true}); // {hello: 'hello', foo: 'foo'}
|
|
@@ -186,7 +186,7 @@ const group = groupBy([
|
|
|
186
186
|
|
|
187
187
|
- **dedupe(val:Array)**
|
|
188
188
|
Remove all duplicates from an array, behind the scenes it uses the fnv 1A hash algorithm to performantly do comparisons.
|
|
189
|
-
```
|
|
189
|
+
```typescript
|
|
190
190
|
dedupe(['a','a','b','c','c']); // ['a', 'b', 'c']
|
|
191
191
|
dedupe(['1',1,'2',2]); // ['1','2']
|
|
192
192
|
dedupe([new RegExp(/ab+c/, 'i'), new RegExp(/ab+c/, 'i')]); // [new RegExp(/ab+c/, 'i')]
|
|
@@ -194,30 +194,31 @@ dedupe([new Date('2012-02-02'), new Date('2012-02-02')]); // [new Date('2012-02-
|
|
|
194
194
|
dedupe(['hello', 'hello', 'world']); // ['hello', 'world']
|
|
195
195
|
```
|
|
196
196
|
|
|
197
|
-
- **join(val:Array, opts:object={delim:' ',trim:true,valtrim:true,valround:false})**
|
|
197
|
+
- **join(val:Array, opts:object={delim:' ',trim:true,valtrim:true,innertrim:true,valround:false})**
|
|
198
198
|
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.
|
|
199
199
|
|
|
200
|
-
```
|
|
200
|
+
```typescript
|
|
201
201
|
join(['Valkyrie', 'Studios']); // 'Valkyrie Studios'
|
|
202
202
|
join([5.1, ' years ', 'ago'], {valround: 0}); // '5 years ago'
|
|
203
203
|
join(['peter ', ' valkyrie '], {delim: '@'}); // 'peter@valkyrie'
|
|
204
204
|
join([user.first_name, user.last_name]); // 'John' (where user is {first_name: 'John', last_name: false})
|
|
205
205
|
join([' a', 1], {delim: '', valtrim: false, trim: false}); // ' a1'
|
|
206
|
+
join([' hello world ', 'this is peter '], {valtrim:true, innertrim: true, delim: ' '}); // 'hello world this is peter'
|
|
206
207
|
```
|
|
207
208
|
|
|
208
209
|
- **shuffle(val:Array)**
|
|
209
210
|
Shuffle an array (Fisher-Yates) in O(n), take note this changes the passed value
|
|
210
211
|
|
|
211
|
-
```
|
|
212
|
+
```typescript
|
|
212
213
|
const arr = [1, 2, 3, 4, 5, 6];
|
|
213
214
|
shuffle(arr);
|
|
214
215
|
// [4, 6, 3, 2, 5, 1]
|
|
215
216
|
```
|
|
216
217
|
|
|
217
218
|
- **sort(val:Array[object], by:string|Function, dir:Enum(asc,desc), options:Object)**
|
|
218
|
-
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)
|
|
219
|
+
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)
|
|
219
220
|
|
|
220
|
-
```
|
|
221
|
+
```typescript
|
|
221
222
|
const out = sort([
|
|
222
223
|
{test: 'Peter'},
|
|
223
224
|
{test: 'Jack'},
|
|
@@ -226,11 +227,11 @@ const out = sort([
|
|
|
226
227
|
{test: 'Joe'},
|
|
227
228
|
{test: 'Bob'},
|
|
228
229
|
{test: 'Alice'},
|
|
229
|
-
], 'test', 'desc');
|
|
230
|
+
], 'test', 'desc');
|
|
230
231
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'John'}, {test: 'Joe'}, {test: 'Jack'}, {test: 'Bob'}, {test: 'Alice'}]
|
|
231
232
|
```
|
|
232
233
|
|
|
233
|
-
```
|
|
234
|
+
```typescript
|
|
234
235
|
const out = sort([
|
|
235
236
|
{test: 'Peter'},
|
|
236
237
|
{test: 'Jack'},
|
|
@@ -239,13 +240,13 @@ const out = sort([
|
|
|
239
240
|
{test: 'Joe'},
|
|
240
241
|
{test: 'Bob'},
|
|
241
242
|
{test: 'Alice'},
|
|
242
|
-
], 'test', 'asc');
|
|
243
|
+
], 'test', 'asc');
|
|
243
244
|
// [{test: 'Alice'}, {test: 'Bob'}, {test: 'Jack'}, {test: 'Joe'}, {test: 'John'}, {test: 'Peter'}, {test: 'Pony'}]
|
|
244
245
|
```
|
|
245
246
|
|
|
246
247
|
allows passing a function to determine the key to sort by
|
|
247
248
|
|
|
248
|
-
```
|
|
249
|
+
```typescript
|
|
249
250
|
const out = sort([
|
|
250
251
|
{test: 'Peter'},
|
|
251
252
|
{test: 'Jack'},
|
|
@@ -254,13 +255,13 @@ const out = sort([
|
|
|
254
255
|
{test: 'Joe'},
|
|
255
256
|
{test: 'Bob'},
|
|
256
257
|
{test: 'Alice'},
|
|
257
|
-
], el => el.test.toLowerCase(), 'desc');
|
|
258
|
+
], el => el.test.toLowerCase(), 'desc');
|
|
258
259
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Joe'}, {test: 'Jack'}, {test: 'Bob'}, {test: 'Alice'}]
|
|
259
260
|
```
|
|
260
261
|
|
|
261
262
|
auto-cleans input to only contains non-empty objects
|
|
262
263
|
|
|
263
|
-
```
|
|
264
|
+
```typescript
|
|
264
265
|
const out = sort([
|
|
265
266
|
{test: 'Peter'},
|
|
266
267
|
{},
|
|
@@ -272,14 +273,14 @@ const out = sort([
|
|
|
272
273
|
{test: 'Bob'},
|
|
273
274
|
undefined,
|
|
274
275
|
{test: 'Alice'},
|
|
275
|
-
], el => el.test.toLowerCase(), 'desc');
|
|
276
|
+
], el => el.test.toLowerCase(), 'desc');
|
|
276
277
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Joe'}, {test: 'Jack'}, {test: 'Bob'}, {test: 'Alice'}]
|
|
277
278
|
```
|
|
278
279
|
|
|
279
280
|
allows passing custom filter function to clean input
|
|
280
281
|
Take note: Sort will still verify that the object is not an empty object, even when passing a custom filter function.
|
|
281
282
|
|
|
282
|
-
```
|
|
283
|
+
```typescript
|
|
283
284
|
const out = sort([
|
|
284
285
|
{test: 'Peter'},
|
|
285
286
|
{},
|
|
@@ -293,47 +294,68 @@ const out = sort([
|
|
|
293
294
|
{test: 'Bob'},
|
|
294
295
|
undefined,
|
|
295
296
|
{test: 'Alice'},
|
|
296
|
-
], el => el.test.toLowerCase(), 'desc', {filter_fn: el => isNotEmptyString(el.test)});
|
|
297
|
+
], el => el.test.toLowerCase(), 'desc', {filter_fn: el => isNotEmptyString(el.test)});
|
|
297
298
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Joe'}, {test: 'Jack'}, {test: 'Bob'}, {test: 'Alice'}]
|
|
298
299
|
```
|
|
299
300
|
|
|
300
301
|
allows passing custom options to position elements without a proper key (nokey_atend, defaults to true), or hide them (nokey_hide, defaults to false)
|
|
301
302
|
|
|
302
|
-
```
|
|
303
|
+
```typescript
|
|
303
304
|
const arr = [{test: 'Peter'}, {test: undefined}, {test: 'Jack'}, {test: 'Pony'}, {uid: 100}, {test: 'JOHn'}];
|
|
304
|
-
const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_atend: false});
|
|
305
|
+
const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_atend: false});
|
|
305
306
|
// [{test: undefined}, {uid: 100}, {test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Jack'}]
|
|
306
307
|
|
|
307
|
-
const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_atend: true});
|
|
308
|
+
const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_atend: true});
|
|
308
309
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Jack'}, {test: undefined}, {uid: 100}]
|
|
309
310
|
|
|
310
|
-
const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_hide: true});
|
|
311
|
+
const out = sort(arr, el => el.test.toLowerCase(), 'desc', {nokey_hide: true});
|
|
311
312
|
// [{test: 'Pony'}, {test: 'Peter'}, {test: 'JOHn'}, {test: 'Jack'}]
|
|
312
313
|
```
|
|
313
314
|
|
|
314
315
|
### boolean
|
|
315
316
|
- **isBoolean(val:any)**
|
|
316
317
|
Check if a variable is of type Boolean
|
|
317
|
-
```
|
|
318
|
+
```typescript
|
|
318
319
|
isBoolean(null); // FALSE
|
|
319
320
|
isBoolean(false); // TRUE
|
|
320
321
|
isBoolean(true); // TRUE
|
|
321
322
|
```
|
|
322
323
|
|
|
323
324
|
### caching
|
|
324
|
-
- **memoize(fn:Function, resolver:Function=false)**
|
|
325
|
-
memoize the output of a
|
|
325
|
+
- **memoize(fn:Function, resolver:Function=false, memoize_for:number|false)**
|
|
326
|
+
memoize the output of a function. An optional resolver function can be passed which allows custom cache key generation.
|
|
326
327
|
|
|
327
|
-
```
|
|
328
|
+
```typescript
|
|
328
329
|
const memoized_function = memoize((a) => {
|
|
329
330
|
return fnv1A(a);
|
|
330
331
|
});
|
|
331
332
|
```
|
|
332
333
|
|
|
334
|
+
Take Note: Also supports async functions and cache busting, eg:
|
|
335
|
+
```typescript
|
|
336
|
+
async function retrieveUser (userId:string) {
|
|
337
|
+
...
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/* Async but with no cache busting */
|
|
341
|
+
const memoized = memoize(retrieveUser);
|
|
342
|
+
await memoized('123456'); /* Original function will be called */
|
|
343
|
+
await memoized('123456'); /* Original function will not be called and memoized cache will be returned */
|
|
344
|
+
|
|
345
|
+
/* Async with cache busting after 5 seconds */
|
|
346
|
+
const memoized = memoize(retrieveUser, null, 5000);
|
|
347
|
+
await memoized('123456'); /* Original function will be called */
|
|
348
|
+
await memoized('123456'); /* Original function will not be called and memoized cache will be returned */
|
|
349
|
+
|
|
350
|
+
... (some time longer than 5 seconds passes)
|
|
351
|
+
|
|
352
|
+
await memoized('123456'); /* Original function will be called and re-cached */
|
|
353
|
+
```
|
|
354
|
+
|
|
333
355
|
### date
|
|
334
356
|
- **isDate(val:any)**
|
|
335
357
|
Check if a variable is of type Date
|
|
336
|
-
```
|
|
358
|
+
```typescript
|
|
337
359
|
isDate(new Date('December 17, 1995 03:24:00')); // TRUE
|
|
338
360
|
isDate('December 17, 1995 03:24:00'); // FALSE
|
|
339
361
|
```
|
|
@@ -342,7 +364,7 @@ isDate('December 17, 1995 03:24:00'); // FALSE
|
|
|
342
364
|
Take two incoming dates and return the difference between them in a certain unit. Possible key options(week,day,hour,minute,second,millisecond).
|
|
343
365
|
|
|
344
366
|
Note: Does not touch the passed date objects, if no key is passed will default to millisecond
|
|
345
|
-
```
|
|
367
|
+
```typescript
|
|
346
368
|
diff(new Date("2022-10-05T13:12:11+02:00"), new Date("2022-11-05T13:12:11+06:00"), 'week'); // -4.404761904761905
|
|
347
369
|
diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:12:11+02:00"), 'day'); // 30.83333333333333332
|
|
348
370
|
diff(new Date("2022-11-05T13:12:11+06:00"), new Date("2022-10-05T13:12:11+02:00"), 'hour'); // 740
|
|
@@ -383,7 +405,7 @@ Available tokens for usage in spec:
|
|
|
383
405
|
| `A` | Uppercase AM/PM | AM ... PM |
|
|
384
406
|
| `a` | Lowercase AM/PM | am ... pm |
|
|
385
407
|
|
|
386
|
-
```
|
|
408
|
+
```typescript
|
|
387
409
|
format(new Date('2023-01-10T14:30:00Z'), '[Today is] dddd, MMMM D, YYYY [at] h:mm A', 'en', 'Europe/Brussels');
|
|
388
410
|
// 'Today is Tuesday, January 10, 2023 at 2:30 PM'
|
|
389
411
|
|
|
@@ -413,7 +435,7 @@ Returns the current unix timestamp in milliseconds
|
|
|
413
435
|
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).
|
|
414
436
|
|
|
415
437
|
Note: Does not touch the date object passed
|
|
416
|
-
```
|
|
438
|
+
```typescript
|
|
417
439
|
startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'year'); // new Date("2023-01-01T00:00:00.000Z")
|
|
418
440
|
startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'quarter'); // new Date("2023-04-01T00:00:00.000Z")
|
|
419
441
|
startOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'month'); // new Date("2023-05-01T00:00:00.000Z")
|
|
@@ -433,7 +455,7 @@ startOfUTC(new Date("2023-05-04T12:04:27.043+02:00"), 'second'); // new Date("20
|
|
|
433
455
|
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).
|
|
434
456
|
|
|
435
457
|
Note: Does not touch the date object passed
|
|
436
|
-
```
|
|
458
|
+
```typescript
|
|
437
459
|
endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'year'); // new Date("2023-12-31T23:59:59.999Z")
|
|
438
460
|
endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'quarter'); // new Date("2023-06-30T23:59:59.999Z")
|
|
439
461
|
endOfUTC(new Date("2023-05-04T12:04:27+02:00"), 'month'); // new Date("2023-05-31T23:59:59.999Z")
|
|
@@ -457,7 +479,7 @@ endOfUTC(new Date("2023-05-04T12:04:27.043+02:00"), 'second'); // new Date("2023
|
|
|
457
479
|
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).
|
|
458
480
|
|
|
459
481
|
Note: Does not touch the date object passed
|
|
460
|
-
```
|
|
482
|
+
```typescript
|
|
461
483
|
addUTC(new Date("2022-10-05T13:12:11+02:00"), 10, 'year'); // new Date("2032-10-05T11:12:11.000Z")
|
|
462
484
|
addUTC(new Date("2022-10-05T13:12:11+02:00"), -10, 'year'); // new Date("2012-10-05T11:12:11.000Z")
|
|
463
485
|
addUTC(new Date("2022-10-05T13:12:11+02:00"), 10, 'month'); // new Date("2023-08-05T11:12:11.000Z")
|
|
@@ -479,7 +501,7 @@ addUTC(new Date("2022-10-05T13:12:11+02:00"), -10, 'second'); // new Date("2022-
|
|
|
479
501
|
### deep
|
|
480
502
|
- **deepFreeze(val:Object)**
|
|
481
503
|
Recursively freezes all properties of an object
|
|
482
|
-
```
|
|
504
|
+
```typescript
|
|
483
505
|
const myObj = deepFreeze({
|
|
484
506
|
a: 2,
|
|
485
507
|
b: {
|
|
@@ -496,7 +518,7 @@ Object.isFrozen(myObj.b.d); // TRUE
|
|
|
496
518
|
|
|
497
519
|
- **deepSeal(val:Object)**
|
|
498
520
|
Recursively freezes all properties of an object
|
|
499
|
-
```
|
|
521
|
+
```typescript
|
|
500
522
|
const myObj = deepSeal({
|
|
501
523
|
a: 2,
|
|
502
524
|
b: {
|
|
@@ -514,7 +536,7 @@ Object.isFrozen(myObj.b.d); // FALSE
|
|
|
514
536
|
|
|
515
537
|
- **deepSet(obj:Object, path:string, value:any=null, define:boolean=false)**
|
|
516
538
|
Sets a property and its value deep in the structure of an object
|
|
517
|
-
```
|
|
539
|
+
```typescript
|
|
518
540
|
const myObj = {
|
|
519
541
|
a: 2,
|
|
520
542
|
};
|
|
@@ -522,7 +544,7 @@ deepSet(myObj, 'b.c.d.e', 4);
|
|
|
522
544
|
myObj.b.c.d.e; // 4
|
|
523
545
|
```
|
|
524
546
|
|
|
525
|
-
```
|
|
547
|
+
```typescript
|
|
526
548
|
const myObj = {
|
|
527
549
|
a: 2,
|
|
528
550
|
b: [
|
|
@@ -536,7 +558,7 @@ myObj.b[0].price; // 100
|
|
|
536
558
|
myObj.b[1].price; // 500
|
|
537
559
|
```
|
|
538
560
|
|
|
539
|
-
```
|
|
561
|
+
```typescript
|
|
540
562
|
const myObj = {
|
|
541
563
|
a: 2,
|
|
542
564
|
};
|
|
@@ -546,7 +568,7 @@ myObj.b.c; // Function
|
|
|
546
568
|
|
|
547
569
|
- **deepGet(obj:Object, path:string, get_parent:boolean=false)**
|
|
548
570
|
Retrieves a value based on a path in a deeply nested object
|
|
549
|
-
```
|
|
571
|
+
```typescript
|
|
550
572
|
const myObj = {
|
|
551
573
|
a: 2,
|
|
552
574
|
b: [
|
|
@@ -557,7 +579,7 @@ const myObj = {
|
|
|
557
579
|
deepGet(myObj, 'b[0].price', true); // [{price: 2}, {price: 4}]
|
|
558
580
|
```
|
|
559
581
|
|
|
560
|
-
```
|
|
582
|
+
```typescript
|
|
561
583
|
const myObj = {
|
|
562
584
|
a: 2,
|
|
563
585
|
b: [
|
|
@@ -571,7 +593,7 @@ deepGet(myObj, 'b[0].price'); // 2
|
|
|
571
593
|
### equal
|
|
572
594
|
- **equal(a:any, b:any)**
|
|
573
595
|
Check if a variable is equal to another one
|
|
574
|
-
```
|
|
596
|
+
```typescript
|
|
575
597
|
equal(5, 6); // FALSE
|
|
576
598
|
equal(1, 1); // TRUE
|
|
577
599
|
equal([0, 1, 2], [1, 2]); // FALSE
|
|
@@ -604,7 +626,7 @@ An empty function that returns a promise that will resolve after X milliseconds,
|
|
|
604
626
|
### formdata
|
|
605
627
|
- **isFormData(val:any)**
|
|
606
628
|
Check if a variable is of type FormData
|
|
607
|
-
```
|
|
629
|
+
```typescript
|
|
608
630
|
isFormData(new FormData()); // TRUE
|
|
609
631
|
isFormData({hi: 'there'}); // FALSE
|
|
610
632
|
```
|
|
@@ -612,13 +634,13 @@ isFormData({hi: 'there'}); // FALSE
|
|
|
612
634
|
### hash
|
|
613
635
|
- **guid()**
|
|
614
636
|
Generate a unique identifier (guid) according to RFC4122
|
|
615
|
-
```
|
|
637
|
+
```typescript
|
|
616
638
|
guid(); // 245caf1a-86af-11e7-bb31-be2e44b06b34
|
|
617
639
|
```
|
|
618
640
|
|
|
619
641
|
- **fnv1A(val:any)**
|
|
620
642
|
Generate a fnv1A hash from an object, using a 32-bit prime/offset
|
|
621
|
-
```
|
|
643
|
+
```typescript
|
|
622
644
|
fnv1A('hello world'); // -2023343616
|
|
623
645
|
fnv1A({a:1,b:2}); // 361168128
|
|
624
646
|
fnv1A(4); // 1630425728
|
|
@@ -629,7 +651,7 @@ fnv1A(new Date('2012-02-02')); // 1655579136
|
|
|
629
651
|
### number
|
|
630
652
|
- **isNumber(val:any)**
|
|
631
653
|
Check if a variable is a number
|
|
632
|
-
```
|
|
654
|
+
```typescript
|
|
633
655
|
isNumber('foo'); // FALSE
|
|
634
656
|
isNumber(4); // TRUE
|
|
635
657
|
isNumber(0.5); // TRUE
|
|
@@ -637,7 +659,7 @@ isNumber(0.5); // TRUE
|
|
|
637
659
|
|
|
638
660
|
- **isNumberAbove(val:number, comp:number)**
|
|
639
661
|
Check if a variable is a number above a certain bound
|
|
640
|
-
```
|
|
662
|
+
```typescript
|
|
641
663
|
isNumberAbove(5, 0); // TRUE
|
|
642
664
|
isNumberAbove(.1, 0); // TRUE
|
|
643
665
|
isNumberAbove(-1, -1); // FALSE
|
|
@@ -646,7 +668,7 @@ isNumberAbove(-10, -9); // FALSE
|
|
|
646
668
|
|
|
647
669
|
- **isNumberAboveOrEqual(val:number, comp:number)**
|
|
648
670
|
Check if a variable is a number above or equal to a certain bound
|
|
649
|
-
```
|
|
671
|
+
```typescript
|
|
650
672
|
isNumberAboveOrEqual(5, 0); // TRUE
|
|
651
673
|
isNumberAboveOrEqual(.1, 0); // TRUE
|
|
652
674
|
isNumberAboveOrEqual(-1, -1); // TRUE
|
|
@@ -655,7 +677,7 @@ isNumberAboveOrEqual(-10, -9); // FALSE
|
|
|
655
677
|
|
|
656
678
|
- **isNumberBelow(val:number, comp:number)**
|
|
657
679
|
Check if a variable is a number below a certain bound
|
|
658
|
-
```
|
|
680
|
+
```typescript
|
|
659
681
|
isNumberBelow(0, 5); // TRUE
|
|
660
682
|
isNumberBelow(0, .1); // TRUE
|
|
661
683
|
isNumberBelow(-1, -1); // FALSE
|
|
@@ -664,7 +686,7 @@ isNumberBelow(-9, -10); // FALSE
|
|
|
664
686
|
|
|
665
687
|
- **isNumberBelowOrEqual(val:number, comp:number)**
|
|
666
688
|
Check if a variable is a number below or equal a certain bound
|
|
667
|
-
```
|
|
689
|
+
```typescript
|
|
668
690
|
isNumberBelowOrEqual(0, 5); // TRUE
|
|
669
691
|
isNumberBelowOrEqual(0, .1); // TRUE
|
|
670
692
|
isNumberBelowOrEqual(-1, -1); // TRUE
|
|
@@ -673,7 +695,7 @@ isNumberBelowOrEqual(-9, -10); // FALSE
|
|
|
673
695
|
|
|
674
696
|
- **isNumberBetween(val:number, min:number, max:number)**
|
|
675
697
|
Check if a variable is a number between a range of numbers
|
|
676
|
-
```
|
|
698
|
+
```typescript
|
|
677
699
|
isNumberBetween(5, 0, 10); // TRUE
|
|
678
700
|
isNumberBetween(.1, 0, 1); // TRUE
|
|
679
701
|
isNumberBetween(-.1, -1, 0); // TRUE
|
|
@@ -683,7 +705,7 @@ isNumberBetween(-1, 0, 1); // FALSE
|
|
|
683
705
|
|
|
684
706
|
- **isInteger(val:any)**
|
|
685
707
|
Check if a variable is an integer
|
|
686
|
-
```
|
|
708
|
+
```typescript
|
|
687
709
|
isInteger('foo'); // FALSE
|
|
688
710
|
isInteger(4); // TRUE
|
|
689
711
|
isInteger(0.5); // FALSE
|
|
@@ -691,7 +713,7 @@ isInteger(0.5); // FALSE
|
|
|
691
713
|
|
|
692
714
|
- **isIntegerAbove(val:number, comp:number)**
|
|
693
715
|
Check if a variable is an integer above a certain bound
|
|
694
|
-
```
|
|
716
|
+
```typescript
|
|
695
717
|
isIntegerAbove(5, 0); // TRUE
|
|
696
718
|
isIntegerAbove(.1, 0); // FALSE
|
|
697
719
|
isIntegerAbove(-1, -1); // FALSE
|
|
@@ -700,7 +722,7 @@ isIntegerAbove(-10, -9); // FALSE
|
|
|
700
722
|
|
|
701
723
|
- **isIntegerAboveOrEqual(val:number, comp:number)**
|
|
702
724
|
Check if a variable is an integer above or equal to a certain bound
|
|
703
|
-
```
|
|
725
|
+
```typescript
|
|
704
726
|
isIntegerAboveOrEqual(5, 0); // TRUE
|
|
705
727
|
isIntegerAboveOrEqual(.1, 0); // FALSE
|
|
706
728
|
isIntegerAboveOrEqual(-1, -1); // TRUE
|
|
@@ -709,7 +731,7 @@ isIntegerAboveOrEqual(-10, -9); // FALSE
|
|
|
709
731
|
|
|
710
732
|
- **isIntegerBelow(val:number, comp:number)**
|
|
711
733
|
Check if a variable is an integer below a certain bound
|
|
712
|
-
```
|
|
734
|
+
```typescript
|
|
713
735
|
isIntegerBelow(0, 5); // TRUE
|
|
714
736
|
isIntegerBelow(0, .1); // TRUE
|
|
715
737
|
isIntegerBelow(.4, 5); // FALSE
|
|
@@ -719,7 +741,7 @@ isIntegerBelow(-9, -10); // FALSE
|
|
|
719
741
|
|
|
720
742
|
- **isIntegerBelowOrEqual(val:number, comp:number)**
|
|
721
743
|
Check if a variable is an integer below or equal to a certain bound
|
|
722
|
-
```
|
|
744
|
+
```typescript
|
|
723
745
|
isIntegerBelowOrEqual(0, 5); // TRUE
|
|
724
746
|
isIntegerBelowOrEqual(0, .1); // TRUE
|
|
725
747
|
isIntegerBelowOrEqual(.4, 5); // FALSE
|
|
@@ -729,7 +751,7 @@ isIntegerBelowOrEqual(-9, -10); // FALSE
|
|
|
729
751
|
|
|
730
752
|
- **isIntegerBetween(val:number, min:number, max:number)**
|
|
731
753
|
Check if a variable is an integer between a range of numbers
|
|
732
|
-
```
|
|
754
|
+
```typescript
|
|
733
755
|
isIntegerBetween(5, 0, 10); // TRUE
|
|
734
756
|
isIntegerBetween(.1, 0, 1); // FALSE
|
|
735
757
|
isIntegerBetween(-.1, -1, 0); // FALSE
|
|
@@ -739,14 +761,14 @@ isIntegerBetween(-1, 0, 1); // FALSE
|
|
|
739
761
|
|
|
740
762
|
- **isNumericalNaN(val:any)**
|
|
741
763
|
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)
|
|
742
|
-
```
|
|
764
|
+
```typescript
|
|
743
765
|
isNumericalNaN('foo'); // FALSE
|
|
744
766
|
isNumericalNaN(NaN); // TRUE
|
|
745
767
|
```
|
|
746
768
|
|
|
747
769
|
- **toPercentage(val:Number,precision:Number=0,min:Number=0,max:Number=1)**
|
|
748
770
|
Calculate the percentage of a specific value in a range
|
|
749
|
-
```
|
|
771
|
+
```typescript
|
|
750
772
|
toPercentage(0.50106579, 5); // 50.11658
|
|
751
773
|
toPercentage(-356, 0, -1000, 1000); // 32
|
|
752
774
|
toPercentage(0.5); // 50
|
|
@@ -754,7 +776,7 @@ toPercentage(0.5); // 50
|
|
|
754
776
|
|
|
755
777
|
- **round(val:Number,precision:Number=0)**
|
|
756
778
|
Round a numeric value to a specific amount of decimals
|
|
757
|
-
```
|
|
779
|
+
```typescript
|
|
758
780
|
round(5.123456789, 0); // 5
|
|
759
781
|
round(5.123456789, 2); // 5.12
|
|
760
782
|
round(5.123456789, 5); // 5.12346
|
|
@@ -762,14 +784,14 @@ round(5.123456789, 5); // 5.12346
|
|
|
762
784
|
|
|
763
785
|
- **randomBetween(min:Number=0,max:Number=10)**
|
|
764
786
|
Generate a random numeric value between a min and max range
|
|
765
|
-
```
|
|
787
|
+
```typescript
|
|
766
788
|
randomBetween(); // Will generate a random between 0 and 10
|
|
767
789
|
randomBetween(25, 100); // Will generate a random between 25 and 100
|
|
768
790
|
```
|
|
769
791
|
|
|
770
792
|
- **randomIntBetween(min:Number=0,max:Number=10)**
|
|
771
793
|
Generate a random numeric value between a min and max range (max not inclusive)
|
|
772
|
-
```
|
|
794
|
+
```typescript
|
|
773
795
|
randomIntBetween(); // Will generate a random between 0 and 10 (10 not inclusive)
|
|
774
796
|
randomIntBetween(25, 100); // Will generate a random between 25 and 100 (100 not inclusive)
|
|
775
797
|
```
|
|
@@ -777,14 +799,14 @@ randomIntBetween(25, 100); // Will generate a random between 25 and 100 (100 not
|
|
|
777
799
|
### object
|
|
778
800
|
- **isObject(val:any)**
|
|
779
801
|
Check if a variable is of type Object
|
|
780
|
-
```
|
|
802
|
+
```typescript
|
|
781
803
|
isObject({a: 1}); // TRUE
|
|
782
804
|
isObject(1); // FALSE
|
|
783
805
|
```
|
|
784
806
|
|
|
785
807
|
- **isNotEmptyObject(val:any)**
|
|
786
808
|
Check if a variable a non-empty object
|
|
787
|
-
```
|
|
809
|
+
```typescript
|
|
788
810
|
isNotEmptyObject({a:1}); // TRUE
|
|
789
811
|
isNotEmptyObject({}); // FALSE
|
|
790
812
|
isNotEmptyObject('Hi'); // FALSE
|
|
@@ -793,21 +815,21 @@ isNotEmptyObject('Hi'); // FALSE
|
|
|
793
815
|
- **pick(obj:Object={}, keys:Array[string]=[])**
|
|
794
816
|
Copies the keys passed in the 'keys' array from the passed object to a new object and returns that object.**
|
|
795
817
|
<small>If a key wasn't found it will be set as undefined</small>
|
|
796
|
-
```
|
|
818
|
+
```typescript
|
|
797
819
|
pick({a: 1, b: 2, c: 3}, ['a','b']); // {a: 1, b: 2}
|
|
798
820
|
```
|
|
799
821
|
|
|
800
822
|
- **merge(target:Object={},obj:Object={})**
|
|
801
823
|
Merges two objects together, with the preference over the second object.
|
|
802
|
-
```
|
|
824
|
+
```typescript
|
|
803
825
|
merge({a: 1, b: false}, {a: 900, c: 50}); // {a: 900, b: false, c: 50}
|
|
804
826
|
```
|
|
805
827
|
|
|
806
828
|
- **define(props:Object, obj:Object={})**
|
|
807
829
|
Creates an object with the passed accessors set on it
|
|
808
|
-
```
|
|
830
|
+
```typescript
|
|
809
831
|
define(
|
|
810
|
-
{
|
|
832
|
+
{
|
|
811
833
|
a: {
|
|
812
834
|
enumerable: false,
|
|
813
835
|
value : function () { ... }
|
|
@@ -818,7 +840,7 @@ define(
|
|
|
818
840
|
// { a : () => ..., b: 2 }
|
|
819
841
|
```
|
|
820
842
|
|
|
821
|
-
```
|
|
843
|
+
```typescript
|
|
822
844
|
define({
|
|
823
845
|
a : {
|
|
824
846
|
enumerable: false,
|
|
@@ -830,7 +852,7 @@ define({
|
|
|
830
852
|
### regexp
|
|
831
853
|
- **isRegExp(val:any)**
|
|
832
854
|
Check if a variable is an instance of RegExp
|
|
833
|
-
```
|
|
855
|
+
```typescript
|
|
834
856
|
isRegExp('foo'); // FALSE
|
|
835
857
|
isRegExp(new RegExp('ab+c', 'i')); // TRUE
|
|
836
858
|
isRegExp(new RegExp(/ab+c/, 'i')); // TRUE
|
|
@@ -839,21 +861,21 @@ isRegExp(/ab+c/i); // FALSE
|
|
|
839
861
|
|
|
840
862
|
- **sanitize(val:string)**
|
|
841
863
|
Escapes special characters in a string and returns a sanitized version safe for usage in RegExp instances
|
|
842
|
-
```
|
|
864
|
+
```typescript
|
|
843
865
|
sanitizeRegExp('contact@valkyriestudios.be'); // contact@valkyriestudios\\.be
|
|
844
866
|
```
|
|
845
867
|
|
|
846
868
|
### string
|
|
847
869
|
- **isString(val:any)**
|
|
848
870
|
Check if a variable is a string
|
|
849
|
-
```
|
|
871
|
+
```typescript
|
|
850
872
|
isString('foo'); // TRUE
|
|
851
873
|
isString(4); // FALSE
|
|
852
874
|
```
|
|
853
875
|
|
|
854
876
|
- **isStringBetween(val:string, min:number, max:number, trimmed:boolean=true)**
|
|
855
877
|
Check if a variable is between a range of numbers
|
|
856
|
-
```
|
|
878
|
+
```typescript
|
|
857
879
|
isStringBetween('Peter', 4, 10); // TRUE
|
|
858
880
|
isStringBetween('Jeff', 4, 10); // TRUE
|
|
859
881
|
isStringBetween('Moe', 4, 10); // FALSE
|
|
@@ -864,7 +886,7 @@ isStringBetween(' Joe', 1, 3, false); // FALSE
|
|
|
864
886
|
|
|
865
887
|
- **isNotEmptyString(val:any, trimmed:boolean=true)**
|
|
866
888
|
Check if a variable a non-empty string
|
|
867
|
-
```
|
|
889
|
+
```typescript
|
|
868
890
|
isNotEmptyString({a:1}); // FALSE
|
|
869
891
|
isNotEmptyString(''); // FALSE
|
|
870
892
|
isNotEmptyString(' '); // FALSE
|
|
@@ -874,7 +896,7 @@ isNotEmptyString('Hi'); // TRUE
|
|
|
874
896
|
|
|
875
897
|
- **shorten(val:any, length:integer, postfix:string=...)**
|
|
876
898
|
Shorten a string and add a postfix if string went over length
|
|
877
|
-
```
|
|
899
|
+
```typescript
|
|
878
900
|
shorten('To the moon and beyond', 11, '..'); // 'To the moon..'
|
|
879
901
|
shorten('Hi', 250); // 'Hi'
|
|
880
902
|
shorten('To the moon and beyond'); // 'To the moon...'
|
|
@@ -887,7 +909,7 @@ Humanize an amount of bytes
|
|
|
887
909
|
-- option:separator (default:'.'): Override the separator used for floats, eg: '20.034' -> '20,034'
|
|
888
910
|
-- option:precision (default:2): Override decimal precision for floats: eg: '20.0344233' with precision 2 -> '20.03'
|
|
889
911
|
-- 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'`
|
|
890
|
-
```
|
|
912
|
+
```typescript
|
|
891
913
|
humanizeBytes(1504230); // '1.4 MB'
|
|
892
914
|
humanizeBytes(23); // '23 bytes'
|
|
893
915
|
humanizeBytes(-374237489237); // '-348.5 GB'
|
|
@@ -903,7 +925,7 @@ Humanize a number
|
|
|
903
925
|
-- option:real (default:false): Set to true to automatically round input numbers
|
|
904
926
|
-- option:divider (default:1000): Override default divider used for units (used internally for humanizeBytes with 1024 as divider)
|
|
905
927
|
|
|
906
|
-
```
|
|
928
|
+
```typescript
|
|
907
929
|
humanizeNumber(4327963279469432); // '4.33q'
|
|
908
930
|
humanizeNumber(1504230); // '1.5m'
|
|
909
931
|
humanizeNumber(-432443); // '-432.44k'
|
package/array/dedupe.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.dedupe = dedupe;
|
|
4
|
+
exports.default = dedupe;
|
|
4
5
|
const fnv1A_1 = require("../hash/fnv1A");
|
|
5
6
|
function dedupe(val) {
|
|
6
7
|
if (!Array.isArray(val))
|
|
7
8
|
return [];
|
|
8
9
|
const set = new Set();
|
|
9
10
|
const acc = [];
|
|
10
|
-
for (
|
|
11
|
-
const
|
|
11
|
+
for (let i = 0; i < val.length; i++) {
|
|
12
|
+
const el = val[i];
|
|
13
|
+
const hash = (0, fnv1A_1.fnv1A)(el);
|
|
12
14
|
if (set.has(hash))
|
|
13
15
|
continue;
|
|
14
16
|
set.add(hash);
|
|
15
|
-
acc.push(
|
|
17
|
+
acc.push(el);
|
|
16
18
|
}
|
|
17
19
|
return acc;
|
|
18
20
|
}
|
|
19
|
-
exports.dedupe = dedupe;
|
|
20
|
-
exports.default = dedupe;
|