@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 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 val - Object/Array to get the value from
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('Deepget is only supported for objects');
9
- if (typeof path !== 'string')
10
- throw new TypeError('No path was given');
11
- const path_s = path.trim();
12
- const path_len = path_s.length;
13
- if (!path_len)
14
- throw new TypeError('No path was given');
15
- const parts = [];
16
- let cursor_part = '';
17
- let in_bracket = false;
18
- for (let i = 0; i < path_len; i++) {
19
- const char = path_s[i];
20
- if (char === '[' || char === ']') {
21
- in_bracket = !in_bracket;
22
- if (cursor_part) {
23
- parts.push(cursor_part);
24
- cursor_part = '';
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
- let cursor = obj;
47
- for (let i = 0; i < len; i++) {
48
- if (Array.isArray(cursor)) {
49
- const ix = parseInt(parts[i], 10);
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
- cursor = cursor[ix];
51
+ node = node[ix];
52
+ nodes.push(node);
53
53
  }
54
- else if (Object.prototype.toString.call(cursor) === '[object Object]') {
55
- cursor = cursor[parts[i]];
56
- if (cursor === undefined)
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
- return cursor;
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.28.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" }
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" }