<commit_before>var _ = require('lodash');

function tryLoadName(name, lookupTable, thingKind) {
  // todo: consider passing in variables via a name like 'uglify:compress=true'
  var parsed = parseName(name);

  var thing = lookupTable[parsed[0]];
  if (!thing) {
    throw new Error('Failed to resolve ' + name + ' ' + thingKind);
    return;
  }

  if (typeof thing === 'function') {
    var options = parsed[1];
    return thing(parsed[1]);
  }

  return thing;
}

function parseName(name) {
  var splitName = name.split(':');
  if (splitName.length === 1) { return splitName };
  if (splitName.length !== 2) { throw new Error('invalid argument syntax in: ' + name); };
  var functionName = splitName[0];
  var args = parseArgs(splitName[1]);
  return [functionName, args];
}

function parseArgs(argString) {
  var argsPairs = argString.split(',');
  return argsPairs.map(parseArgPair).reduce(_.extend, {});
}

function parseArgPair(argPair) {
  var pair = argPair.trim().split('=');
  if (pair.length !== 2) { throw new Error('invalid argument pair ' + argPair); }
  var res = {};
  res[pair[0]] = JSON.parse('"' + pair[1] + '"');
  return res;
}

module.exports = {
  tryLoadName,
  parseName,
  parseArgs,
  parseArgPair,
};
<commit_msg>add a concept of default to generators<commit_after>var _ = require('lodash');

function tryLoadName(name, lookupTable, thingKind) {
  // todo: consider passing in variables via a name like 'uglify:compress=true'
  if (name === 'default') {
    return tryLoadDefault(lookupTable, thingKind);
  }
  
  var parsed = parseName(name);

  var thing = lookupTable[parsed[0]];
  if (!thing) {
    throw new Error('Failed to resolve ' + name + ' ' + thingKind);
  }

  if (typeof thing === 'function') {
    var options = parsed[1];
    return thing(parsed[1]);
  }

  return thing;
}

function tryLoadDefault(lookupTable, thingKind) {
  var defaultName = lookupTable.default;
  if (!defaultName) {
    throw new Error('no default name specified for type: ' + thingKind);
  }

  return tryLoadName(defaultName, lookupTable, thingKind);
}

function parseName(name) {
  var splitName = name.split(':');
  if (splitName.length === 1) { return splitName };
  if (splitName.length !== 2) { throw new Error('invalid argument syntax in: ' + name); };
  var functionName = splitName[0];
  var args = parseArgs(splitName[1]);
  return [functionName, args];
}

function parseArgs(argString) {
  var argsPairs = argString.split(',');
  return argsPairs.map(parseArgPair).reduce(_.extend, {});
}

function parseArgPair(argPair) {
  var pair = argPair.trim().split('=');
  if (pair.length !== 2) { throw new Error('invalid argument pair ' + argPair); }
  var res = {};
  res[pair[0]] = JSON.parse('"' + pair[1] + '"');
  return res;
}

module.exports = {
  tryLoadName,
  parseName,
  parseArgs,
  parseArgPair,
};

{'fraction_non_alphanumeric': 0.1562059238363893, 'fraction_numerical': 0.007052186177715092, 'mean_word_length': 4.093357271095153, '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': '3975836', 'n_tokens_mistral': 939, 'n_tokens_neox': 922, 'n_words': 242}