<commit_before>/*jshint browserify: true */
"use strict";

var noop = require('./util/noop'),
  extend = require('extend'),
  ArangoError = require('./error'),
  all = require('./util/all');

module.exports = ArrayCursor;

function ArrayCursor(connection, body) {
  this.extra = body.extra;
  this._connection = connection;
  this._result = body.result;
  this._hasMore = Boolean(body.hasMore);
  this._id = body.id;
  this._current = 0;
}

extend(ArrayCursor.prototype, {
  _drain: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    self._more(function (err) {
      if (err) callback(err);
      else if (!self._hasMore) callback(null, self);
      else self._drain(callback);
    });
  },
  _more: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    if (!self._hasMore) callback(null, self);
    else {
      self._connection.put('cursor/' + this._id, function (err, body) {
        if (err) callback(err);
        else {
          self._result.push.apply(self._result, body.result);
          self._hasMore = body.hasMore;
          callback(null, self);
        }
      });
    }
  },
  all: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    self._drain(function (err) {
      if (err) callback(err);
      else callback(null, self._result);
    });
  },
  next: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    function next() {
      var value = self._result[self._current];
      self._current += 1;
      callback(null, value);
    }
    if (self._current < self._result.length) next();
    else {
      if (!self._hasMore) callback(null);
      else {
        self._more(function (err) {
          if (err) callback(err);
          else next();
        });
      }
    }
  },
  hasNext: function () {
    return (this._hasMore || this._current < this._result.length);
  },
  each: function (fn, callback) {
    if (!callback) callback = noop;
    var self = this;
    self._drain(function (err) {
      if (err) callback(err);
      else {
        try {
          var i, result;
          for (i = 0; i < self._result.length; i++) {
            result = fn(self._result[i], i, self);
            if (result === false) break;
          }
          callback(null, self);
        }
        catch (e) {callback(e);}
      }
    });
  },
  every: function (fn, callback) {
    if (!callback) callback = noop;
    var self = this;
    function step(x) {
      try {
        var i, result = true;
        for (i = x; i < self._result.length; i++) {
          result = fn(self._result[i], i, self);
          if (!result) break;
        }
        if (!self._hasMore || !result) callback(null, result);
        else {
          self._more(function (err) {
            if (err) callback(err);
            else step(i);
          });
        }
      }
      catch(e) {callback(e);}
    }
    step(0);
  },<commit_msg>Don't return self in cursor.each.<commit_after>/*jshint browserify: true */
"use strict";

var noop = require('./util/noop'),
  extend = require('extend'),
  ArangoError = require('./error'),
  all = require('./util/all');

module.exports = ArrayCursor;

function ArrayCursor(connection, body) {
  this.extra = body.extra;
  this._connection = connection;
  this._result = body.result;
  this._hasMore = Boolean(body.hasMore);
  this._id = body.id;
  this._current = 0;
}

extend(ArrayCursor.prototype, {
  _drain: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    self._more(function (err) {
      if (err) callback(err);
      else if (!self._hasMore) callback(null, self);
      else self._drain(callback);
    });
  },
  _more: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    if (!self._hasMore) callback(null, self);
    else {
      self._connection.put('cursor/' + this._id, function (err, body) {
        if (err) callback(err);
        else {
          self._result.push.apply(self._result, body.result);
          self._hasMore = body.hasMore;
          callback(null, self);
        }
      });
    }
  },
  all: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    self._drain(function (err) {
      if (err) callback(err);
      else callback(null, self._result);
    });
  },
  next: function (callback) {
    if (!callback) callback = noop;
    var self = this;
    function next() {
      var value = self._result[self._current];
      self._current += 1;
      callback(null, value);
    }
    if (self._current < self._result.length) next();
    else {
      if (!self._hasMore) callback(null);
      else {
        self._more(function (err) {
          if (err) callback(err);
          else next();
        });
      }
    }
  },
  hasNext: function () {
    return (this._hasMore || this._current < this._result.length);
  },
  each: function (fn, callback) {
    if (!callback) callback = noop;
    var self = this;
    self._drain(function (err) {
      if (err) callback(err);
      else {
        try {
          var i, result;
          for (i = 0; i < self._result.length; i++) {
            result = fn(self._result[i], i, self);
            if (result === false) break;
          }
          callback(null);
        }
        catch (e) {callback(e);}
      }
    });
  },
  every: function (fn, callback) {
    if (!callback) callback = noop;
    var self = this;
    function step(x) {
      try {
        var i, result = true;
        for (i = x; i < self._result.length; i++) {
          result = fn(self._result[i], i, self);
          if (!result) break;
        }
        if (!self._hasMore || !result) callback(null, result);
        else {
          self._more(function (err) {
            if (err) callback(err);
            else step(i);
          });
        }
      }
      catch(e) {callback(e);}
    }
    step(0);
  },
{'fraction_non_alphanumeric': 0.15340331865899084, 'fraction_numerical': 0.0013545546901456147, 'mean_word_length': 2.0765625, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '1400795', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1865, 'n_words': 463}