<commit_before>require 'corelib/enumerable'

class Enumerator
  include Enumerable

  def self.for(object, method = :each, *args, &block)
    %x{
      var obj = #{allocate};

      obj.object = object;
      obj.size   = block;
      obj.method = method;
      obj.args   = args;

      return obj;
    }

  end

  def initialize(*, &block)
    if block
      @object = Generator.new(&block)
      @method = :each
      @args   = []
      @size   = `arguments[0] || nil`

      if @size
        @size = Opal.coerce_to @size, Integer, :to_int
      end
    else
      @object = `arguments[0]`
      @method = `arguments[1] || "each"`
      @args   = `$slice.call(arguments, 2)`
      @size   = nil
    end
  end

  def each(*args, &block)
    return self if block.nil? && args.empty?

    args = @args + args

    return self.class.new(@object, @method, *args) if block.nil?

    @object.__send__(@method, *args, &block)
  end

  def size
    Proc === @size ? @size.call(*@args) : @size
  end

  def with_index(offset = 0, &block)
    if offset
      offset = Opal.coerce_to offset, Integer, :to_int
    else
      offset = 0
    end

    return enum_for :with_index, offset unless block

    %x{
      var result, index = offset;

      self.$each.$$p = function() {
        var param = #{Opal.destructure(`arguments`)},
            value = block(param, index);

        if (value === $breaker) {
          result = $breaker.$v;
          return $breaker;
        }

        index++;
      }

      self.$each();

      if (result !== undefined) {
        return result;
      }

      return self.object;
    }
  end<commit_msg>Fix Enumerator#with_index<commit_after>require 'corelib/enumerable'

class Enumerator
  include Enumerable

  def self.for(object, method = :each, *args, &block)
    %x{
      var obj = #{allocate};

      obj.object = object;
      obj.size   = block;
      obj.method = method;
      obj.args   = args;

      return obj;
    }

  end

  def initialize(*, &block)
    if block
      @object = Generator.new(&block)
      @method = :each
      @args   = []
      @size   = `arguments[0] || nil`

      if @size
        @size = Opal.coerce_to @size, Integer, :to_int
      end
    else
      @object = `arguments[0]`
      @method = `arguments[1] || "each"`
      @args   = `$slice.call(arguments, 2)`
      @size   = nil
    end
  end

  def each(*args, &block)
    return self if block.nil? && args.empty?

    args = @args + args

    return self.class.new(@object, @method, *args) if block.nil?

    @object.__send__(@method, *args, &block)
  end

  def size
    Proc === @size ? @size.call(*@args) : @size
  end

  def with_index(offset = 0, &block)
    if offset
      offset = Opal.coerce_to offset, Integer, :to_int
    else
      offset = 0
    end

    return enum_for(:with_index, offset){self.size} unless block

    %x{
      var result, index = offset;

      self.$each.$$p = function() {
        var param = #{Opal.destructure(`arguments`)},
            value = block(param, index);

        if (value === $breaker) {
          result = $breaker.$v;
          return $breaker;
        }

        index++;
      }

      self.$each();

      if (result !== undefined) {
        return result;
      }

      return self.object;
    }
  end
{'fraction_non_alphanumeric': 0.13702801461632155, 'fraction_numerical': 0.0036540803897685747, 'mean_word_length': 1.9648014440433212, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3681329', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1064, 'n_words': 279}