rauricoste-immutable v2.0.1
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.2constructor
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 atpathlocation. 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 atpathwithvalue. 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): pushvalueon 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 atpathand removes the first occurence of value. If the data atpathis an object, an error is thrown.resetWith(object): replaces all the data withobject. similar toimmuableObject = object.