Created
March 1, 2016 08:58
-
-
Save rankun203/be4b4e0e3e5c43a29109 to your computer and use it in GitHub Desktop.
Omit empty properties
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
/** | |
* 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