src/lib/common.js
function doUpdate(data, filter) {
  console.log('update called', data, filter);

  var i, keys = false;

  if (data instanceof Array) {
    if (typeof data[0] === 'undefined') {
      return Promise.reject(new Error('Can\'t have undefined keys'));
    }
    // Check if a key is the first element
    if (keyTypes.indexOf(typeof data[0]) !== -1) {
      // Check for an even amount of elements
      if (data.length % 2) {
        return Promise.reject(new Error('uneven number of key/values given to update'));
      }

      // Check every second item are keys
      for (i = 2; i < data.length; i = i + 2) {
        if (keyTypes.indexOf(typeof data[i]) === -1) {
          return Promise.reject(new Error('Invalid key value for key ' + ((i/2) + 1)
              + ' (' + typeof data[i] + ' given)'));
        }
      }

      keys = true;
    } else {
      if (!this.options.id) {
        return Promise.reject(new Error('Can\'t give an array of data objects if not key has been specified'));
      }

      // Check if each object value has or doesn't have an id
      for (i = 0; i < data.length; i++) {
        if (typeof data[i][this.options.id] === 'undefined') {
          return Promise.reject(new Error('Can\'t have objects in array without a key'));
        }
      }
    }

    // Check if given a filter object
    if (typeof filter === 'object') {
      return Promise.reject(new Error('can\'t use a filter with an array keyed objects'));
    }

    return this.save.call(this, {
      keys: keys,
      replace: filter === true ? true : undefined,
      data: data
    });
  } else if(data instanceof Object) {
    if (this.options.id && typeof data[this.options.id] !== 'undefined') {
      return Promise.reject(new Error('Can\'t include key value when updating with object'));
    }

    if (typeof filter === 'boolean') {
      // Object of key value pairs to put into database
      return this.save.call(this, {
        keys: true,
        replace: filter ? true : undefined,
        data: data
      });
    } else if (typeof filter === 'object') {
      // Object of fields to update in object values selected with a filter
      return this.save.call(this, {
        data: data,
        filter: filter
      });
    } else {
      // Object of fields to update in all object values
      return this.save.call(this, {
        data: data
      });
    }
  } else {
    return Promise.reject(new Error('Unknown data type given'));
  }
}
function doUpdate(data, filter) {
  console.log('update called', data, filter);

  var i, keys = false;

  if (data instanceof Array) {
    if (typeof data[0] === 'undefined') {
      return Promise.reject(new Error('Can\'t have undefined keys'));
    }
    // Check if a key is the first element
    if (keyTypes.indexOf(typeof data[0]) !== -1) {
      // Check for an even amount of elements
      if (data.length % 2) {
        return Promise.reject(new Error('uneven number of key/values given to update'));
      }

      // Check every second item are keys
      for (i = 2; i < data.length; i = i + 2) {
        if (keyTypes.indexOf(typeof data[i]) === -1) {
          return Promise.reject(new Error('Invalid key value for key ' + ((i/2) + 1)
              + ' (' + typeof data[i] + ' given)'));
        }
      }

      keys = true;
    } else {
      if (!this.options.id) {
        return Promise.reject(new Error('Can\'t give an array of data objects if not key has been specified'));
      }

      // Check if each object value has or doesn't have an id
      for (i = 0; i < data.length; i++) {
        if (typeof data[i][this.options.id] === 'undefined') {
          return Promise.reject(new Error('Can\'t have objects in array without a key'));
        }
      }
    }

    // Check if given a filter object
    if (typeof filter === 'object') {
      return Promise.reject(new Error('can\'t use a filter with an array keyed objects'));
    }

    return this.save.call(this, {
      keys: keys,
      replace: filter === true ? true : undefined,
      data: data
    });
  } else if(data instanceof Object) {
    if (this.options.id && typeof data[this.options.id] !== 'undefined') {
      return Promise.reject(new Error('Can\'t include key value when updating with object'));
    }

    if (typeof filter === 'boolean') {
      // Object of key value pairs to put into database
      return this.save.call(this, {
        keys: true,
        replace: filter ? true : undefined,
        data: data
      });
    } else if (typeof filter === 'object') {
      // Object of fields to update in object values selected with a filter
      return this.save.call(this, {
        data: data,
        filter: filter
      });
    } else {
      // Object of fields to update in all object values
      return this.save.call(this, {
        data: data
      });
    }
  } else {
    return Promise.reject(new Error('Unknown data type given'));
  }
}
function doUpdate ( data , filter ) { console . log ( 'update called' , data , filter ) ; var i , keys = false ; if ( data instanceof Array ) { if ( typeof data [ 0 ] === 'undefined' ) { return Promise . reject ( new Error ( 'Can\'t have undefined keys' ) ) ; } // Check if a key is the first element if ( keyTypes . indexOf ( typeof data [ 0 ] ) !== - 1 ) { // Check for an even amount of elements if ( data . length % 2 ) { return Promise . reject ( new Error ( 'uneven number of key/values given to update' ) ) ; } // Check every second item are keys for ( i = 2 ; i < data . length ; i = i + 2 ) { if ( keyTypes . indexOf ( typeof data [ i ] ) === - 1 ) { return Promise . reject ( new Error ( 'Invalid key value for key ' + ( ( i / 2 ) + 1 ) + ' (' + typeof data [ i ] + ' given)' ) ) ; } } keys = true ; } else { if ( ! this . options . id ) { return Promise . reject ( new Error ( 'Can\'t give an array of data objects if not key has been specified' ) ) ; } // Check if each object value has or doesn't have an id for ( i = 0 ; i < data . length ; i ++ ) { if ( typeof data [ i ] [ this . options . id ] === 'undefined' ) { return Promise . reject ( new Error ( 'Can\'t have objects in array without a key' ) ) ; } } } // Check if given a filter object if ( typeof filter === 'object' ) { return Promise . reject ( new Error ( 'can\'t use a filter with an array keyed objects' ) ) ; } return this . save . call ( this , { keys : keys , replace : filter === true ? true : undefined , data : data } ) ; } else if ( data instanceof Object ) { if ( this . options . id && typeof data [ this . options . id ] !== 'undefined' ) { return Promise . reject ( new Error ( 'Can\'t include key value when updating with object' ) ) ; } if ( typeof filter === 'boolean' ) { // Object of key value pairs to put into database return this . save . call ( this , { keys : true , replace : filter ? true : undefined , data : data } ) ; } else if ( typeof filter === 'object' ) { // Object of fields to update in object values selected with a filter return this . save . call ( this , { data : data , filter : filter } ) ; } else { // Object of fields to update in all object values return this . save . call ( this , { data : data } ) ; } } else { return Promise . reject ( new Error ( 'Unknown data type given' ) ) ; } }
Updates data in the JSON database. Data can either be given as
key-value parameter pairs, OR if the key field has been specified Object
values. New values will be merge into any existing values.

@param {Object|Object[]|Array} data Either:
- an array of key-value pairs,
- an object containing the data to update
- object value(s) containing the key value (if a key has been specified)
@param {Object|true} [filter] If a object containing the data to update, a
filter to select the items that should be updated, or if object value(s)
have been given, true if the existing items should be replaced instead of
merged into

@returns {Promise} A promise that will resolve with an array containing
keys of the updated data.
Updates data in the JSON database . Data can either be given as key - value parameter pairs OR if the key field has been specified Object values . New values will be merge into any existing values .