Skip to content

Instantly share code, notes, and snippets.

@VanDalkvist
Created August 9, 2017 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VanDalkvist/1a198922762fda016fd9d610ecf9d787 to your computer and use it in GitHub Desktop.
Save VanDalkvist/1a198922762fda016fd9d610ecf9d787 to your computer and use it in GitHub Desktop.
Build objects tree from string with properties separating by dot.
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;
}
// 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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment