<commit_before>// mtd.js
// <NAME>, <<EMAIL>>
// MIT, 2016

var ARGS = require('minimist');

function MTD (options) {
  this.options = ARGS(process.argv.slice(2), options);
  this.argv = this.options._;
  this.tracks = {};
  this.radio = {};
  this._default = null;
  this._always = [];
}

MTD.prototype.halt = function (track, failures) {
  console.warn('Required option(s) not found for [ %s ]:', track);

  failures.forEach(function (failure) {
    console.warn('\t%s', failure);
  });
};

var isFunction = function (object) {
  return typeof object === 'function';
};

var parse = function (block) {
  var args = /\(\s*([^)]+?)\s*\)/.exec(block.toString());
  return args ? args[1].split(/\s*,\s*/) : [];
};

MTD.prototype.track = function (name, requirements, block) {
  var jump;

  if (isFunction(requirements)) {
    block = requirements;
    requirements = parse(block);
    jump = true;
  } else if (!isFunction(block))
    block = requirements.pop();

  if (jump || isFunction(block))
    this.tracks[name] = {
      requirements: requirements,
      block: block
    };
  else console.warn('No block given for [ %s ].', name);

  return this;
};

MTD.prototype.default = function (name, requirements, block) {
  if (typeof name !== 'string') {
    block = requirements;
    requirements = name;
    name = 'MTD_DEFAULT';
  }

  if (!requirements || this.track(name, requirements, block))
    this._default = name;

  return this;
};

MTD.prototype.always = function (name, requirements, block) {
  var always = this._always;

  if (typeof name !== 'string') {
    block = requirements;
    requirements = name;
    name = 'MTD_ALWAYS_' + always.length;
  }

  if (!requirements || this.track(name, requirements, block))
    always.push(name);

  return this;
};

MTD.prototype.dispatch = function (track) {
  var
  tracks = this.tracks,
  options, context, bindings, failures;

  if (tracks.hasOwnProperty(track))
    context = tracks[track];
  else return console.warn('Track [ %s ] not found.', track);

  options = this.options;
  bindings = context.requirements.map(function (requirement) {
    if (options.hasOwnProperty(requirement))
      return options[requirement];
    else if (failures) failures.push(requirement);
    else failures = [requirement];
  });

  if (failures) return this.halt(track, failures);

  context.block.apply(this, bindings);
};

var dispatcher = function (track) {
  this.dispatch(track);
};

MTD.prototype.embark = function () {
  var def, argv = this.argv;

  if (argv.length)
    argv.forEach(dispatcher, this);
  else if ((def = this._default))
    this.dispatch(def);

  this._always.forEach(dispatcher, this);
};

module.exports = function wrap (options) {
  return new MTD(options);
};
<commit_msg>parse fix for blocks with no arguments, grammar<commit_after>// mtd.js
// Colin 'Oka' Hall-Coates, <<EMAIL>>
// MIT, 2016

var ARGS = require('minimist');

function MTD (options) {
  this.options = ARGS(process.argv.slice(2), options);
  this.argv = this.options._;
  this.tracks = {};
  this.radio = {};
  this._default = null;
  this._always = [];
}

MTD.prototype.halt = function (track, failures) {
  console.warn('Required option(s) not found for [ %s ]:', track);

  failures.forEach(function (failure) {
    console.warn('\t%s', failure);
  });
};

var isFunction = function (object) {
  return typeof object === 'function';
};

var parse = function (block) {
  var args = block.length && /\(\s*([^)]+?)\s*\)/.exec(block.toString());
  return args ? args[1].split(/\s*,\s*/) : [];
};

MTD.prototype.track = function (name, requirements, block) {
  var jump;

  if (isFunction(requirements)) {
    block = requirements;
    requirements = parse(block);
    jump = true;
  } else if (!isFunction(block))
    block = requirements.pop();

  if (jump || isFunction(block))
    this.tracks[name] = {
      requirements: requirements,
      block: block
    };
  else console.warn('No block given for [ %s ].', name);

  return this;
};

MTD.prototype.default = function (name, requirements, block) {
  if (typeof name !== 'string') {
    block = requirements;
    requirements = name;
    name = 'MTD_DEFAULT';
  }

  if (!requirements || this.track(name, requirements, block))
    this._default = name;

  return this;
};

MTD.prototype.always = function (name, requirements, block) {
  var always = this._always;

  if (typeof name !== 'string') {
    block = requirements;
    requirements = name;
    name = 'MTD_ALWAYS_' + always.length;
  }

  if (!requirements || this.track(name, requirements, block))
    always.push(name);

  return this;
};

MTD.prototype.dispatch = function (track) {
  var
  tracks = this.tracks,
  options, context, bindings, failures;

  if (tracks.hasOwnProperty(track))
    context = tracks[track];
  else return console.warn('Track [ %s ] not found.', track);

  options = this.options;
  bindings = context.requirements.map(function (requirement) {
    if (options.hasOwnProperty(requirement))
      return options[requirement];
    else if (failures) failures.push(requirement);
    else failures = [requirement];
  });

  if (failures) return this.halt(track, failures);

  context.block.apply(this, bindings);
};

var dispatcher = function (track) {
  this.dispatch(track);
};

MTD.prototype.embark = function () {
  var def, argv = this.argv;

  if (argv.length)
    argv.forEach(dispatcher, this);
  else if ((def = this._default))
    this.dispatch(def);

  this._always.forEach(dispatcher, this);
};

module.exports = function barrier (options) {
  return new MTD(options);
};

{'fraction_non_alphanumeric': 0.1613424264178033, 'fraction_numerical': 0.0021536252692031586, 'mean_word_length': 3.5755336617405584, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '147671', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1779, 'n_words': 442}