obus
v6.0.0
Published
deep access to object properties
Downloads
1,818
Readme
obus
obus
is deep object accessor
Usage
var Obus = require('obus');
var obus = new Obus();
obus.set('a.b.c', 42);
API
Obus new Obus()
Creates new Obus
object
var obus = new Obus();
* obus.set(String path, * data)
Puts the given object deep into object according to given path
var obus = new Obus();
obus.set('a.b.c', 42);
obus.valueOf(); // -> {a: {b: {c: 42}}}
* obus.get(String path[, * defaultValue])
Returns the value placed deep in object according to given path. Returns the second argument if returning value is undefined
var obus = new Obus();
obus.set('a.b.c', 42);
obus.get('a.b'); // -> {c: 42}
obus.get('a.b.c'); // -> 42
obus.get('a.b.c.d'); // -> undefined
obus.get('a.b.c.d', 146); // -> 146
Boolean obus.del(String path)
Deletes data from given path. Returns true if the data was deleted else false
var obus = new Obus();
obus.set('a', 42);
obus.del('a'); // -> true
obus.del('a'); // -> false
Boolean obus.has(String path)
Checks if any truey (not undefined
) data placed by the given path
var obus = new Obus();
obus.set('a.b', 42);
obus.has('a.b'); // -> true
obus.has('a'); // -> true
obus.has('a.b.c'); // -> false
* obus.add(String path, * data)
Extends existing data with the given value
var obus = new Obus();
obus.set('a.b', 42);
obus.add('a', {c: 42});
obus.valueOf(); // -> {a: {b: 42, c: 42}}
Also
You can use brace accessors if the properties you want is not identifiers.
var obus = new Obus();
obus.set('foo.bar-baz', 42); // error
// Obus supports string literals
obus.set('foo["bar-baz"]', 42); // done
obus.set("foo['bar-baz']", 42); // done
LICENSE MIT