<commit_before>/**
 * Module dependencies
 */

var assert = require('assert');

/**
 * Expose 'foyer'
 */

module.exports = foyer;

/**
 * Foyer
 *
 * @param {Function[]} tasks
 * @param {Function} cb
 * @api public
 */

function foyer(tasks, cb) {
  assert('[object Array]' == Object.prototype.toString.call(tasks), 'Tasks must be an array of functions');
  cb = cb || function(){};
  var done = false;
  var results = [];
  var errors = [];
  var index = 0;

  function next() {
    var i = index++;
    var fn = tasks[i];
    if (!fn) return;
    
    try{
      fn(callback)
    } catch(err) {
      callback(err)
    }

    function callback(err, res) {
      if (done) return;
      if (err) return done = true, cb(err);

      results[i] = res;
      errors[i] = err;
      if(index == tasks.length) cb(errors, results);
    }
  }

  for (var i = 0; i < tasks.length; i++) {
    next();
  }

  return this;
}<commit_msg>Add self instantiation, prevents possible reference conflicts<commit_after>/**
 * Module dependencies
 */

var assert = require('assert');

/**
 * Expose 'foyer'
 */

module.exports = foyer;

/**
 * Foyer
 *
 * @param {Function[]} tasks
 * @param {Function} cb
 * @api public
 */

function foyer(tasks, cb) {
  if (!(this instanceof foyer)) return new foyer(tasks, cb);
  assert('[object Array]' == Object.prototype.toString.call(tasks), 'Tasks must be an array of functions');

  cb = cb || function(){};
  var done = false;
  var results = [];
  var errors = [];
  var index = 0;

  function next() {
    var i = index++;
    var fn = tasks[i];
    if (!fn) return;
    
    try{
      fn(callback)
    } catch(err) {
      callback(err)
    }

    function callback(err, res) {
      if (done) return;
      if (err) {
        done = true; 
        return cb(err);
      }

      results[i] = res;
      errors[i] = err;
      if(index == tasks.length) cb(errors, results);
    }
  }

  for (var i = 0; i < tasks.length; i++) {
    next();
  }

  return this;
}
{'fraction_non_alphanumeric': 0.16675037669512807, 'fraction_numerical': 0.0020090406830738324, 'mean_word_length': 2.4404145077720205, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4340270', 'n_tokens_mistral': 695, 'n_tokens_neox': 667, 'n_words': 194}