Last active
March 19, 2018 06:15
-
-
Save agnel/0199e34eb37c659e6ff23dbf056989a5 to your computer and use it in GitHub Desktop.
A collection of _.js mixins
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
_.mixin({ | |
isHash: function(object) { | |
return _.isObject(object) && !_.isArray(object); | |
}, | |
capitalize: function(string) { | |
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase(); | |
}, | |
deepMerge: function(hash1, hash2){ | |
var hash = hash1; | |
_.each(hash2, function(value, key){ | |
if( value instanceof Object && hash[key] instanceof Object){ | |
hash[key] = _.deepMerge(hash[key], value) | |
} else { | |
hash[key] = value; | |
} | |
}); | |
return hash; | |
}, | |
nestedErrorHanler: function(obj) { | |
var errorText = ""; | |
_.each(obj, function(error, key) { | |
if(error.constructor == Object){ | |
errorText = errorText + _.nestedErrorHanler(error); | |
} else { | |
errorText = errorText + "<li>" + key.titleize() + ' - ' + error + "</li>"; | |
} | |
}); | |
return errorText; | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment