Skip to content

Instantly share code, notes, and snippets.

@sandosh
Created May 6, 2011 23:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandosh/959969 to your computer and use it in GitHub Desktop.
Save sandosh/959969 to your computer and use it in GitHub Desktop.
Underscore.js - compact an object just like compact function for array
_.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