1.0.2 • Published 8 years ago

rauricoste-mutable v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

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 with object. 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. Returns undefined if the path is invalid.

  • set(path, value) : will set the value at the specidied path. Required parents objects will be created if they do not exist.

  • push(path, value) : will add the value to the array located at the path. Data located at the path 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 specified path. Do nothing if the path is invalid.

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago