<commit_before>var escape     = require('escape-string-regexp')
var forceArray = require('force-array')
var stream     = require('stream')
var strip      = require('strip-ansi')
var util       = require('util')


function SupposeStream(options) {
  if(!(this instanceof SupposeStream)) return new SupposeStream(options)

  SupposeStream.super_.call(this)


  var debug
  var stripAnsi
  if(options)
  {
    debug = (options instanceof stream.Writable) ? options : options.debug
    if(options.stripAnsi) stripAnsi = strip
  }

  this.expects = []

  var readBuffer = ''
  var needNew = true
  var sentEof = false

  var condition
  var responses

  this._transform = function(chunk, encoding, callback)
  {
    if(debug) debug.write(chunk)

    readBuffer += chunk.toString()
    if(needNew)
    {
      var expect = this.expects.shift()

      condition = expect && expect.condition
      responses = expect && expect.responses

      if(!expect && !sentEof)
      {
        // this.expects was always empty
        this.push(null)
        sentEof = true
      }

      needNew = false
    }

    const buffer = stripAnsi ? stripAnsi(readBuffer) : readBuffer

    var match
    switch(typeof condition)
    {
      case 'string':
        match = new RegExp(escape(condition) + `[^\S\r\n]*`).test(buffer);
      break

      case 'object':
        match = buffer.match(condition)
    }

    if(match)
    {
      match = (match instanceof Array) ? match.slice(1) : []

      readBuffer = ''
      needNew = true

      for(let response of responses)
      {
        if(response instanceof Function)
          response.apply(this, match)
        else
        {
          this.push(response)

          if(debug) debug.write(response, 'utf8')
        }
      }

      if(!this.expects.length)
      {
        this.push(null)
        sentEof = true
      }
    }

    callback()
  }
}
util.inherits(SupposeStream, stream.Transform)


SupposeStream.prototype.when = function(condition, response) {
  var expect =
  {<commit_msg>Use the return value of a response argument whose value is a function<commit_after>var escape     = require('escape-string-regexp')
var forceArray = require('force-array')
var stream     = require('stream')
var strip      = require('strip-ansi')
var util       = require('util')


function SupposeStream(options) {
  if(!(this instanceof SupposeStream)) return new SupposeStream(options)

  SupposeStream.super_.call(this)


  var debug
  var stripAnsi
  if(options)
  {
    debug = (options instanceof stream.Writable) ? options : options.debug
    if(options.stripAnsi) stripAnsi = strip
  }

  this.expects = []

  var readBuffer = ''
  var needNew = true
  var sentEof = false

  var condition
  var responses

  this._transform = function(chunk, encoding, callback)
  {
    if(debug) debug.write(chunk)

    readBuffer += chunk.toString()
    if(needNew)
    {
      var expect = this.expects.shift()

      condition = expect && expect.condition
      responses = expect && expect.responses

      if(!expect && !sentEof)
      {
        // this.expects was always empty
        this.push(null)
        sentEof = true
      }

      needNew = false
    }

    const buffer = stripAnsi ? stripAnsi(readBuffer) : readBuffer

    var match
    switch(typeof condition)
    {
      case 'string':
        match = new RegExp(escape(condition) + `[^\S\r\n]*`).test(buffer);
      break

      case 'object':
        match = buffer.match(condition)
    }

    if(match)
    {
      match = (match instanceof Array) ? match.slice(1) : []

      readBuffer = ''
      needNew = true

      for(let response of responses)
      {
        if(response instanceof Function) response = response.apply(this, match)

        this.push(response)

        if(debug) debug.write(response, 'utf8')
      }

      if(!this.expects.length)
      {
        this.push(null)
        sentEof = true
      }
    }

    callback()
  }
}
util.inherits(SupposeStream, stream.Transform)


SupposeStream.prototype.when = function(condition, response) {
  var expect =
  {
{'fraction_non_alphanumeric': 0.11182108626198083, 'fraction_numerical': 0.0009830425165888424, 'mean_word_length': 2.405857740585774, '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': '2843168', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1199, 'n_words': 282}