Created
July 7, 2016 05:38
-
-
Save craigrbruce/c18a1ef69f9d1f07754ab580bac974c7 to your computer and use it in GitHub Desktop.
Object property dirty checker with dependency on lodash
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
/** | |
* Checks all fields of an object literal for values. | |
* @param {Object} object - A POJO. | |
* @param {Array<String>} omitProperies - An array of properties that you wish to omit from the checker e.g. "id". | |
* @returns {Boolean} | |
*/ | |
function isObjectDirty(object, omitProperties) { | |
if(omitProperties) { | |
object = _.omit(object, omitProperties); | |
} | |
return _.some(_.values(object), (value) => { | |
if (_.isArray(value)) { | |
return _.some(value, (item) => isObjectDirty(item, omitProperties)); | |
} | |
if (_.isPlainObject(value)) { | |
return isObjectDirty(value, omitProperties); | |
} | |
return !!value | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
For learning purposes.. what's the diff between using your method VS simply stringifying two objects and use the === operator in js ?
Thank u in advance :)