Last active
January 25, 2016 16:23
-
-
Save mgtitimoli/1ce97cfc59d318ea1125 to your computer and use it in GitHub Desktop.
Module extend
This file contains 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
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); | |
} |
This file contains 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
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