accessors

Read and update (nested) objects using simple patterns.


Keywords
accessor, pattern, access, object, nested, path
License
MIT
Install
npm install accessors@0.0.2

Documentation

accessors

Read and update (nested) objects using simple patterns.

browser support

build status

examples

get

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

set

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

Accessors(object)

Creates an Accessor instance with an object to operate on.

.get(key, defaultValue)

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

.set(key, value)

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

Accessors.get(object, key, defaultValue)

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

Accessors.set(object, key, value)

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