rauricoste-mutable v1.0.2
Summary
This projects aims to provide methods to modify a deep object easily
Installation
npm install --save rauricoste-mutableUsage
new Mutator().set("a", 1)
.set("b.c", "deep")
.push("d", "arr1")
.push("d", "arr2")
.delete("a")
.toObject()
/*
returns :
{
b: {
c: "deep"
},
d: ["arr1", "arr2"]
}
*/Constructor
new Mutator(object or empty): will initialize the state of the object withobject. If empty, the initial state is{}
API
path : a path is a string representing a data you want to access
or modify. If you want to access a specific item in an array,
use its index.
Exemple :
var object = {
arr: [
1,
2,
{
att: "value"
}
]
}
new Mutator(object).get("arr.2.att") // returns "value"get(path): returns the value associated with the path. Returnsundefinedif the path is invalid.set(path, value): will set thevalueat the specidiedpath. Required parents objects will be created if they do not exist.push(path, value): will add thevalueto the array located at thepath. Data located at thepathis assumed to be an array. If not, an error will be thrown. Required parents objects will be created if they do not exist.delete(path): will delete the data at the specifiedpath. Do nothing if thepathis invalid.