(MAINTAINED Version) (Works on 0.6 - 0.11) Previous version not tested but should work
- note: After Dotty fell into the hole of forgotten projects, I have uploaded this module. I will work to get the current pull requests merged into my branch as well as improve any existing functionality as best I can without breaking backwards compatibility with Dotty.
Dotty (Maintained as "dotty-bindable" on NPM)
Access properties of nested objects using dot-path notation.
Overview
Dotty makes it easy to programmatically access arbitrarily nested objects and
their properties.
Installation
Here's a link to the npm page.
npm install dotty
Usage
Also see the documentation and
example.
var dotty = require("dotty");
var object = {
a: {
b: {
x: "y",
},
c: {
x: "z",
},
},
};
console.log(dotty.exists(object, "a.b.x"));
console.log(dotty.exists(object, ["a", "b", "x"]));
console.log(dotty.exists(object, "a.b.z"));
console.log(dotty.exists(object, ["a", "b", "z"]));
console.log(dotty.get(object, "a.b.x"));
console.log(dotty.get(object, ["a", "b", "x"]));
console.log(dotty.get(object, "a.b.z"));
console.log(dotty.get(object, ["a", "b", "z"]));
dotty.put(object, "a.b.hello", "hi");
dotty.put(object, ["a", "c", "yo"], "sup");
console.log(dotty.search(object, "a.b.*"));
console.log(dotty.search(object, ["a", "b", "*"]));
console.log(dotty.search(object, "a.*.x"));
console.log(dotty.search(object, ["a", "*", "x"]));
console.log(dotty.search(object, ["a", "*", /..+/]));
console.log(dotty.remove(object, "a.b.x"));
console.log(dotty.remove(object, "a.b.y"));
console.log(dotty.deepKeys(object));
console.log(dotty.deepKeys(object, {leavesOnly: true}));
console.log(dotty.deepKeys(object, {leavesOnly: true, asStrings: true}));
console.log(object);
Binding and Currying
Note please read below for the one Caveat of this new feature, which affects put.
var obj =
{
user: {
name: {
primary: "John",
surname: "Doe"
},
age: 26,
location: {
city: "New York",
region: "New York",
region_abbrev: "NY",
}
}
}
var user = dotty.get.bind(null, obj, ['user']);
var name
name = user.bind(null, ['name']);
console.log('Curried from user object')
console.log(name('primary'));
console.log('Curried from top level object');
name = dotty.get.bind(null, obj, ['user'], 'name');
console.log(name('primary'));
console.log(name('surname'));
console.log('Accessed via return name as an object and just accessing its properties.');
console.log(name().primary);
console.log(name().surname);
console.log('Let try make a function to dynamically return the city even if it is changed from somewhere else in the program');
console.log('Curry from top level object');
var city = dotty.get.bind(null, obj)
.bind(null, 'user.location')
.bind(null, 'city');
console.log('My city is ' + city() );
console.log('someones changing my city');
obj.user.location.city = 'Albany';
console.log('My city is now ' + city() );
console.log('Now showing PUT caveat');
obj1 = {a: { b: {c: {d: { e: 1}}}}}
var ans = dotty.get(obj1, 'a', 'b', 'c', ['d', 'e']);
console.log(ans);
obj1['a']['b']['c']['d']['e'];
dotty.put(obj1, 'a', 'b', 'c', ['d', 'e']);
console.log(dotty.get(obj1, 'a', 'b', 'c'));
obj1['a']['b']['c'] = ['d', 'e'];
License
3-clause BSD. A copy is included with the source.
Contact