Last active
August 29, 2015 14:01
-
-
Save mogelbrod/ada594dca002108146e2 to your computer and use it in GitHub Desktop.
pathToRef implementation in various JavaScript dialects
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
###* | |
* Helper function which returns a reference to a value in an object hierarchy. | |
* Call with path to parent object if you wish to mutate a value in-place, as | |
* assignment on reference variables will only set the variable in the local scope. | |
* | |
* AngularJS: Available in 'app' module as 'pathToRef' constant. | |
* | |
* @param path {Array | String} Path components | |
* @param root {Object | Array} Root object to start at | |
* @param initIntermediary {Boolean} Initialize and continue on non-existant values (false = bail) | |
* @return {Any | undefined} Reference to the value at path, or undefined if no valid reference was found | |
### | |
pathToRef = (path, root, initIntermediary = false) -> | |
return root unless path? and root? | |
path = path.split '.' if typeof path is 'string' | |
ref = root | |
for key in path | |
unless ref[key]? | |
return undefined unless initIntermediary | |
ref[key] = {} | |
ref = ref[key] | |
return ref | |
# Export as AngularJS / CommonJS module | |
angular?.module('app').constant 'pathToRef', pathToRef | |
module?.exports = pathToRef |
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
/** | |
* Helper function which returns a reference to a value in an object hierarchy. | |
* Call with path to parent object if you wish to mutate a value in-place, as | |
* assignment on reference variables will only set the variable in the local scope. | |
* | |
* AngularJS: Available in 'app' module as 'pathToRef' constant. | |
* | |
* @param path {Array | String} Path components | |
* @param root {Object | Array} Root object to start at | |
* @param initIntermediary {Boolean} Initialize non-existant objects (false = bail) | |
* @return {Any | undefined} Reference to the value at path or undefined if unreachable | |
*/ | |
function pathToRef(path, root, initIntermediary) { | |
if (path == null || root == null) | |
return root; | |
if (initIntermediary == null) | |
initIntermediary = false; | |
if (typeof path === 'string') | |
path = path.split('.'); | |
var ref = root, i = 0, len = path.length; | |
for (; i < len; i++, key = path[i]) { | |
if (ref[key] == null) { | |
if (!initIntermediary) | |
return undefined; | |
ref[key] = {}; | |
} | |
ref = ref[key]; | |
} | |
return ref; | |
}; | |
// Export as AngularJS module | |
if (typeof angular !== "undefined" && angular !== null) | |
angular.module('app').constant('pathToRef', pathToRef); | |
// Export as CommonJS module | |
if (typeof module !== "undefined" && module !== null) | |
module.exports = pathToRef; |
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
/** | |
* Helper function which returns a reference to a value in an object hierarchy. | |
* Call with path to parent object if you wish to mutate a value in-place, as | |
* assignment on reference variables will only set the variable in the local scope. | |
* | |
* AngularJS: Available in 'validator' module as 'pathToRef' constant. | |
* | |
* @param path {Array | String} Path components | |
* @param root {Object | Array} Root object to start at | |
* @param initIntermediary {Boolean} Initialize and continue on non-existant values (false = bail) | |
* @return {Any | undefined} Reference to the value at path, or undefined if no valid reference was found | |
*/ | |
pathToRef = (path, root, initIntermediary = false) -> | |
return root unless path? and root? | |
path = path.split \. if typeof! path is \String | |
ref = root | |
for key in path | |
unless ref[key]? | |
return void unless initIntermediary | |
ref[key] = {} | |
ref = ref[key] | |
return ref | |
# Export as AngularJS / CommonJS module | |
angular?.module(\app).constant \pathToRef, pathToRef | |
module?.exports = pathToRef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment