@valkyriestudios/utils 11.4.0 → 11.6.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 +3 -3
- package/array/join.js +3 -3
- package/array/mapFn.js +4 -3
- package/array/mapKey.js +4 -3
- package/array/mapPrimitive.js +3 -2
- package/array/sort.js +13 -4
- package/hash/fnv1A.js +4 -3
- package/object/merge.js +13 -8
- package/object/pick.js +8 -11
- package/package.json +1 -1
package/array/dedupe.js
CHANGED
|
@@ -4,14 +4,14 @@ const fnv1A_1 = require("../hash/fnv1A");
|
|
|
4
4
|
function dedupe(val) {
|
|
5
5
|
if (!Array.isArray(val) || val.length === 0)
|
|
6
6
|
return [];
|
|
7
|
-
const
|
|
7
|
+
const set = new Set();
|
|
8
8
|
const acc = [];
|
|
9
9
|
let hash;
|
|
10
10
|
for (const item of val) {
|
|
11
11
|
hash = (0, fnv1A_1.default)(item);
|
|
12
|
-
if (
|
|
12
|
+
if (set.has(hash))
|
|
13
13
|
continue;
|
|
14
|
-
|
|
14
|
+
set.add(hash);
|
|
15
15
|
acc.push(item);
|
|
16
16
|
}
|
|
17
17
|
return acc;
|
package/array/join.js
CHANGED
|
@@ -4,12 +4,12 @@ const round_1 = require("../number/round");
|
|
|
4
4
|
function join(val, opts = {}) {
|
|
5
5
|
if (!Array.isArray(val) || val.length === 0)
|
|
6
6
|
return '';
|
|
7
|
-
const OPTS =
|
|
7
|
+
const OPTS = {
|
|
8
8
|
delim: ' ',
|
|
9
9
|
trim: true,
|
|
10
10
|
valtrim: true,
|
|
11
|
-
|
|
12
|
-
}
|
|
11
|
+
...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
|
|
12
|
+
};
|
|
13
13
|
const filtered = [];
|
|
14
14
|
for (const el of val) {
|
|
15
15
|
if (typeof el === 'string' && el.trim().length > 0) {
|
package/array/mapFn.js
CHANGED
|
@@ -4,9 +4,10 @@ function mapFn(arr, fn, opts = {}) {
|
|
|
4
4
|
if ((!Array.isArray(arr) || arr.length === 0) ||
|
|
5
5
|
typeof fn !== 'function')
|
|
6
6
|
return {};
|
|
7
|
-
const OPTS =
|
|
7
|
+
const OPTS = {
|
|
8
8
|
merge: false,
|
|
9
|
-
|
|
9
|
+
...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
|
|
10
|
+
};
|
|
10
11
|
const map = {};
|
|
11
12
|
let hash = false;
|
|
12
13
|
for (const el of arr) {
|
|
@@ -17,7 +18,7 @@ function mapFn(arr, fn, opts = {}) {
|
|
|
17
18
|
continue;
|
|
18
19
|
hash = `${hash}`;
|
|
19
20
|
if (OPTS.merge === true && map.hasOwnProperty(hash)) {
|
|
20
|
-
map[hash] =
|
|
21
|
+
map[hash] = { ...map[hash], ...el };
|
|
21
22
|
}
|
|
22
23
|
else {
|
|
23
24
|
map[hash] = el;
|
package/array/mapKey.js
CHANGED
|
@@ -7,16 +7,17 @@ function mapKey(arr, key, opts = {}) {
|
|
|
7
7
|
const key_s = key.trim();
|
|
8
8
|
if (key_s.length === 0)
|
|
9
9
|
return {};
|
|
10
|
-
const OPTS =
|
|
10
|
+
const OPTS = {
|
|
11
11
|
merge: false,
|
|
12
|
-
|
|
12
|
+
...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
|
|
13
|
+
};
|
|
13
14
|
const map = {};
|
|
14
15
|
for (const el of arr) {
|
|
15
16
|
if (Object.prototype.toString.call(el) !== '[object Object]' ||
|
|
16
17
|
!Object.prototype.hasOwnProperty.call(el, key_s))
|
|
17
18
|
continue;
|
|
18
19
|
if (OPTS.merge === true && map.hasOwnProperty(el[key_s])) {
|
|
19
|
-
map[el[key_s]] =
|
|
20
|
+
map[el[key_s]] = { ...map[el[key_s]], ...el };
|
|
20
21
|
}
|
|
21
22
|
else {
|
|
22
23
|
map[el[key_s]] = el;
|
package/array/mapPrimitive.js
CHANGED
|
@@ -4,11 +4,12 @@ const round_1 = require("../number/round");
|
|
|
4
4
|
function mapPrimitive(arr, opts = {}) {
|
|
5
5
|
if (!Array.isArray(arr) || arr.length === 0)
|
|
6
6
|
return {};
|
|
7
|
-
const OPTS =
|
|
7
|
+
const OPTS = {
|
|
8
8
|
valtrim: false,
|
|
9
9
|
valround: false,
|
|
10
10
|
keyround: false,
|
|
11
|
-
|
|
11
|
+
...Object.prototype.toString.call(opts) === '[object Object]' ? opts : {},
|
|
12
|
+
};
|
|
12
13
|
const map = {};
|
|
13
14
|
for (const el of arr) {
|
|
14
15
|
if (typeof el === 'string' && el.trim().length > 0) {
|
package/array/sort.js
CHANGED
|
@@ -69,7 +69,7 @@ function sort(arr, by, dir = 'asc', opts = {}) {
|
|
|
69
69
|
nokey_arr.push(el);
|
|
70
70
|
}
|
|
71
71
|
else {
|
|
72
|
-
prepared_arr.push({ t:
|
|
72
|
+
prepared_arr.push({ t: key, el });
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -79,14 +79,23 @@ function sort(arr, by, dir = 'asc', opts = {}) {
|
|
|
79
79
|
quickSort(prepared_arr);
|
|
80
80
|
if (dir === 'desc')
|
|
81
81
|
prepared_arr.reverse();
|
|
82
|
+
const result = [];
|
|
82
83
|
if (OPTS.nokey_hide) {
|
|
83
|
-
|
|
84
|
+
for (const obj of prepared_arr)
|
|
85
|
+
result.push(obj.el);
|
|
84
86
|
}
|
|
85
87
|
else if (OPTS.nokey_atend) {
|
|
86
|
-
|
|
88
|
+
for (const obj of prepared_arr)
|
|
89
|
+
result.push(obj.el);
|
|
90
|
+
for (const el of nokey_arr)
|
|
91
|
+
result.push(el);
|
|
87
92
|
}
|
|
88
93
|
else {
|
|
89
|
-
|
|
94
|
+
for (const el of nokey_arr)
|
|
95
|
+
result.push(el);
|
|
96
|
+
for (const obj of prepared_arr)
|
|
97
|
+
result.push(obj.el);
|
|
90
98
|
}
|
|
99
|
+
return result;
|
|
91
100
|
}
|
|
92
101
|
exports.default = sort;
|
package/hash/fnv1A.js
CHANGED
|
@@ -13,7 +13,7 @@ function fnv1A(data, offset = FNV_32) {
|
|
|
13
13
|
sanitized = data;
|
|
14
14
|
}
|
|
15
15
|
else if (Number.isFinite(data)) {
|
|
16
|
-
sanitized =
|
|
16
|
+
sanitized = String(data);
|
|
17
17
|
}
|
|
18
18
|
else if (Array.isArray(data) || Object.prototype.toString.call(data) === '[object Object]') {
|
|
19
19
|
sanitized = JSON.stringify(data);
|
|
@@ -22,7 +22,7 @@ function fnv1A(data, offset = FNV_32) {
|
|
|
22
22
|
sanitized = data.toString();
|
|
23
23
|
}
|
|
24
24
|
else if (data instanceof Date) {
|
|
25
|
-
sanitized =
|
|
25
|
+
sanitized = String(data.getTime());
|
|
26
26
|
}
|
|
27
27
|
else if (Number.isNaN(data) || data === Infinity) {
|
|
28
28
|
sanitized = REPL_NAN;
|
|
@@ -42,7 +42,8 @@ function fnv1A(data, offset = FNV_32) {
|
|
|
42
42
|
else {
|
|
43
43
|
throw new TypeError('An FNV1A Hash could not be calculated for this datatype');
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
const len = sanitized.length;
|
|
46
|
+
for (let i = 0; i < len; i++) {
|
|
46
47
|
hash ^= sanitized.charCodeAt(i);
|
|
47
48
|
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
48
49
|
}
|
package/object/merge.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const PROTO_OBJ = '[object Object]';
|
|
3
4
|
function merge(target, source = {}) {
|
|
4
|
-
if (Object.prototype.toString.call(target) !==
|
|
5
|
-
Object.prototype.toString.call(source) !==
|
|
5
|
+
if (Object.prototype.toString.call(target) !== PROTO_OBJ ||
|
|
6
|
+
Object.prototype.toString.call(source) !== PROTO_OBJ)
|
|
6
7
|
throw new TypeError('Please pass a target and object to merge');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const acc = {};
|
|
9
|
+
for (const key in target) {
|
|
10
|
+
if (!Object.prototype.hasOwnProperty.call(target, key))
|
|
11
|
+
continue;
|
|
12
|
+
if (Object.prototype.toString.call(target[key]) === PROTO_OBJ &&
|
|
13
|
+
Object.prototype.hasOwnProperty.call(source, key) &&
|
|
14
|
+
Object.prototype.toString.call(source[key]) === PROTO_OBJ) {
|
|
15
|
+
acc[key] = merge(target[key], source[key]);
|
|
11
16
|
}
|
|
12
17
|
else {
|
|
13
18
|
acc[key] = Object.prototype.hasOwnProperty.call(source, key)
|
|
14
19
|
? source[key]
|
|
15
20
|
: target[key];
|
|
16
21
|
}
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
}
|
|
23
|
+
return acc;
|
|
19
24
|
}
|
|
20
25
|
exports.default = merge;
|
package/object/pick.js
CHANGED
|
@@ -8,25 +8,22 @@ function pick(obj, keys) {
|
|
|
8
8
|
keys.length === 0)
|
|
9
9
|
throw new TypeError('Please pass an object to pick from and a keys array');
|
|
10
10
|
const map = {};
|
|
11
|
-
let key_deep = false;
|
|
12
11
|
let val;
|
|
12
|
+
let sanitized;
|
|
13
13
|
for (const key of keys) {
|
|
14
14
|
if (typeof key !== 'string')
|
|
15
15
|
continue;
|
|
16
|
-
|
|
16
|
+
sanitized = key.trim();
|
|
17
17
|
if (sanitized.length === 0)
|
|
18
18
|
continue;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (val === undefined)
|
|
24
|
-
continue;
|
|
25
|
-
if (key_deep) {
|
|
19
|
+
if (/(\.|\[)/g.test(sanitized)) {
|
|
20
|
+
val = (0, get_1.default)(obj, sanitized);
|
|
21
|
+
if (val === undefined)
|
|
22
|
+
continue;
|
|
26
23
|
(0, set_1.default)(map, sanitized, val);
|
|
27
24
|
}
|
|
28
|
-
else {
|
|
29
|
-
map[sanitized] =
|
|
25
|
+
else if (obj[sanitized] !== undefined) {
|
|
26
|
+
map[sanitized] = obj[sanitized];
|
|
30
27
|
}
|
|
31
28
|
}
|
|
32
29
|
return map;
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "name": "@valkyriestudios/utils", "version": "11.
|
|
1
|
+
{ "name": "@valkyriestudios/utils", "version": "11.6.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" }
|