Created
May 6, 2011 23:06
-
-
Save sandosh/959969 to your computer and use it in GitHub Desktop.
Underscore.js - compact an object just like compact function for array
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({ | |
capitalize : function(string) { | |
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase(); | |
}, | |
compactObject: function(to_clean) { | |
_.map(to_clean, function(value, key, to_clean) { | |
if (_.isNull(value) || _.isUndefined(value) || (_.isString(value) && _.trim(value).length === 0) || (_.isBoolean(value) && value === false)) { | |
delete to_clean[key]; | |
} | |
}); | |
return to_clean; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment