Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
Last active January 25, 2016 16:23
Show Gist options
  • Save mgtitimoli/1ce97cfc59d318ea1125 to your computer and use it in GitHub Desktop.
Save mgtitimoli/1ce97cfc59d318ea1125 to your computer and use it in GitHub Desktop.
Module extend
function recursiveExtend(dest, path) {
if (typeof path === "string") {
path = path.split(".");
}
function extend(path, curSegmentIndex, curDest) {
if (curSegmentIndex === path.length) {
return curDest;
}
var segment = path[curSegmentIndex];
if (typeof curDest[segment] === "undefined") {
curDest[segment] = {};
}
return extend(path, curSegmentIndex + 1, curDest[segment])
}
return extend(path.slice(1), 0, dest);
}
function extend(dest, path) {
if (typeof path === "string") {
path = path.split(".");
}
return path.slice(1).reduce(function(res, segment) {
if (typeof res[segment] === "undefined") {
res[segment] = {};
}
return res[segment];
}, dest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment