<commit_before>var Promise   = require('any-promise');
var isArray   = Array.isArray;
var cast      = Promise.cast || Promise.resolve;

module.exports = function pipeline(tasks, args, self){
  if (!isArray(args)) {
    self = args;
    args = void 0;
  }
  if (!isArray(tasks)) tasks = [tasks];
  if (!self) self = this;
  var current = cast.call(Promise);
  var result = [];
  result.push(current = current.then(function(){return tasks[0].apply(self, args);}));
  tasks.slice(1).forEach(function(task){
    result.push(current = current.then(task));
  });
  return Promise.all(result);
};<commit_msg>* [bug] pipeline should return the last result only. * [bug] the arguments could promise.<commit_after>var Promise   = require('any-promise');
var isArray   = Array.isArray;
var cast      = Promise.cast || Promise.resolve;

/*
  Run a set of task functions in sequence, passing the result
  of the previous as an argument to the next.  Like a shell
  pipeline, e.g. `cat file.txt | grep 'foo' | sed -e 's/foo/bar/g'

  @param tasks {Array|Promise} array or promise for array of task functions
  @param initialArgs {Array} arguments to be passed to the first task
  @param self {*} `this` argument to be passed to the first task
  @return {Promise} promise for return value of the final task
*/
module.exports = function pipeline(tasks, aArgs, self){
  if (!isArray(aArgs)) {
    self = aArgs;
    aArgs = [];
  }
  if (tasks && !isArray(tasks)) tasks = [tasks];
  if (!self) self = this;
  return Promise.all(aArgs).then(function(args){
    if (tasks && tasks.length) {
      var current = cast.call(Promise);
      current = current.then(function(){return tasks[0].apply(self, args);});
      tasks.slice(1).forEach(function(task){
        current = current.then(task);
      });
      return current;
    } else {
      return args;
    }
  });
};
{'fraction_non_alphanumeric': 0.14324324324324325, 'fraction_numerical': 0.002702702702702703, 'mean_word_length': 3.4927184466019416, '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': '1823972', 'n_tokens_mistral': 567, 'n_tokens_neox': 570, 'n_words': 181}