<commit_before>var cli       = require('cli').enable('status', 'help', 'version', 'glob', 'timeout');
var fs        = require('fs');
var glob      = require('glob');
var path      = require('path');
var phpjsutil = new require('../lib/phpjsutil');
var equal     = require('deep-equal');
var __root    = __dirname + '/..';


var PhpjsUtil = phpjsutil({
  injectDependencies: ['ini_set', 'ini_get', 'array'],
  equal             : equal,
  debug             : cli.debug
});

// Environment-specific file opener. function name needs to
// be translated to code. The difficulty is in finding the
// category.
PhpjsUtil.opener = function(name, cb) {
  glob(__root + '/functions/*/' + name + '.js', {}, function(err, files) {
    if (err) {
      return cb(err);
    }
    var filepath = files[0];

    if (!filepath) {
      return cb('could not find ' + __root + '/functions/*/' + name + '.js');
    }

    fs.readFile(filepath, 'utf-8', function(err, code) {
      if (err) {
        return cb(err);
      }
      return cb(null, code);
    });
  });
};

// --debug works out of the box. See -h
cli.parse({
  action  : ['a', 'Test / Build', 'string', 'test'],
  output  : ['o', 'Build output file', 'string', __root + '/build/npm.js'],
  name    : ['n', 'Function name to test', 'path', '*'],
  category: ['c', 'Category to test', 'path', '*'],
  abort   : ['a', 'Abort on first failure']
});

cli.cleanup = function(args, options) {
  var self     = this;
  var globpath = __root + '/functions/' + options.category + '/' + options.name + '.js';
  self.glob(globpath, function (err, params, file) {
    var buf  = '';
    buf += params.func_signature.trim() + '\n';
    var k = 'from';
    buf += '  // ' + (new Array(19 - k.length).join(' ')) + k + ': ' + 'http://phpjs.org/functions' + '\n';
    for (k in params.headKeys) {
      if (params.headKeys[k].length <= 1) {
        buf += '  // ' + (new Array(19 - k.length).join(' ')) + k + ': ' + params.headKeys[k][0] + '\n';
      } else {
        for (var i in params.headKeys[k]) {
          if (k === 'example') {
            buf += '  // ' + (new Array(17 - k.length).join(' ')) + k + ' ' + ((i*1)+1) + ': ' + params.headKeys[k][i] + '\n';
            buf += '  // ' + (new Array(17 - k.length).join(' ')) + 'returns' + ' ' + ((i*1)+1) + ': ' + params.headKeys.returns[i] + '\n';
          } else if (k !== 'returns') {
            buf += '  // ' + (new Array(17 - k.length).join(' ')) + k + ' ' + ((i*1)+1) + ': ' + params.headKeys[k][i] + '\n';
          }
        }
      }
    }
    buf += '\n';<commit_msg>WIP cleanup<commit_after>var cli       = require('cli').enable('status', 'help', 'version', 'glob', 'timeout');
var fs        = require('fs');
var glob      = require('glob');
var path      = require('path');
var phpjsutil = new require('../lib/phpjsutil');
var equal     = require('deep-equal');
var __root    = __dirname + '/..';


var PhpjsUtil = phpjsutil({
  injectDependencies: ['ini_set', 'ini_get', 'array'],
  equal             : equal,
  debug             : cli.debug
});

// Environment-specific file opener. function name needs to
// be translated to code. The difficulty is in finding the
// category.
PhpjsUtil.opener = function(name, cb) {
  glob(__root + '/functions/*/' + name + '.js', {}, function(err, files) {
    if (err) {
      return cb(err);
    }
    var filepath = files[0];

    if (!filepath) {
      return cb('could not find ' + __root + '/functions/*/' + name + '.js');
    }

    fs.readFile(filepath, 'utf-8', function(err, code) {
      if (err) {
        return cb(err);
      }
      return cb(null, code);
    });
  });
};

// --debug works out of the box. See -h
cli.parse({
  action  : ['a', 'Test / Build', 'string', 'test'],
  output  : ['o', 'Build output file', 'string', __root + '/build/npm.js'],
  name    : ['n', 'Function name to test', 'path', '*'],
  category: ['c', 'Category to test', 'path', '*'],
  abort   : ['a', 'Abort on first failure']
});

cli.cleanup = function(args, options) {
  var self     = this;
  var globpath = __root + '/functions/' + options.category + '/' + options.name + '.js';
  self.glob(globpath, function (err, params, file) {
    var buf  = '';
    buf += params.func_signature.trim() + '\n';
    var k = 'from';
    buf += '  // ' + (new Array(19 - k.length).join(' ')) + k + ': ' + 'http://phpjs.org/functions' + '\n';
    for (k in params.headKeys) {
      if (params.headKeys[k].length <= 1 && k !== 'example') {
        buf += '  // ' + (new Array(19 - k.length).join(' ')) + k + ': ' + params.headKeys[k][0] + '\n';
      } else {
        for (var i in params.headKeys[k]) {
          if (k === 'example') {
            buf += '  // ' + (new Array(17 - k.length).join(' ')) + k + ' ' + ((i*1)+1) + ': ' + params.headKeys[k][i] + '\n';
            buf += '  // ' + (new Array(17 - k.length).join(' ')) + 'returns' + ' ' + ((i*1)+1) + ': ' + params.headKeys.returns[i] + '\n';
          } else if (k !== 'returns') {
            buf += '  // ' + (new Array(17 - k.length).join(' ')) + k + ' ' + ((i*1)+1) + ': ' + params.headKeys[k][i] + '\n';
          }
        }
      }
    }
    buf += '\n';
{'fraction_non_alphanumeric': 0.22846587594789033, 'fraction_numerical': 0.0077775617343962666, 'mean_word_length': 2.284802043422733, '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': '1913314', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1775, 'n_words': 442}