@valkyriestudios/utils 12.28.0 → 12.29.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/deep/get.d.ts +3 -3
- package/deep/get.js +49 -46
- package/package.json +1 -1
package/deep/get.d.ts
CHANGED
|
@@ -18,9 +18,9 @@ type DeepGetResult<T extends ObjectType | ArrayType, P extends string> = P exten
|
|
|
18
18
|
* Output:
|
|
19
19
|
* 2
|
|
20
20
|
*
|
|
21
|
-
* @param
|
|
22
|
-
* @param path - Path string to deeply get the value at
|
|
23
|
-
* @param get_parent - If passed as true retrieves the parent of where the value lives
|
|
21
|
+
* @param {Record<string, unknown>|unknown[]} obj - Object/Array to get the value from
|
|
22
|
+
* @param {string} path - Path string to deeply get the value at
|
|
23
|
+
* @param {boolean} get_parent - If passed as true retrieves the parent of where the value lives
|
|
24
24
|
* @throws {TypeError}
|
|
25
25
|
*/
|
|
26
26
|
declare function deepGet<T extends ObjectType | ArrayType, P extends string>(obj: T, path: P, get_parent?: boolean): DeepGetResult<T, P> | undefined;
|
package/deep/get.js
CHANGED
|
@@ -5,60 +5,63 @@ exports.default = deepGet;
|
|
|
5
5
|
function deepGet(obj, path, get_parent = false) {
|
|
6
6
|
if (Object.prototype.toString.call(obj) !== '[object Object]' &&
|
|
7
7
|
!Array.isArray(obj))
|
|
8
|
-
throw new TypeError('
|
|
9
|
-
if (typeof path !== 'string'
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
8
|
+
throw new TypeError('deepGet: Requires object or array');
|
|
9
|
+
if (typeof path !== 'string' ||
|
|
10
|
+
!path.length)
|
|
11
|
+
throw new TypeError('deepGet: Invalid path provided');
|
|
12
|
+
const nodes = [];
|
|
13
|
+
let node = obj;
|
|
14
|
+
let key = '';
|
|
15
|
+
for (let i = 0; i < path.length; i++) {
|
|
16
|
+
const char = path[i];
|
|
17
|
+
switch (char) {
|
|
18
|
+
case '[':
|
|
19
|
+
case ']':
|
|
20
|
+
case '.':
|
|
21
|
+
if (!key)
|
|
22
|
+
break;
|
|
23
|
+
if (Array.isArray(node)) {
|
|
24
|
+
const ix = parseInt(key, 10);
|
|
25
|
+
if (ix < 0 || ix > node.length - 1)
|
|
26
|
+
return undefined;
|
|
27
|
+
node = node[ix];
|
|
28
|
+
nodes.push(node);
|
|
29
|
+
}
|
|
30
|
+
else if (typeof node === 'object' && node !== null) {
|
|
31
|
+
node = node[key];
|
|
32
|
+
nodes.push(node);
|
|
33
|
+
if (node === undefined)
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
key = '';
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
key += char;
|
|
43
|
+
break;
|
|
26
44
|
}
|
|
27
|
-
else if (char === '.' && !in_bracket) {
|
|
28
|
-
if (cursor_part) {
|
|
29
|
-
parts.push(cursor_part);
|
|
30
|
-
cursor_part = '';
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
cursor_part += char;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
if (cursor_part)
|
|
38
|
-
parts.push(cursor_part);
|
|
39
|
-
let len = parts.length;
|
|
40
|
-
if (!len || (len === 1 && get_parent))
|
|
41
|
-
return obj;
|
|
42
|
-
if (get_parent) {
|
|
43
|
-
parts.pop();
|
|
44
|
-
len -= 1;
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (ix < 0 || ix > cursor.length - 1)
|
|
46
|
+
if (key) {
|
|
47
|
+
if (Array.isArray(node)) {
|
|
48
|
+
const ix = parseInt(key, 10);
|
|
49
|
+
if (ix < 0 || ix > node.length - 1)
|
|
51
50
|
return undefined;
|
|
52
|
-
|
|
51
|
+
node = node[ix];
|
|
52
|
+
nodes.push(node);
|
|
53
53
|
}
|
|
54
|
-
else if (
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
else if (typeof node === 'object' && node !== null) {
|
|
55
|
+
node = node[key];
|
|
56
|
+
nodes.push(node);
|
|
57
|
+
if (node === undefined)
|
|
57
58
|
return undefined;
|
|
58
59
|
}
|
|
59
60
|
else {
|
|
60
61
|
return undefined;
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
|
-
|
|
64
|
+
if (get_parent)
|
|
65
|
+
nodes.pop();
|
|
66
|
+
return nodes.length ? nodes.pop() : obj;
|
|
64
67
|
}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "name": "@valkyriestudios/utils", "version": "12.
|
|
1
|
+
{ "name": "@valkyriestudios/utils", "version": "12.29.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "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" }
|