@valkyriestudios/utils 12.0.0 → 12.2.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/array/dedupe.js +4 -10
- package/array/is.d.ts +1 -1
- package/array/isNotEmpty.d.ts +1 -1
- package/array/join.d.ts +2 -2
- package/array/join.js +18 -10
- package/array/mapFn.d.ts +9 -14
- package/array/mapFn.js +7 -11
- package/array/mapKey.d.ts +6 -12
- package/array/mapKey.js +7 -11
- package/array/mapPrimitive.d.ts +4 -6
- package/array/mapPrimitive.js +21 -12
- package/array/shuffle.js +1 -1
- package/array/sort.js +18 -14
- package/boolean/is.d.ts +1 -1
- package/date/format.js +1 -1
- package/date/is.d.ts +1 -1
- package/deep/set.js +7 -5
- package/function/is.d.ts +1 -1
- package/function/isAsync.d.ts +1 -1
- package/hash/guid.js +18 -18
- package/index.d.ts +39 -48
- package/number/is.d.ts +1 -1
- package/number/isAbove.d.ts +1 -1
- package/number/isAboveOrEqual.d.ts +1 -1
- package/number/isBelow.d.ts +1 -1
- package/number/isBelowOrEqual.d.ts +1 -1
- package/number/isBetween.d.ts +1 -1
- package/number/isInteger.d.ts +1 -1
- package/number/isIntegerAbove.d.ts +1 -1
- package/number/isIntegerAboveOrEqual.d.ts +1 -1
- package/number/isIntegerBelow.d.ts +1 -1
- package/number/isIntegerBelowOrEqual.d.ts +1 -1
- package/number/isIntegerBetween.d.ts +1 -1
- package/object/is.d.ts +3 -1
- package/object/isNotEmpty.d.ts +3 -1
- package/object/pick.js +2 -1
- package/package.json +1 -1
- package/regexp/is.d.ts +1 -1
- package/string/humanizeNumber.d.ts +1 -1
- package/string/humanizeNumber.js +40 -41
- package/string/is.d.ts +1 -1
- package/string/isBetween.d.ts +1 -1
- package/string/isNotEmpty.d.ts +1 -1
- package/string/shorten.js +2 -2
package/array/dedupe.js
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const fnv1A_1 = require("../hash/fnv1A");
|
|
4
3
|
function dedupe(val) {
|
|
5
|
-
if (!Array.isArray(val)
|
|
4
|
+
if (!Array.isArray(val))
|
|
6
5
|
return [];
|
|
7
6
|
const set = new Set();
|
|
8
|
-
const acc = [];
|
|
9
|
-
let hash;
|
|
10
7
|
for (const item of val) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
continue;
|
|
14
|
-
set.add(hash);
|
|
15
|
-
acc.push(item);
|
|
8
|
+
if (!set.has(item))
|
|
9
|
+
set.add(item);
|
|
16
10
|
}
|
|
17
|
-
return
|
|
11
|
+
return [...set];
|
|
18
12
|
}
|
|
19
13
|
exports.default = dedupe;
|
package/array/is.d.ts
CHANGED
package/array/isNotEmpty.d.ts
CHANGED
package/array/join.d.ts
CHANGED
|
@@ -27,8 +27,8 @@ interface joinOptions {
|
|
|
27
27
|
/**
|
|
28
28
|
* Join an array of values while autofiltering any non-string/non-number elements
|
|
29
29
|
*
|
|
30
|
-
* @param val - Array of values to join
|
|
31
|
-
* @param opts - Join options
|
|
30
|
+
* @param {unknown[]} val - Array of values to join
|
|
31
|
+
* @param {joinOptions?} opts - Join options
|
|
32
32
|
*
|
|
33
33
|
* @returns Joined array as string
|
|
34
34
|
*/
|
package/array/join.js
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const round_1 = require("../number/round");
|
|
4
|
-
function join(val, opts
|
|
4
|
+
function join(val, opts) {
|
|
5
5
|
if (!Array.isArray(val) || !val.length)
|
|
6
6
|
return '';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
let DELIM = ' ';
|
|
8
|
+
let TRIM = true;
|
|
9
|
+
let VALTRIM = true;
|
|
10
|
+
let VALROUND = false;
|
|
11
|
+
if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
|
|
12
|
+
if (typeof opts.delim === 'string')
|
|
13
|
+
DELIM = opts.delim;
|
|
14
|
+
if (opts.trim === false)
|
|
15
|
+
TRIM = opts.trim;
|
|
16
|
+
if (opts.valtrim === false)
|
|
17
|
+
VALTRIM = opts.valtrim;
|
|
18
|
+
if (Number.isInteger(opts.valround) && opts.valround >= 0)
|
|
19
|
+
VALROUND = opts.valround;
|
|
20
|
+
}
|
|
13
21
|
const filtered = [];
|
|
14
22
|
for (const el of val) {
|
|
15
23
|
if (typeof el === 'string' && el.trim().length) {
|
|
16
|
-
filtered.push(
|
|
24
|
+
filtered.push(VALTRIM ? el.trim() : el);
|
|
17
25
|
}
|
|
18
26
|
else if (Number.isFinite(el)) {
|
|
19
|
-
filtered.push(
|
|
27
|
+
filtered.push(VALROUND !== false ? (0, round_1.default)(el, VALROUND) : el);
|
|
20
28
|
}
|
|
21
29
|
}
|
|
22
|
-
return
|
|
30
|
+
return TRIM ? filtered.join(DELIM).trim() : filtered.join(DELIM);
|
|
23
31
|
}
|
|
24
32
|
exports.default = join;
|
package/array/mapFn.d.ts
CHANGED
|
@@ -12,11 +12,10 @@ interface mapOptions {
|
|
|
12
12
|
*/
|
|
13
13
|
merge?: boolean;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
[key: string]:
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
15
|
+
type mapFn = (entry: {
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
}) => (string | number | boolean);
|
|
18
|
+
type mapReturn = Record<string, Record<string, any>>;
|
|
20
19
|
/**
|
|
21
20
|
* Map an object array into a kv-object through a function that generates a key. Returning a non-string,
|
|
22
21
|
* non-numeric value from the function (eg: false) will filter out the object.
|
|
@@ -26,15 +25,11 @@ interface kvMap {
|
|
|
26
25
|
* Output:
|
|
27
26
|
* {12: {uid: 12, name: 'Peter'}, 15: {uid: 15, name: 'Jonas'}}
|
|
28
27
|
*
|
|
29
|
-
* @param val - Array to map
|
|
30
|
-
* @param fn - Handler function which is run for each of the objects and should return a string or number
|
|
31
|
-
* @param opts - Options object to override built-in defaults
|
|
28
|
+
* @param {Record<string, any>[]} val - Array to map
|
|
29
|
+
* @param {mapFn} fn - Handler function which is run for each of the objects and should return a string or number
|
|
30
|
+
* @param {mapOptions?} opts - Options object to override built-in defaults
|
|
32
31
|
*
|
|
33
|
-
* @returns KV-Map object
|
|
32
|
+
* @returns {mapReturn} KV-Map object
|
|
34
33
|
*/
|
|
35
|
-
export default function mapFn(arr:
|
|
36
|
-
[key: string]: any;
|
|
37
|
-
}[], fn: (entry: {
|
|
38
|
-
[key: string]: any;
|
|
39
|
-
}) => (string | number | boolean), opts?: mapOptions): kvMap;
|
|
34
|
+
export default function mapFn(arr: Record<string, any>[], fn: mapFn, opts?: mapOptions): mapReturn;
|
|
40
35
|
export {};
|
package/array/mapFn.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function mapFn(arr, fn, opts
|
|
3
|
+
function mapFn(arr, fn, opts) {
|
|
4
4
|
if ((!Array.isArray(arr) || !arr.length) ||
|
|
5
5
|
typeof fn !== 'function')
|
|
6
6
|
return {};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
let MERGE = false;
|
|
8
|
+
if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
|
|
9
|
+
if (opts.merge === true)
|
|
10
|
+
MERGE = true;
|
|
11
|
+
}
|
|
11
12
|
const map = {};
|
|
12
13
|
let hash = false;
|
|
13
14
|
for (const el of arr) {
|
|
@@ -17,12 +18,7 @@ function mapFn(arr, fn, opts = {}) {
|
|
|
17
18
|
if (!Number.isFinite(hash) && !(typeof hash === 'string' && hash.trim().length))
|
|
18
19
|
continue;
|
|
19
20
|
hash = `${hash}`;
|
|
20
|
-
|
|
21
|
-
map[hash] = { ...map[hash], ...el };
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
map[hash] = el;
|
|
25
|
-
}
|
|
21
|
+
map[hash] = MERGE && map.hasOwnProperty(hash) ? { ...map[hash], ...el } : el;
|
|
26
22
|
}
|
|
27
23
|
return map;
|
|
28
24
|
}
|
package/array/mapKey.d.ts
CHANGED
|
@@ -12,11 +12,7 @@ interface mapOptions {
|
|
|
12
12
|
*/
|
|
13
13
|
merge?: boolean;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
[key: string]: {
|
|
17
|
-
[key: string]: any;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
15
|
+
type kvMap = Record<string, Record<string, any>>;
|
|
20
16
|
/**
|
|
21
17
|
* Map an object array into a kv-object by passing a common key that exists on the objects. Objects for
|
|
22
18
|
* which the key doesn't exist will be filtered out automatically
|
|
@@ -26,13 +22,11 @@ interface kvMap {
|
|
|
26
22
|
* Output:
|
|
27
23
|
* {12: {uid: 12, name: 'Peter'}, 15: {uid: 15, name: 'Jonas'}}
|
|
28
24
|
*
|
|
29
|
-
* @param val - Array to map
|
|
30
|
-
* @param key - Key to map by
|
|
31
|
-
* @param opts - Options object to override built-in defaults
|
|
25
|
+
* @param {Record<string,any>[]} val - Array to map
|
|
26
|
+
* @param {string} key - Key to map by
|
|
27
|
+
* @param {mapOptions?} opts - Options object to override built-in defaults
|
|
32
28
|
*
|
|
33
|
-
* @returns KV-Map object
|
|
29
|
+
* @returns {kvMap} KV-Map object
|
|
34
30
|
*/
|
|
35
|
-
export default function mapKey(arr:
|
|
36
|
-
[key: string]: any;
|
|
37
|
-
}[], key: string, opts?: mapOptions): kvMap;
|
|
31
|
+
export default function mapKey(arr: Record<string, any>[], key: string, opts?: mapOptions): kvMap;
|
|
38
32
|
export {};
|
package/array/mapKey.js
CHANGED
|
@@ -1,27 +1,23 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function mapKey(arr, key, opts
|
|
3
|
+
function mapKey(arr, key, opts) {
|
|
4
4
|
if ((!Array.isArray(arr) || !arr.length) ||
|
|
5
5
|
typeof key !== 'string')
|
|
6
6
|
return {};
|
|
7
7
|
const key_s = key.trim();
|
|
8
8
|
if (!key_s.length)
|
|
9
9
|
return {};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
let MERGE = false;
|
|
11
|
+
if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
|
|
12
|
+
if (opts.merge === true)
|
|
13
|
+
MERGE = true;
|
|
14
|
+
}
|
|
14
15
|
const map = {};
|
|
15
16
|
for (const el of arr) {
|
|
16
17
|
if (Object.prototype.toString.call(el) !== '[object Object]' ||
|
|
17
18
|
!Object.prototype.hasOwnProperty.call(el, key_s))
|
|
18
19
|
continue;
|
|
19
|
-
|
|
20
|
-
map[el[key_s]] = { ...map[el[key_s]], ...el };
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
map[el[key_s]] = el;
|
|
24
|
-
}
|
|
20
|
+
map[el[key_s]] = MERGE && map.hasOwnProperty(el[key_s]) ? { ...map[el[key_s]], ...el } : el;
|
|
25
21
|
}
|
|
26
22
|
return map;
|
|
27
23
|
}
|
package/array/mapPrimitive.d.ts
CHANGED
|
@@ -19,9 +19,7 @@ interface mapOptions {
|
|
|
19
19
|
*/
|
|
20
20
|
keyround?: boolean;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
[key: string]: string | number;
|
|
24
|
-
}
|
|
22
|
+
type mapReturn = Record<string, string | number>;
|
|
25
23
|
/**
|
|
26
24
|
* Map an array of primitive values (numbers/strings) into a kv-object
|
|
27
25
|
* non-numeric and non-string values will be filtered out
|
|
@@ -31,10 +29,10 @@ interface mapReturn {
|
|
|
31
29
|
* Output:
|
|
32
30
|
* {hello: 'hello', foo: 'foo', bar: 'bar'}
|
|
33
31
|
*
|
|
34
|
-
* @param val - Array to map
|
|
35
|
-
* @param opts - Options object to override built-in defaults
|
|
32
|
+
* @param {unknown[]} val - Array to map
|
|
33
|
+
* @param {mapOptions?} opts - Options object to override built-in defaults
|
|
36
34
|
*
|
|
37
|
-
* @returns KV-Map object
|
|
35
|
+
* @returns {mapReturn} KV-Map object
|
|
38
36
|
*/
|
|
39
37
|
export default function mapPrimitive(arr: unknown[], opts?: mapOptions): mapReturn;
|
|
40
38
|
export {};
|
package/array/mapPrimitive.js
CHANGED
|
@@ -1,26 +1,35 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const round_1 = require("../number/round");
|
|
4
|
-
function mapPrimitive(arr, opts
|
|
4
|
+
function mapPrimitive(arr, opts) {
|
|
5
5
|
if (!Array.isArray(arr) || !arr.length)
|
|
6
6
|
return {};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
let VALTRIM = false;
|
|
8
|
+
let VALROUND = false;
|
|
9
|
+
let KEYROUND = false;
|
|
10
|
+
if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
|
|
11
|
+
if (opts.valtrim === true)
|
|
12
|
+
VALTRIM = true;
|
|
13
|
+
if (opts.valround === true ||
|
|
14
|
+
(Number.isInteger(opts.valround) && opts.valround >= 0))
|
|
15
|
+
VALROUND = opts.valround;
|
|
16
|
+
if (opts.keyround === true)
|
|
17
|
+
KEYROUND = true;
|
|
18
|
+
}
|
|
13
19
|
const map = {};
|
|
14
20
|
for (const el of arr) {
|
|
15
|
-
if (typeof el === 'string'
|
|
16
|
-
|
|
21
|
+
if (typeof el === 'string') {
|
|
22
|
+
const trimmed = el.trim();
|
|
23
|
+
if (!trimmed.length)
|
|
24
|
+
continue;
|
|
25
|
+
map[trimmed] = VALTRIM ? trimmed : el;
|
|
17
26
|
}
|
|
18
27
|
else if (typeof el === 'number' && Number.isFinite(el)) {
|
|
19
|
-
map[`${
|
|
28
|
+
map[`${KEYROUND ? Math.round(el) : el}`] = VALROUND === false
|
|
20
29
|
? el
|
|
21
|
-
:
|
|
30
|
+
: VALROUND === true
|
|
22
31
|
? Math.round(el)
|
|
23
|
-
: (0, round_1.default)(el,
|
|
32
|
+
: (0, round_1.default)(el, VALROUND);
|
|
24
33
|
}
|
|
25
34
|
}
|
|
26
35
|
return map;
|
package/array/shuffle.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
function shuffle(arr) {
|
|
4
|
-
if (!Array.isArray(arr)
|
|
4
|
+
if (!Array.isArray(arr))
|
|
5
5
|
return;
|
|
6
6
|
for (let i = arr.length - 1; i > 0; i--) {
|
|
7
7
|
const j = Math.floor(Math.random() * (i + 1));
|
package/array/sort.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const is_1 = require("../boolean/is");
|
|
4
3
|
const isNotEmpty_1 = require("../object/isNotEmpty");
|
|
5
4
|
function partition(arr, start_ix, end_ix) {
|
|
6
5
|
const pivot_val = arr[Math.floor((start_ix + end_ix) / 2)].t;
|
|
@@ -29,19 +28,24 @@ function quickSort(arr, start_ix = 0, end_ix = arr.length - 1) {
|
|
|
29
28
|
}
|
|
30
29
|
return arr;
|
|
31
30
|
}
|
|
32
|
-
function sort(arr, by, dir = 'asc', opts
|
|
31
|
+
function sort(arr, by, dir = 'asc', opts) {
|
|
33
32
|
if (!Array.isArray(arr) || !arr.length)
|
|
34
33
|
return [];
|
|
35
34
|
if (dir !== 'asc' && dir !== 'desc')
|
|
36
35
|
throw new Error('Direction should be either asc or desc');
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
let NOKEY_HIDE = false;
|
|
37
|
+
let NOKEY_AT_END = true;
|
|
38
|
+
let FILTER_FN = isNotEmpty_1.default;
|
|
39
|
+
if (opts && Object.prototype.toString.call(opts) === '[object Object]') {
|
|
40
|
+
if (opts.nokey_hide === true)
|
|
41
|
+
NOKEY_HIDE = true;
|
|
42
|
+
if (opts.nokey_atend === false)
|
|
43
|
+
NOKEY_AT_END = false;
|
|
44
|
+
if (typeof opts.filter_fn === 'function') {
|
|
45
|
+
const fn = opts.filter_fn;
|
|
46
|
+
FILTER_FN = el => (0, isNotEmpty_1.default)(el) && fn(el);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
45
49
|
const prepared_arr = [];
|
|
46
50
|
const nokey_arr = [];
|
|
47
51
|
if (typeof by === 'string') {
|
|
@@ -49,7 +53,7 @@ function sort(arr, by, dir = 'asc', opts = {}) {
|
|
|
49
53
|
if (!by_s.length)
|
|
50
54
|
throw new Error('Sort by as string should contain content');
|
|
51
55
|
for (const el of arr) {
|
|
52
|
-
if (!
|
|
56
|
+
if (!FILTER_FN(el))
|
|
53
57
|
continue;
|
|
54
58
|
if (!Object.prototype.hasOwnProperty.call(el, by_s) || el[by_s] === undefined) {
|
|
55
59
|
nokey_arr.push(el);
|
|
@@ -62,7 +66,7 @@ function sort(arr, by, dir = 'asc', opts = {}) {
|
|
|
62
66
|
else if (typeof by === 'function') {
|
|
63
67
|
let key;
|
|
64
68
|
for (const el of arr) {
|
|
65
|
-
if (!
|
|
69
|
+
if (!FILTER_FN(el))
|
|
66
70
|
continue;
|
|
67
71
|
key = by(el);
|
|
68
72
|
if (key === undefined) {
|
|
@@ -80,11 +84,11 @@ function sort(arr, by, dir = 'asc', opts = {}) {
|
|
|
80
84
|
if (dir === 'desc')
|
|
81
85
|
prepared_arr.reverse();
|
|
82
86
|
const result = [];
|
|
83
|
-
if (
|
|
87
|
+
if (NOKEY_HIDE) {
|
|
84
88
|
for (const obj of prepared_arr)
|
|
85
89
|
result.push(obj.el);
|
|
86
90
|
}
|
|
87
|
-
else if (
|
|
91
|
+
else if (NOKEY_AT_END) {
|
|
88
92
|
for (const obj of prepared_arr)
|
|
89
93
|
result.push(obj.el);
|
|
90
94
|
for (const el of nokey_arr)
|
package/boolean/is.d.ts
CHANGED
package/date/format.js
CHANGED
|
@@ -51,7 +51,7 @@ function runIntl(loc, token, props, val) {
|
|
|
51
51
|
}
|
|
52
52
|
const Tokens = [
|
|
53
53
|
['YYYY', d => d.getFullYear()],
|
|
54
|
-
['Q', d =>
|
|
54
|
+
['Q', d => ((d.getMonth() + 3) / 3) | 0],
|
|
55
55
|
['MMMM', (d, loc) => runIntl(loc, 'MMMM', { month: 'long' }, d)],
|
|
56
56
|
['MMM', (d, loc) => runIntl(loc, 'MMM', { month: 'short' }, d)],
|
|
57
57
|
['MM', d => `${d.getMonth() + 1}`.padStart(2, '0')],
|
package/date/is.d.ts
CHANGED
package/deep/set.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RGX_MALICIOUS = /__proto__|constructor|prototype/;
|
|
3
4
|
function deepSet(obj, path, value, define = false) {
|
|
4
5
|
if (Object.prototype.toString.call(obj) !== '[object Object]' &&
|
|
5
6
|
!Array.isArray(obj))
|
|
6
7
|
throw new TypeError('Deepset is only supported for objects');
|
|
7
8
|
if (typeof path !== 'string')
|
|
8
9
|
throw new TypeError('No path was given');
|
|
9
|
-
if (
|
|
10
|
+
if (RGX_MALICIOUS.test(path))
|
|
10
11
|
throw new TypeError('Malicious path provided');
|
|
11
12
|
const path_s = path.trim();
|
|
12
13
|
if (!path_s.length)
|
|
@@ -16,7 +17,8 @@ function deepSet(obj, path, value, define = false) {
|
|
|
16
17
|
.replace(/(\.){2,}/g, '.')
|
|
17
18
|
.replace(/(^\.|\.$|\])/g, '')
|
|
18
19
|
.split('.');
|
|
19
|
-
|
|
20
|
+
const last_part_ix = parts.length - 1;
|
|
21
|
+
for (let i = 0; i < last_part_ix; i++) {
|
|
20
22
|
if (parts[i] === '')
|
|
21
23
|
continue;
|
|
22
24
|
if (Array.isArray(obj)) {
|
|
@@ -36,16 +38,16 @@ function deepSet(obj, path, value, define = false) {
|
|
|
36
38
|
if (!Array.isArray(obj) && Object.prototype.toString.call(obj) !== '[object Object]')
|
|
37
39
|
return false;
|
|
38
40
|
if (define) {
|
|
39
|
-
Object.defineProperty(obj, parts[
|
|
41
|
+
Object.defineProperty(obj, parts[last_part_ix], value);
|
|
40
42
|
}
|
|
41
43
|
else if (Array.isArray(obj)) {
|
|
42
|
-
const idx = parseInt(parts[
|
|
44
|
+
const idx = parseInt(parts[last_part_ix]);
|
|
43
45
|
if (!Number.isInteger(idx) || idx < 0)
|
|
44
46
|
throw new TypeError('Invalid path provided');
|
|
45
47
|
obj[idx] = value;
|
|
46
48
|
}
|
|
47
49
|
else {
|
|
48
|
-
obj[parts[
|
|
50
|
+
obj[parts[last_part_ix]] = value;
|
|
49
51
|
}
|
|
50
52
|
return true;
|
|
51
53
|
}
|
package/function/is.d.ts
CHANGED
package/function/isAsync.d.ts
CHANGED
package/hash/guid.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const HEX = [];
|
|
4
4
|
for (let i = 0; i < 256; i++) {
|
|
5
|
-
|
|
5
|
+
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
6
6
|
}
|
|
7
7
|
function guid() {
|
|
8
8
|
const d0 = (Math.random() * 0xffffffff) | 0;
|
|
9
9
|
const d1 = (Math.random() * 0xffffffff) | 0;
|
|
10
10
|
const d2 = (Math.random() * 0xffffffff) | 0;
|
|
11
11
|
const d3 = (Math.random() * 0xffffffff) | 0;
|
|
12
|
-
return
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
return HEX[d0 & 0xff] +
|
|
13
|
+
HEX[(d0 >> 8) & 0xff] +
|
|
14
|
+
HEX[(d0 >> 16) & 0xff] +
|
|
15
|
+
HEX[(d0 >> 24) & 0xff] +
|
|
16
16
|
'-' +
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
HEX[d1 & 0xff] +
|
|
18
|
+
HEX[(d1 >> 8) & 0xff] +
|
|
19
19
|
'-' +
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
HEX[((d1 >> 16) & 0x0f) | 0x40] +
|
|
21
|
+
HEX[(d1 >> 24) & 0xff] +
|
|
22
22
|
'-' +
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
HEX[(d2 & 0x3f) | 0x80] +
|
|
24
|
+
HEX[(d2 >> 8) & 0xff] +
|
|
25
25
|
'-' +
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
HEX[(d2 >> 16) & 0xff] +
|
|
27
|
+
HEX[(d2 >> 24) & 0xff] +
|
|
28
|
+
HEX[d3 & 0xff] +
|
|
29
|
+
HEX[(d3 >> 8) & 0xff] +
|
|
30
|
+
HEX[(d3 >> 16) & 0xff] +
|
|
31
|
+
HEX[(d3 >> 24) & 0xff];
|
|
32
32
|
}
|
|
33
33
|
exports.default = guid;
|
package/index.d.ts
CHANGED
|
@@ -6,76 +6,80 @@ declare module "equal" {
|
|
|
6
6
|
export default equal;
|
|
7
7
|
}
|
|
8
8
|
declare module "array/isNotEmpty" {
|
|
9
|
-
export default function isNotEmptyArray(val: unknown):
|
|
9
|
+
export default function isNotEmptyArray(val: unknown): val is unknown[];
|
|
10
10
|
}
|
|
11
11
|
declare module "array/is" {
|
|
12
|
-
export default function isArray(val: unknown):
|
|
12
|
+
export default function isArray(val: unknown): val is unknown[];
|
|
13
13
|
}
|
|
14
14
|
declare module "boolean/is" {
|
|
15
|
-
export default function isBoolean(val: unknown): boolean;
|
|
15
|
+
export default function isBoolean(val: unknown): val is boolean;
|
|
16
16
|
}
|
|
17
17
|
declare module "date/is" {
|
|
18
|
-
export default function isDate(val: unknown):
|
|
18
|
+
export default function isDate(val: unknown): val is Date;
|
|
19
19
|
}
|
|
20
20
|
declare module "function/is" {
|
|
21
|
-
export default function isFunction(val: unknown):
|
|
21
|
+
export default function isFunction(val: unknown): val is Function;
|
|
22
22
|
}
|
|
23
23
|
declare module "function/isAsync" {
|
|
24
|
-
export default function isAsyncFunction(val: unknown):
|
|
24
|
+
export default function isAsyncFunction(val: unknown): val is Function;
|
|
25
25
|
}
|
|
26
26
|
declare module "number/is" {
|
|
27
|
-
export default function isNumber(val: unknown):
|
|
27
|
+
export default function isNumber(val: unknown): val is number;
|
|
28
28
|
}
|
|
29
29
|
declare module "number/isBetween" {
|
|
30
|
-
export default function isNumberBetween(val: unknown, min: number, max: number):
|
|
30
|
+
export default function isNumberBetween(val: unknown, min: number, max: number): val is number;
|
|
31
31
|
}
|
|
32
32
|
declare module "number/isBelow" {
|
|
33
|
-
export default function isNumberBelow(val: unknown, ref: number):
|
|
33
|
+
export default function isNumberBelow(val: unknown, ref: number): val is number;
|
|
34
34
|
}
|
|
35
35
|
declare module "number/isBelowOrEqual" {
|
|
36
|
-
export default function isNumberBelowOrEqual(val: unknown, ref: number):
|
|
36
|
+
export default function isNumberBelowOrEqual(val: unknown, ref: number): val is number;
|
|
37
37
|
}
|
|
38
38
|
declare module "number/isAbove" {
|
|
39
|
-
export default function isNumberAbove(val: unknown, ref: number):
|
|
39
|
+
export default function isNumberAbove(val: unknown, ref: number): val is number;
|
|
40
40
|
}
|
|
41
41
|
declare module "number/isAboveOrEqual" {
|
|
42
|
-
export default function isNumberAboveOrEqual(val: unknown, ref: number):
|
|
42
|
+
export default function isNumberAboveOrEqual(val: unknown, ref: number): val is number;
|
|
43
43
|
}
|
|
44
44
|
declare module "number/isInteger" {
|
|
45
|
-
export default function isInteger(val: unknown):
|
|
45
|
+
export default function isInteger(val: unknown): val is number;
|
|
46
46
|
}
|
|
47
47
|
declare module "number/isIntegerBetween" {
|
|
48
|
-
export default function isIntegerBetween(val: unknown, min: number, max: number):
|
|
48
|
+
export default function isIntegerBetween(val: unknown, min: number, max: number): val is number;
|
|
49
49
|
}
|
|
50
50
|
declare module "number/isIntegerBelow" {
|
|
51
|
-
export default function isIntegerBelow(val: unknown, ref: number):
|
|
51
|
+
export default function isIntegerBelow(val: unknown, ref: number): val is number;
|
|
52
52
|
}
|
|
53
53
|
declare module "number/isIntegerBelowOrEqual" {
|
|
54
|
-
export default function isIntegerBelowOrEqual(val: unknown, ref: number):
|
|
54
|
+
export default function isIntegerBelowOrEqual(val: unknown, ref: number): val is number;
|
|
55
55
|
}
|
|
56
56
|
declare module "number/isIntegerAbove" {
|
|
57
|
-
export default function isIntegerAbove(val: unknown, ref: number):
|
|
57
|
+
export default function isIntegerAbove(val: unknown, ref: number): val is number;
|
|
58
58
|
}
|
|
59
59
|
declare module "number/isIntegerAboveOrEqual" {
|
|
60
|
-
export default function isIntegerAboveOrEqual(val: unknown, ref: number):
|
|
60
|
+
export default function isIntegerAboveOrEqual(val: unknown, ref: number): val is number;
|
|
61
61
|
}
|
|
62
62
|
declare module "regexp/is" {
|
|
63
|
-
export default function isRegExp(val: unknown):
|
|
63
|
+
export default function isRegExp(val: unknown): val is RegExp;
|
|
64
64
|
}
|
|
65
65
|
declare module "object/is" {
|
|
66
|
-
export default function isObject(val: unknown):
|
|
66
|
+
export default function isObject(val: unknown): val is {
|
|
67
|
+
[key: string]: any;
|
|
68
|
+
};
|
|
67
69
|
}
|
|
68
70
|
declare module "object/isNotEmpty" {
|
|
69
|
-
export default function isNotEmptyObject(val: unknown):
|
|
71
|
+
export default function isNotEmptyObject(val: unknown): val is {
|
|
72
|
+
[key: string]: any;
|
|
73
|
+
};
|
|
70
74
|
}
|
|
71
75
|
declare module "string/is" {
|
|
72
|
-
export default function isString(val: unknown):
|
|
76
|
+
export default function isString(val: unknown): val is string;
|
|
73
77
|
}
|
|
74
78
|
declare module "string/isBetween" {
|
|
75
|
-
export default function isStringBetween(val: unknown, min: number, max: number, trimmed?: boolean):
|
|
79
|
+
export default function isStringBetween(val: unknown, min: number, max: number, trimmed?: boolean): val is string;
|
|
76
80
|
}
|
|
77
81
|
declare module "string/isNotEmpty" {
|
|
78
|
-
export default function isNotEmptyString(val: unknown, trimmed?: boolean):
|
|
82
|
+
export default function isNotEmptyString(val: unknown, trimmed?: boolean): val is string;
|
|
79
83
|
}
|
|
80
84
|
declare module "is" {
|
|
81
85
|
import equal from "equal";
|
|
@@ -156,9 +160,6 @@ declare module "is" {
|
|
|
156
160
|
}>;
|
|
157
161
|
export default Is;
|
|
158
162
|
}
|
|
159
|
-
declare module "hash/fnv1A" {
|
|
160
|
-
export default function fnv1A(data: unknown, offset?: number): number;
|
|
161
|
-
}
|
|
162
163
|
declare module "array/dedupe" {
|
|
163
164
|
export default function dedupe<T>(val: T[]): T[];
|
|
164
165
|
}
|
|
@@ -178,29 +179,18 @@ declare module "array/mapFn" {
|
|
|
178
179
|
interface mapOptions {
|
|
179
180
|
merge?: boolean;
|
|
180
181
|
}
|
|
181
|
-
|
|
182
|
-
[key: string]: {
|
|
183
|
-
[key: string]: any;
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
export default function mapFn(arr: {
|
|
182
|
+
type mapFn = (entry: {
|
|
187
183
|
[key: string]: any;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
184
|
+
}) => (string | number | boolean);
|
|
185
|
+
type mapReturn = Record<string, Record<string, any>>;
|
|
186
|
+
export default function mapFn(arr: Record<string, any>[], fn: mapFn, opts?: mapOptions): mapReturn;
|
|
191
187
|
}
|
|
192
188
|
declare module "array/mapKey" {
|
|
193
189
|
interface mapOptions {
|
|
194
190
|
merge?: boolean;
|
|
195
191
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
[key: string]: any;
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
export default function mapKey(arr: {
|
|
202
|
-
[key: string]: any;
|
|
203
|
-
}[], key: string, opts?: mapOptions): kvMap;
|
|
192
|
+
type kvMap = Record<string, Record<string, any>>;
|
|
193
|
+
export default function mapKey(arr: Record<string, any>[], key: string, opts?: mapOptions): kvMap;
|
|
204
194
|
}
|
|
205
195
|
declare module "array/mapPrimitive" {
|
|
206
196
|
interface mapOptions {
|
|
@@ -208,9 +198,7 @@ declare module "array/mapPrimitive" {
|
|
|
208
198
|
valround?: boolean | number;
|
|
209
199
|
keyround?: boolean;
|
|
210
200
|
}
|
|
211
|
-
|
|
212
|
-
[key: string]: string | number;
|
|
213
|
-
}
|
|
201
|
+
type mapReturn = Record<string, string | number>;
|
|
214
202
|
export default function mapPrimitive(arr: unknown[], opts?: mapOptions): mapReturn;
|
|
215
203
|
}
|
|
216
204
|
declare module "array/shuffle" {
|
|
@@ -306,6 +294,9 @@ declare module "function/noopreturn" {
|
|
|
306
294
|
declare module "function/sleep" {
|
|
307
295
|
export default function sleep(ms?: number): Promise<void>;
|
|
308
296
|
}
|
|
297
|
+
declare module "hash/fnv1A" {
|
|
298
|
+
export default function fnv1A(data: unknown, offset?: number): number;
|
|
299
|
+
}
|
|
309
300
|
declare module "hash/guid" {
|
|
310
301
|
export default function guid(): string;
|
|
311
302
|
}
|
|
@@ -356,7 +347,7 @@ declare module "string/humanizeNumber" {
|
|
|
356
347
|
divider?: number;
|
|
357
348
|
real?: boolean;
|
|
358
349
|
}
|
|
359
|
-
export default function humanizeNumber(val: number | string, options?: humanizeNumberOptions): string;
|
|
350
|
+
export default function humanizeNumber(val: number | string, options?: humanizeNumberOptions | false): string;
|
|
360
351
|
}
|
|
361
352
|
declare module "string/humanizeBytes" {
|
|
362
353
|
interface humanizeBytesOptions {
|
package/number/is.d.ts
CHANGED
package/number/isAbove.d.ts
CHANGED
package/number/isBelow.d.ts
CHANGED
package/number/isBetween.d.ts
CHANGED
|
@@ -9,4 +9,4 @@
|
|
|
9
9
|
*
|
|
10
10
|
* @returns Whether or not the value is a number between min and max inclusive
|
|
11
11
|
*/
|
|
12
|
-
export default function isNumberBetween(val: unknown, min: number, max: number):
|
|
12
|
+
export default function isNumberBetween(val: unknown, min: number, max: number): val is number;
|
package/number/isInteger.d.ts
CHANGED
|
@@ -9,4 +9,4 @@
|
|
|
9
9
|
*
|
|
10
10
|
* @returns Whether or not the value is an integer between min and max inclusive
|
|
11
11
|
*/
|
|
12
|
-
export default function isIntegerBetween(val: unknown, min: number, max: number):
|
|
12
|
+
export default function isIntegerBetween(val: unknown, min: number, max: number): val is number;
|
package/object/is.d.ts
CHANGED
package/object/isNotEmpty.d.ts
CHANGED
package/object/pick.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const get_1 = require("../deep/get");
|
|
4
4
|
const set_1 = require("../deep/set");
|
|
5
|
+
const RGX_DEEP = /(\.|\[)/;
|
|
5
6
|
function pick(obj, keys) {
|
|
6
7
|
if (Object.prototype.toString.call(obj) !== '[object Object]' ||
|
|
7
8
|
!Array.isArray(keys) ||
|
|
@@ -16,7 +17,7 @@ function pick(obj, keys) {
|
|
|
16
17
|
sanitized = key.trim();
|
|
17
18
|
if (!sanitized.length)
|
|
18
19
|
continue;
|
|
19
|
-
if (
|
|
20
|
+
if (RGX_DEEP.test(sanitized)) {
|
|
20
21
|
val = (0, get_1.default)(obj, sanitized);
|
|
21
22
|
if (val === undefined)
|
|
22
23
|
continue;
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "name": "@valkyriestudios/utils", "version": "12.
|
|
1
|
+
{ "name": "@valkyriestudios/utils", "version": "12.2.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "email": "contact@valkyriestudios.be", "url": "www.valkyriestudios.be" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
|
package/regexp/is.d.ts
CHANGED
|
@@ -48,5 +48,5 @@ interface humanizeNumberOptions {
|
|
|
48
48
|
*
|
|
49
49
|
* @returns Humanized number as string
|
|
50
50
|
*/
|
|
51
|
-
export default function humanizeNumber(val: number | string, options?: humanizeNumberOptions): string;
|
|
51
|
+
export default function humanizeNumber(val: number | string, options?: humanizeNumberOptions | false): string;
|
|
52
52
|
export {};
|
package/string/humanizeNumber.js
CHANGED
|
@@ -1,57 +1,56 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const is_1 = require("../boolean/is");
|
|
4
|
-
const is_2 = require("../string/is");
|
|
5
3
|
const round_1 = require("../number/round");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
4
|
+
const DEFAULT_UNITS = ['', 'k', 'm', 'b', 't', 'q'];
|
|
5
|
+
function humanizeNumber(val, options = false) {
|
|
6
|
+
let DELIM = ',';
|
|
7
|
+
let SEPARATOR = '.';
|
|
8
|
+
let PRECISION = 2;
|
|
9
|
+
let UNITS = DEFAULT_UNITS;
|
|
10
|
+
let DIVIDER = 1000;
|
|
11
|
+
let REAL = false;
|
|
12
|
+
if (options && Object.prototype.toString.call(options) === '[object Object]') {
|
|
13
|
+
if (typeof options.delim === 'string')
|
|
14
|
+
DELIM = options.delim;
|
|
15
|
+
if (typeof options.separator === 'string' && options.separator.trim().length)
|
|
16
|
+
SEPARATOR = options.separator;
|
|
17
|
+
if (Number.isInteger(options.precision) && options.precision >= 0)
|
|
18
|
+
PRECISION = options.precision;
|
|
19
|
+
if (Array.isArray(options.units) && options.units.length) {
|
|
20
|
+
UNITS = options.units;
|
|
21
|
+
}
|
|
22
|
+
else if (options.units === false) {
|
|
23
|
+
UNITS = false;
|
|
24
|
+
}
|
|
25
|
+
if (Number.isInteger(options.divider) && options.divider > 1) {
|
|
26
|
+
DIVIDER = options.divider;
|
|
27
|
+
}
|
|
28
|
+
if (options.real === true)
|
|
29
|
+
REAL = true;
|
|
31
30
|
}
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
let normalized = 0;
|
|
32
|
+
if (typeof val === 'string') {
|
|
33
|
+
normalized = REAL ? parseInt(val.trim(), 10) : parseFloat(val);
|
|
34
34
|
}
|
|
35
|
-
if (
|
|
36
|
-
|
|
35
|
+
else if (Number.isFinite(val)) {
|
|
36
|
+
normalized = REAL ? Math.round(val) : val;
|
|
37
37
|
}
|
|
38
|
+
if (!Number.isFinite(normalized) || normalized === 0)
|
|
39
|
+
return UNITS ? `0${UNITS[0]}` : '0';
|
|
38
40
|
const sign = normalized < 0 ? '-' : '';
|
|
39
41
|
normalized = Math.abs(normalized);
|
|
40
|
-
let
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
normalized /= OPTS.divider;
|
|
42
|
+
let unit_ix = 0;
|
|
43
|
+
if (UNITS) {
|
|
44
|
+
for (unit_ix; normalized >= DIVIDER && unit_ix < UNITS.length - 1; unit_ix++) {
|
|
45
|
+
normalized /= DIVIDER;
|
|
45
46
|
}
|
|
46
|
-
postfix = OPTS.units[unit_ix];
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
const humanized = `${normalized}`.split('.');
|
|
48
|
+
const humanized = `${(0, round_1.default)(normalized, PRECISION)}`.split('.', 2);
|
|
50
49
|
humanized[0] = humanized[0].split('').reverse().map((char, ix, original) => {
|
|
51
50
|
if (ix > 0 && ix < original.length && ix % 3 === 0)
|
|
52
|
-
return char +
|
|
51
|
+
return char + DELIM;
|
|
53
52
|
return char;
|
|
54
53
|
}).reverse().join('');
|
|
55
|
-
return `${sign}${humanized.join(
|
|
54
|
+
return `${sign}${humanized.join(SEPARATOR)}${UNITS ? UNITS[unit_ix] : ''}`;
|
|
56
55
|
}
|
|
57
56
|
exports.default = humanizeNumber;
|
package/string/is.d.ts
CHANGED
package/string/isBetween.d.ts
CHANGED
|
@@ -10,4 +10,4 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @returns Whether or not the value is a string of length between min and max inclusive
|
|
12
12
|
*/
|
|
13
|
-
export default function isStringBetween(val: unknown, min: number, max: number, trimmed?: boolean):
|
|
13
|
+
export default function isStringBetween(val: unknown, min: number, max: number, trimmed?: boolean): val is string;
|
package/string/isNotEmpty.d.ts
CHANGED
package/string/shorten.js
CHANGED
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
function shorten(val, length, postfix = '...') {
|
|
4
4
|
if (typeof val !== 'string')
|
|
5
5
|
return '';
|
|
6
|
-
if (typeof postfix !== 'string' || !Number.
|
|
6
|
+
if (typeof postfix !== 'string' || !Number.isInteger(length) || length <= 0)
|
|
7
7
|
return val;
|
|
8
8
|
const sanitized = val.trim();
|
|
9
|
-
return sanitized.length <= length ? sanitized : `${sanitized.
|
|
9
|
+
return sanitized.length <= length ? sanitized : `${sanitized.substring(0, length)}${postfix}`;
|
|
10
10
|
}
|
|
11
11
|
exports.default = shorten;
|