<commit_before>/**
 *
 * @param {Collection} dataCollection
 * @param {int} total
 * @param {Document[]} documents
 * @param {object} [aggregations]
 * @param {object} [searchArgs]
 * @param {KuzzleSearchResult} [previous]
 * @constructor
 */
function KuzzleSearchResult (dataCollection, total, documents, aggregations, searchArgs, previous) {
  Object.defineProperties(this, {
    // read-only properties
    dataCollection: {
      value: dataCollection,
    },
    total: {
      value: total,
      enumerable: true
    },
    documents: {
      value: documents,
      enumerable: true
    },
    aggregations: {
      value: aggregations || {},
      enumerable: true
    },
    searchArgs: {
      value: searchArgs || {},
      enumerable: true
    },
    // writable properties
    fetchedDocument: {
      value: documents.length,
      enumerable: true,
      writable: true
    },
    _previous: {
      value: previous || null,
      writable: true
    },
    _next: {
      value: null,
      writable: true
    }
  });

  if (this._previous instanceof KuzzleSearchResult) {
    this._previous._next = this;
    this.fetchedDocument += this._previous.fetchedDocument;
  }

  // promisifying
  if (this.dataCollection.kuzzle.bluebird) {
    return this.dataCollection.kuzzle.bluebird.promisifyAll(this, {
      suffix: 'Promise',
      filter: function (name, func, target, passes) {
        var whitelist = ['previous', 'next'];

        return passes && whitelist.indexOf(name) !== -1;
      }
    });
  }

  return this;
}


/**
 * @param cb
 * @returns {*}
 */
KuzzleSearchResult.prototype.previous = function (cb) {
  cb(null, this._previous);

  return this;
};

/**
 * @param {function} cb
 */
KuzzleSearchResult.prototype.next = function (cb) {
  var
    filters,
    options = Object.assign({}, this.searchArgs.options),
    self = this;

  if (!this._next) {
    // retrieve next results with scroll if original search use it
    if (options.scrollId) {
      if (this.fetchedDocument >= this.total) {
        cb(null, null);
        return;
      }

      // from and size parameters are not valid for a scroll action
      if (options.from) {
        delete options.from;
      }

      if (options.size) {
        delete options.size;
      }

      this.dataCollection.scroll(
        options.scrollId,
        options,
        this.searchArgs.filters || {},
        function(error, newSearchResults) {
          handleNextSearchResults(error, self, newSearchResults, cb);
        }<commit_msg>when from is equal to 0, didn't remove the property properly<commit_after>/**
 *
 * @param {Collection} dataCollection
 * @param {int} total
 * @param {Document[]} documents
 * @param {object} [aggregations]
 * @param {object} [searchArgs]
 * @param {KuzzleSearchResult} [previous]
 * @constructor
 */
function KuzzleSearchResult (dataCollection, total, documents, aggregations, searchArgs, previous) {
  Object.defineProperties(this, {
    // read-only properties
    dataCollection: {
      value: dataCollection,
    },
    total: {
      value: total,
      enumerable: true
    },
    documents: {
      value: documents,
      enumerable: true
    },
    aggregations: {
      value: aggregations || {},
      enumerable: true
    },
    searchArgs: {
      value: searchArgs || {},
      enumerable: true
    },
    // writable properties
    fetchedDocument: {
      value: documents.length,
      enumerable: true,
      writable: true
    },
    _previous: {
      value: previous || null,
      writable: true
    },
    _next: {
      value: null,
      writable: true
    }
  });

  if (this._previous instanceof KuzzleSearchResult) {
    this._previous._next = this;
    this.fetchedDocument += this._previous.fetchedDocument;
  }

  // promisifying
  if (this.dataCollection.kuzzle.bluebird) {
    return this.dataCollection.kuzzle.bluebird.promisifyAll(this, {
      suffix: 'Promise',
      filter: function (name, func, target, passes) {
        var whitelist = ['previous', 'next'];

        return passes && whitelist.indexOf(name) !== -1;
      }
    });
  }

  return this;
}


/**
 * @param cb
 * @returns {*}
 */
KuzzleSearchResult.prototype.previous = function (cb) {
  cb(null, this._previous);

  return this;
};

/**
 * @param {function} cb
 */
KuzzleSearchResult.prototype.next = function (cb) {
  var
    filters,
    options = Object.assign({}, this.searchArgs.options),
    self = this;

  if (!this._next) {
    // retrieve next results with scroll if original search use it
    if (options.scrollId) {
      if (this.fetchedDocument >= this.total) {
        cb(null, null);
        return;
      }

      // from and size parameters are not valid for a scroll action
      if (typeof options.from !== 'undefined') {
        delete options.from;
      }

      if (options.size) {
        delete options.size;
      }

      this.dataCollection.scroll(
        options.scrollId,
        options,
        this.searchArgs.filters || {},
        function(error, newSearchResults) {
          handleNextSearchResults(error, self, newSearchResults, cb);
        }
{'fraction_non_alphanumeric': 0.12460876369327073, 'fraction_numerical': 0.0005868544600938967, 'mean_word_length': 2.473505434782609, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '615590', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1495, 'n_words': 366}