Build objects tree from string with properties separating by dot.
// dependencies
var assert = require('assert');
var builder = require('./builder');
// initialization
var built = builder.build('path.to.create');
assert.ok(built);
assert.ok(built.path);
assert.ok(built.path.to);
assert.equal(typeof built, 'object');
assert.equal(typeof built.path, 'object');
assert.equal(typeof built.path.to, 'object');
assert.equal(typeof built.path.to.create, 'string');
assert.equal(built.path.to.create, 'path.to.create');
module.exports = {
construct: _construct
};
function _construct(path) {
var parts = path.split('.');
var result = {};
var last = parts.slice(0, parts.length - 1).reduce(function (prev, part) {
prev[part] = {};
return prev[part];
}, result);
last[parts.slice(-1).pop()] = path;
return result;
}