Skip to content

Instantly share code, notes, and snippets.

@lislon
Created February 6, 2015 15:12
Show Gist options
  • Select an option

  • Save lislon/81f2bff49ef37328e4c4 to your computer and use it in GitHub Desktop.

Select an option

Save lislon/81f2bff49ef37328e4c4 to your computer and use it in GitHub Desktop.
/**
* Safely access to a variable value using dot notation 'loan.city.name'
*
* @param {Object} locals Temlate variables
* @param {String} dotPath Path to a variable, for ex. 'loan.city.name'
* @param {String} defValue Fallback value
* @return {Mixed}
*/
safeVar: function(locals, dotPath, defValue) {
var value = _.reduce(dotPath.split('.'), function(res, token) {
if (res === undefined || res === null) {
return undefined
}
return res[token]
}, locals)
return value === undefined ? defValue : value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment