Skip to content

Instantly share code, notes, and snippets.

@isochronous
Last active December 20, 2015 12:59
Show Gist options
  • Save isochronous/6134976 to your computer and use it in GitHub Desktop.
Save isochronous/6134976 to your computer and use it in GitHub Desktop.
convert a flat object with namespaced keys to a heirarchical object
/**
* Converts a "flat" object with namespaced keys into a hierarchical object
* @param {object} flat - A "flat" object with namespaced keys to turn into a hierarchical object
* @returns object - the hierarchical object
*/
var _paths2obj = function (flat) {
var root = {},
keys = _.keys(flat),
parts, entry, currentPart, pointer, i, il;
for (entry in keys) {
parts = entry.split('.');
pointer = root;
for (i=0, il=parts.length; i < il; i++) {
currentPart = parts[i];
pointer[currentPart] = pointer[currentPart] || {};
pointer = pointer[currentPart];
}
pointer = flat[entry];
}
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment