rauricoste-immutable
v2.0.1
Published
Summary ======
Downloads
20
Readme
Summary
rauricoste-immutable
is an immutable data structure that combine
capabilities of both classic javascript Object and Array
Installation
npm install rauricoste-immutable
API
path
Many methods use a path
to access or update fields. A path is similar
to the javascript notation but there are differences :
if the data structure is :
{
value: 1,
subObject: {
a: 1,
b: "plop"
},
arr: [1, 2, 3]
}
you can access data using these paths :
value
subObject.a
subObject.b
arr.0
arr.1
arr.2
constructor
const Immutable = require("rauricoste-immutable")
const object = new Immutable()
// or
const object = Immutable.fromObject({
value: 1,
subObject: {
a: 1,
b: "plop"
},
arr: [1, 2, 3]
})
read methods
get(path)
: returns the data atpath
location. similar toimmuableObject.key
.toObject()
: returns a vanilla js object corresponding to the data structure. Warning : this method can be time consuming.
write methods (returns a new object since it is immutable)
set(path, value)
: replaces the value of the data atpath
withvalue
. similar toimmuableObject.key = value
.delete(path)
: deletes the value atpath
. If the data at path does not exists, returns the same object. similar todelete immuableObject.key
.push(path, value)
: pushvalue
on the array atpath
. If there is no data atpath
, the array is created. Otherwise, if the data is an object, an error is thrown.remove(path, value)
: iterates over the array atpath
and removes the first occurence of value. If the data atpath
is an object, an error is thrown.resetWith(object)
: replaces all the data withobject
. similar toimmuableObject = object
.