objeto
v1.0.0
Published
Object helper to get, set, delete and check nested properties without risk.
Downloads
4
Maintainers
Readme
🦺 Object helper to get, set, delete and check nested properties without risk.
- 🚀 Lightweight.
- ⚪️ Zero dependencies.
Table of contents
Import
const objeto = require("objeto");
Usage
get
If the targetObject
doesn't contain the path
property, it will return undefined
.
- targetObject
object
: Object to target. - path
string | Array<string>
: Dot path"prop1.prop2"
or array path["prop1", "prop2"]
.
objeto(targetObject).get(path);
set
objeto(targetObject).set(path, value);
delete
const deletedValue = objeto(targetObject).delete(path);
has
Check if the targetObject
has the specified property.
objeto(targetObject).has(path);
isType
Check if a property value type is equal the specified type.
objeto(targetObject).isType(path, type);
Example
let bike = {
wheel1: {
type: "AD-56",
status: "ok"
},
wheel2: {
type: "AT-77",
status: "ok"
},
};
objeto(bike).get("wheel2.type"); // "AT-77"
objeto(bike).set("wheel2.type", "newType");
objeto(bike).delete("wheel2.type"); // "AT-77"
objeto(bike).has("wheel2.type"); // true
objeto(bike).isType("wheel2.type", "string"); // true
objeto(bike).get("wheel2.type.doesntExists"); // undefined
objeto(bike).set("wheel2.type.doesntExists.newProp", "newType"); // new nested property is created
objeto(bike).has("wheel2.type.doesntExists"); // false