safe-get-nested 1.0.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 ADDED
@@ -0,0 +1,54 @@
1
+ # safe-get-nested
2
+
3
+ A minimalist utility to fetch values from deeply nested objects without the risk of throwing `Cannot read property of undefined` error.
4
+
5
+ ## install
6
+
7
+ ```bash
8
+ npm i safe-get-nested
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Supports both [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) (import) and [CommonJs](https://en.wikipedia.org/wiki/CommonJS) (require).
14
+
15
+ ### 1. ESM (Modern)
16
+ ```javascript
17
+ import { safeGet } from 'safe-get-nested';
18
+
19
+ const user = { profile: { address: { city: 'San Francisco' } } };
20
+ console.log(safeGet(user, 'profile.address.city')); // 'San Francisco'
21
+ ```
22
+
23
+ ### 2. CommonJS (Legacy)
24
+ ```javascript
25
+ const safeGet = require('safe-get-nested');
26
+
27
+ const user = { profile: null };
28
+ console.log(safeGet(user, 'profile.address.city')); // undefined (No Error!)
29
+ ```
30
+
31
+ ## Quick test / Examples
32
+ ```
33
+ const data = {
34
+ a: {
35
+ b: {
36
+ c: 42
37
+ }
38
+ }
39
+ };
40
+
41
+ // Valid path
42
+ safeGet(data, 'a.b.c'); // Result: 42 ✅
43
+
44
+ // Broken path
45
+ safeGet(data, 'a.z.c'); // Result: undefined ✅
46
+
47
+ // Null middle-man
48
+ const nullData = { a: null };
49
+ safeGet(nullData, 'a.b'); // Result: undefined ✅
50
+ ```
51
+
52
+ ## Why? 🤔
53
+
54
+ Standard JS throws errors when accessing properties of `null` or `undefined`. This utility acts as a safe **Parsing** layer that traverses the object tree and returns `undefined` as soon as it hits a dead end.
package/index.esm.js ADDED
@@ -0,0 +1,11 @@
1
+ export function safeGet(obj, path) {
2
+ const keys = path.split('.');
3
+ let current = obj;
4
+ for (let i = 0; i < keys.length; i++) {
5
+ if (current === null || typeof(current) !== 'object')
6
+ return undefined;
7
+ current = current[keys[i]];
8
+ }
9
+
10
+ return current;
11
+ }
package/index.js ADDED
@@ -0,0 +1,13 @@
1
+ function safeGet(obj, path) {
2
+ const keys = path.split('.');
3
+ let current = obj;
4
+ for (let i = 0; i < keys.length; i++) {
5
+ if (current === null || typeof(current) !== 'object')
6
+ return undefined;
7
+ current = current[keys[i]];
8
+ }
9
+
10
+ return current;
11
+ }
12
+
13
+ module.exports = safeGet;
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "safe-get-nested",
3
+ "version": "1.0.0",
4
+ "description": "A simple utility to take nested object value with no error",
5
+ "main": "index.js",
6
+ "module": "index.esm.js",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/John-fried/safe-get-nested.git"
13
+ },
14
+ "keywords": ["util", "simple", "utility", "tools"],
15
+ "author": "john-fried",
16
+ "license": "MIT"
17
+ }