accessors

Read and update (nested) objects using simple patterns.

accessors

Read and update (nested) objects using simple patterns.

examples

var Accessors = require("accessors");

var obj = {foo: {bar: "value"}};
var acc = Accessors(obj);

console.log(acc.get("foo.bar"));
console.log(acc.get("foo.bar2", "default"));

// You don't actually need an Accessors instance.
console.log(Accessors.get(obj, "foo.bar"));
console.log(Accessors.get(obj, "foo.bar2"));

output:

$ node examples/acc-get.js
value
default
value
undefined
var Accessors = require("accessors");

var obj = {};
var acc = Accessors(obj);

acc.set("foo.bar", "value");
console.log(acc.get("foo.bar"));

acc.set("foo.baz[]", "value1");
acc.set("foo.baz[]", "value2");

// You don't actually need an Accessors instance.
Accessors.set(obj, "foo.baz[]", "value3");

console.log(acc.get("foo.baz"));

output:

$ node examples/acc-set.js
value
[ 'value1', 'value2', 'value3' ]

methods

Creates an Accessor instance with an object to operate on.

Returns the internal object's value at the given key if found, else returns defaultValue.

Sets the internal object's value to value at the given key and overrides it if it already exists.

Returns the given object's value at the given key if found, else returns defaultValue.

Sets the given object's value to value at the given key and overrides it if it already exists.

install

With npm do:

npm install accessors

license

MIT