secure-nested
v1.0.1
Published
Safe nested object access utility for JavaScript/TypeScript
Downloads
20
Maintainers
Readme
secure-nested 🛡️
Safe nested object access utility for JavaScript/TypeScript. Never crash from Cannot read property of undefined again.
✨ Features
- Simple: One-liner API
- Fast: Minimal overhead
- Safe: Automatic null/undefined checks
- Flexible: String or array path support
- Zero dependencies
- Universal: Works in Node.js and browsers
- TypeScript support: Full type definitions
📦 Install
npm install secure-nested🚀 Usage
import safeGet from "secure-nested";
const user = {
profile: {
name: "Ali",
settings: { theme: "dark" },
},
};
// Safe access
safeGet(user, "profile.name", "Guest"); // 'Ali'
safeGet(user, "profile.age", 0); // 0 (default value)Array Access
const data = {
users: [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
],
};
safeGet(data, "users.0.name"); // 'Alice'
safeGet(data, "users.3.name", "Unknown"); // 'Unknown'Array Path Format
const obj = { a: { b: { c: "value" } } };
safeGet(obj, "a.b.c"); // 'value'
safeGet(obj, ["a", "b", "c"]); // 'value'🎨 Other Functions
safeSet - Safe assignment
import { safeSet } from "secure-nested";
const obj = {};
safeSet(obj, "user.profile.name", "Ali");
safeSet(obj, "items.0.value", "test");safeDelete - Safe deletion
import { safeDelete } from "secure-nested";
const obj = { a: { b: "value", c: "keep" } };
safeDelete(obj, "a.b");safeHas - Property check
import { safeHas } from "secure-nested";
const obj = { user: { id: 1, name: "Ali" } };
safeHas(obj, "user.name"); // true
safeHas(obj, "user.email"); // false📚 API
safeGet(obj, path, defaultValue)
obj- Object to accesspath- Property path (string or array)defaultValue- Default value if not found- Returns: Found value or default
safeSet(obj, path, value, createMissing)
obj- Object to modifypath- Property pathvalue- Value to setcreateMissing- Create missing objects (default: true)- Returns: Success boolean
safeDelete(obj, path)
obj- Object to modifypath- Property path- Returns: Success boolean
safeHas(obj, path)
obj- Object to checkpath- Property path- Returns: Exists boolean
🤝 Contributing
Contributions, issues and feature requests are welcome! Feel free to check issues page.
📄 License
MIT
⭐ Show Your Support
Give a ⭐ if this project helped you!
