dist/bbn.js
/**
     * Returns an object describing the differences for transforming the first given object into the second.
     * 
     * The returned data will use the objects properties as keys. If unchanged is set to true, all the 
     * properties will be returned, otherwise only the different ones. Each of these keys will have the 
     * following properties: 
     * - type: can be _modified_, _created_, _deleted_, and if unchanged is set to true _unchanged_
     * - data: the first object's property's value, or the second if type is _created_
     * - newData: the second object's property's value in case of type _updated_
     *
     * @method   diffObj
     * @global
     * @example
     * ```javascript
     * bbn.fn.diffObj(
     *   {
     *     name: "Thomas", 
     *     age: 45
     *   }, {
     *     name: "Eva",
     *     sex: "Female",
     *     retired: false
     *   }
     * );
     * // {
     * //   name: {
     * //     type: "updated",
     * //     data: "Thomas",
     * //     newData: "Eva"
     * //   },
     * //   age: {
     * //     type: "deleted",
     * //     data: 45
     * //   },
     * //   sex: {
     * //     type: "created",
     * //     data: "Female"
     * //   },
     * //   retired: {
     * //     type: "created",
     * //     data: false
     * //   }
     * // }
     * ```
     * @example
     * ```javascript
     * bbn.fn.diffObj(
     *   {pupil: "Agnes Varda", grade: {year: "B", month: "A"}},
     *   {pupil: "Luc Besson", grade: {year: "C", month: "D"}}
     * );
     * // {
     * //   "pupil": {
     * //     "type": "updated",
     * //     "data": "Agnes Varda",
     * //     "newData": "Luc Besson"
     * //   },
     * //   "grade": {
     * //     "year": {
     * //       "type": "updated",
     * //       "data": "B",
     * //       "newData": "C"
     * //     },
     * //     "month": {
     * //       "type": "updated",
     * //       "data": "A",
     * //       "newData": "D"
     * //     }
     * //   }
     * // }
     * ```
     * @memberof bbn.fn
     * @param    {Object}  obj1
     * @param    {Object}  obj2
     * @param    {String}  unchanged
     * @param    {Boolean} notRoot
     * @returns  {Object}
     */
Returns an object describing the differences for transforming the first given object into the second.

The returned data will use the objects properties as keys. If unchanged is set to true, all the
properties will be returned, otherwise only the different ones. Each of these keys will have the
following properties:
- type: can be _modified_, _created_, _deleted_, and if unchanged is set to true _unchanged_
- data: the first object's property's value, or the second if type is _created_
- newData: the second object's property's value in case of type _updated_


Returns an object describing the differences for transforming the first given object into the second . The returned data will use the objects properties as keys . If unchanged is set to true all the properties will be returned otherwise only the different ones . Each of these keys will have the following properties : - type : can be _modified_ _created_ _deleted_ and if unchanged is set to true _unchanged_ - data : the first object ' s property ' s value or the second if type is _created_ - newData : the second object ' s property ' s value in case of type _updated_
diffObj(obj1, obj2, unchanged, notRoot){
      let VALUE_CREATED = 'created',
          VALUE_UPDATED = 'updated',
          VALUE_DELETED = 'deleted',
          VALUE_UNCHANGED = 'unchanged',
          _compareValues = function(value1, value2) {
            if (value1 === value2) {
              return VALUE_UNCHANGED;
            }
            if (
              bbn.fn.isDate(value1) &&
              bbn.fn.isDate(value2) &&
              (value1.getTime() === value2.getTime())
            ){
              return VALUE_UNCHANGED;
            }
            if ('undefined' == typeof(value1)) {
              return VALUE_CREATED;
            }
            if ('undefined' == typeof(value2)) {
              return VALUE_DELETED;
            }
            return VALUE_UPDATED;
          };
      if ( notRoot === undefined ){
        notRoot = false;
      }
      let diff = {};
      if ( !bbn.fn.isFunction(obj1) && !bbn.fn.isFunction(obj1) ){
        if (bbn.fn.isValue(obj1) || bbn.fn.isValue(obj2) ){
          let res = _compareValues(obj1, obj2);
          if ( unchanged || (res !== VALUE_UNCHANGED) ){
            let ret = {
              type: _compareValues(obj1, obj2),
              data: (obj1 === undefined) ? obj2 : obj1
            };
            if ( obj1 !== undefined ){
              ret.newData = obj2;
            }
            return ret;
          }
          return false;
        }
        for ( let key in obj1 ){
          if ( bbn.fn.isFunction(obj1[key]) ){
            continue;
          }
          let value2 = undefined;
          if ( 'undefined' != typeof(obj2[key]) ){
            value2 = obj2[key];
          }
          let res = bbn.fn.diffObj(obj1[key], value2, unchanged, true);
          if ( res ){
            diff[key] = res;
          }
        }
        for ( let key in obj2 ){
          if ( bbn.fn.isFunction(obj2[key]) || ('undefined' != typeof(obj1[key])) ){
            continue;
          }
          let res = bbn.fn.diffObj(undefined, obj2[key], unchanged, true);
          if ( res ){
            diff[key] = res;
          }
        }
      }
      return !notRoot || unchanged || bbn.fn.numProperties(diff) ? diff : false;
    }
diffObj ( obj1 , obj2 , unchanged , notRoot ) { let VALUE_CREATED = 'created' , VALUE_UPDATED = 'updated' , VALUE_DELETED = 'deleted' , VALUE_UNCHANGED = 'unchanged' , _compareValues = function ( value1 , value2 ) { if ( value1 === value2 ) { return VALUE_UNCHANGED ; } if ( bbn . fn . isDate ( value1 ) && bbn . fn . isDate ( value2 ) && ( value1 . getTime ( ) === value2 . getTime ( ) ) ) { return VALUE_UNCHANGED ; } if ( 'undefined' == typeof ( value1 ) ) { return VALUE_CREATED ; } if ( 'undefined' == typeof ( value2 ) ) { return VALUE_DELETED ; } return VALUE_UPDATED ; } ; if ( notRoot === undefined ) { notRoot = false ; } let diff = { } ; if ( ! bbn . fn . isFunction ( obj1 ) && ! bbn . fn . isFunction ( obj1 ) ) { if ( bbn . fn . isValue ( obj1 ) || bbn . fn . isValue ( obj2 ) ) { let res = _compareValues ( obj1 , obj2 ) ; if ( unchanged || ( res !== VALUE_UNCHANGED ) ) { let ret = { type : _compareValues ( obj1 , obj2 ) , data : ( obj1 === undefined ) ? obj2 : obj1 } ; if ( obj1 !== undefined ) { ret . newData = obj2 ; } return ret ; } return false ; } for ( let key in obj1 ) { if ( bbn . fn . isFunction ( obj1 [ key ] ) ) { continue ; } let value2 = undefined ; if ( 'undefined' != typeof ( obj2 [ key ] ) ) { value2 = obj2 [ key ] ; } let res = bbn . fn . diffObj ( obj1 [ key ] , value2 , unchanged , true ) ; if ( res ) { diff [ key ] = res ; } } for ( let key in obj2 ) { if ( bbn . fn . isFunction ( obj2 [ key ] ) || ( 'undefined' != typeof ( obj1 [ key ] ) ) ) { continue ; } let res = bbn . fn . diffObj ( undefined , obj2 [ key ] , unchanged , true ) ; if ( res ) { diff [ key ] = res ; } } } return ! notRoot || unchanged || bbn . fn . numProperties ( diff ) ? diff : false ; }
Returns an object describing the differences for transforming the first given object into the second.
Returns an object describing the differences for transforming the first given object into the second .