<commit_before>var Promise = require('bluebird');

function TransformQueue(assetGraph) {
  this.assetGraph = assetGraph;
  this.transforms = [];
  this.conditions = [];
}

TransformQueue.prototype = {
  queue: function(...args) {
    // ...
    if (
      !this.conditions.length ||
      this.conditions[this.conditions.length - 1]
    ) {
      Array.prototype.push.apply(this.transforms, args);
    }
    return this;
  },

  if: function(condition) {
    this.conditions.push(condition);
    return this;
  },

  else: function() {
    if (!this.conditions.length) {
      throw new Error('else: No condition on the stack');
    }
    this.conditions.push(!this.conditions.pop());
    return this;
  },

  endif: function() {
    if (!this.conditions.length) {
      throw new Error('endif: No condition on the stack');
    }
    this.conditions.pop();
    return this;
  },

  run: function(cb, previousResult) {
    const that = this;
    let nextTransform;
    that.assetGraph.transformQueue = that; // Hack
    // Skip past falsy transforms:
    do {
      nextTransform = that.transforms.shift();
    } while (!nextTransform && that.transforms.length);
    if (nextTransform) {
      that.assetGraph._runTransform(nextTransform, (err, result) => {
        if (err) {
          // UnexpectedError instance, avoid rendering the error message twice:
          if (err.name !== 'UnexpectedError') {
            err.message =
              (nextTransform.name || 'unnamed') + ' transform: ' + err.message;
          }
          if (cb) {
            cb(err);
          } else {
            throw err;
          }
        } else {
          that.run(cb, result);
        }
      });
    } else if (cb) {
      cb(null, previousResult);
    }
    return that;
  },

  then(...args) {
    // ...
    var promise = new Promise(
      (resolve, reject) => {
        this.run((err, value) => {
          if (err) {
            reject(err);
          } else {
            resolve(value);
          }
        });
      }
    );
    return promise.then.apply(promise, args);
  }
};

// Pre-ES5 alternative for the 'if' method:
TransformQueue.prototype.if_ = TransformQueue.prototype.if;

module.exports = TransformQueue;<commit_msg>Fix up Array#apply<commit_after>var Promise = require('bluebird');

function TransformQueue(assetGraph) {
  this.assetGraph = assetGraph;
  this.transforms = [];
  this.conditions = [];
}

TransformQueue.prototype = {
  queue: function(...args) {
    // ...
    if (
      !this.conditions.length ||
      this.conditions[this.conditions.length - 1]
    ) {
      this.transforms.push(...args);
    }
    return this;
  },

  if: function(condition) {
    this.conditions.push(condition);
    return this;
  },

  else: function() {
    if (!this.conditions.length) {
      throw new Error('else: No condition on the stack');
    }
    this.conditions.push(!this.conditions.pop());
    return this;
  },

  endif: function() {
    if (!this.conditions.length) {
      throw new Error('endif: No condition on the stack');
    }
    this.conditions.pop();
    return this;
  },

  run: function(cb, previousResult) {
    const that = this;
    let nextTransform;
    that.assetGraph.transformQueue = that; // Hack
    // Skip past falsy transforms:
    do {
      nextTransform = that.transforms.shift();
    } while (!nextTransform && that.transforms.length);
    if (nextTransform) {
      that.assetGraph._runTransform(nextTransform, (err, result) => {
        if (err) {
          // UnexpectedError instance, avoid rendering the error message twice:
          if (err.name !== 'UnexpectedError') {
            err.message =
              (nextTransform.name || 'unnamed') + ' transform: ' + err.message;
          }
          if (cb) {
            cb(err);
          } else {
            throw err;
          }
        } else {
          that.run(cb, result);
        }
      });
    } else if (cb) {
      cb(null, previousResult);
    }
    return that;
  },

  then(...args) {
    // ...
    var promise = new Promise((resolve, reject) => {
      this.run((err, value) => {
        if (err) {
          reject(err);
        } else {
          resolve(value);
        }
      });
    });
    return promise.then(...args);
  }
};

// Pre-ES5 alternative for the 'if' method:
TransformQueue.prototype.if_ = TransformQueue.prototype.if;

module.exports = TransformQueue;
{'fraction_non_alphanumeric': 0.14366901568538304, 'fraction_numerical': 0.0009092975676290066, 'mean_word_length': 2.0513176144244105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '7352308', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1296, 'n_words': 282}