Skip to content

Instantly share code, notes, and snippets.

@rankun203
Created March 1, 2016 08:58
Show Gist options
  • Save rankun203/be4b4e0e3e5c43a29109 to your computer and use it in GitHub Desktop.
Save rankun203/be4b4e0e3e5c43a29109 to your computer and use it in GitHub Desktop.
Omit empty properties
/**
* Created on 2/9/16.
* @author rankun203
*/
'use strict';
const _ = require('lodash');
/**
* Omit property when empty value
*
* <pre>
const params = oe({
email: where.email,
id : oe({
'$lt': where.maxId
})
});
* </pre>
* @param {object} o the object.
*/
module.exports = function omitEmpty(o) {
return _.omit(o, function (v) {
if (!v) return {};
if (typeof v === 'object') return Object.getOwnPropertyNames(v).length === 0;
return !v;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment