safe-get-nested
v1.0.1
Published
A simple utility to take nested object value with no error
Readme
safe-get-nested
A minimalist utility to fetch values from deeply nested objects without the risk of throwing Cannot read property of undefined error.
install
npm i safe-get-nestedUsage
Supports both ES Modules (import) and CommonJs (require).
1. ESM (Modern)
import { safeGet } from 'safe-get-nested';
const user = { profile: { address: { city: 'San Francisco' } } };
console.log(safeGet(user, 'profile.address.city')); // 'San Francisco'2. CommonJS (Legacy)
const safeGet = require('safe-get-nested');
const user = { profile: null };
console.log(safeGet(user, 'profile.address.city')); // undefined (No Error!)Quick test / Examples
const data = {
a: {
b: {
c: 42
}
}
};
// Valid path
safeGet(data, 'a.b.c'); // Result: 42 ✅
// Broken path
safeGet(data, 'a.z.c'); // Result: undefined ✅
// Null middle-man
const nullData = { a: null };
safeGet(nullData, 'a.b'); // Result: undefined ✅Why? 🤔
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.
