Created
February 6, 2015 15:12
-
-
Save lislon/81f2bff49ef37328e4c4 to your computer and use it in GitHub Desktop.
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
| /** | |
| * 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