Skip to content

Instantly share code, notes, and snippets.

@craigrbruce
Created July 7, 2016 05:38
Show Gist options
  • Select an option

  • Save craigrbruce/c18a1ef69f9d1f07754ab580bac974c7 to your computer and use it in GitHub Desktop.

Select an option

Save craigrbruce/c18a1ef69f9d1f07754ab580bac974c7 to your computer and use it in GitHub Desktop.

Revisions

  1. craigrbruce created this gist Jul 7, 2016.
    20 changes: 20 additions & 0 deletions isObjectDirty.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    /**
    * 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
    });
    }