rauricoste-mutable
v1.0.2
Published
Summary ======
Downloads
1
Readme
Summary
This projects aims to provide methods to modify a deep object easily
Installation
npm install --save rauricoste-mutable
Usage
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. Returnsundefined
if the path is invalid.set(path, value)
: will set thevalue
at the specidiedpath
. Required parents objects will be created if they do not exist.push(path, value)
: will add thevalue
to the array located at thepath
. Data located at thepath
is 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 thepath
is invalid.