Created
August 9, 2017 10:22
-
-
Save VanDalkvist/1a198922762fda016fd9d610ecf9d787 to your computer and use it in GitHub Desktop.
Build objects tree from string with properties separating by dot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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