<commit_before>'use strict';

var jstransformer = require('jstransformer');
var uglify = require('uglify-js');
var CleanCSS = require('clean-css');
var resolve = require('resolve');

module.exports = filter;
function filter(name, str, options, currentDirectory, funcName) {
  funcName = funcName || 'render';
  var tr;
  try {
    try {
      tr = require(resolve.sync('jstransformer-' + name, {basedir: currentDirectory || process.cwd()}));
    } catch (ex) {
      tr = require('jstransformer-' + name);
    }
    tr = jstransformer(tr);
  } catch (ex) {}
  if (tr) {
    // TODO: we may want to add a way for people to separately specify "locals"
    var result = tr[funcName](str, options, options).body;
    if (options && options.minify) {
      try {
        switch (tr.outputFormat) {
          case 'js':
            result = uglify.minify(result, {fromString: true}).code;
            break;
          case 'css':
            result = new CleanCSS().minify(result).styles;
            break;
        }
      } catch (ex) {
        // better to fail to minify than output nothing
      }
    }
    return result;
  } else {
    var err = new Error('unknown filter ":' + name + '"');
    err.code = 'UNKNOWN_FILTER';
    throw err;
  }
}
<commit_msg>[pug-filters] do not silence errors when attempting to require filters (#3002)<commit_after>'use strict';

var jstransformer = require('jstransformer');
var uglify = require('uglify-js');
var CleanCSS = require('clean-css');
var resolve = require('resolve');

module.exports = filter;
function filter(name, str, options, currentDirectory, funcName) {
  funcName = funcName || 'render';
  var trPath;
  try {
    try {
      trPath = resolve.sync('jstransformer-' + name, {basedir: currentDirectory || process.cwd()});
    } catch (ex) {
      trPath = require.resolve('jstransformer-' + name);
    }
  } catch (ex) {
    var err = new Error('unknown filter ":' + name + '"');
    err.code = 'UNKNOWN_FILTER';
    throw err;
  }
  var tr = jstransformer(require(trPath));
  // TODO: we may want to add a way for people to separately specify "locals"
  var result = tr[funcName](str, options, options).body;
  if (options && options.minify) {
    try {
      switch (tr.outputFormat) {
        case 'js':
          result = uglify.minify(result, {fromString: true}).code;
          break;
        case 'css':
          result = new CleanCSS().minify(result).styles;
          break;
      }
    } catch (ex) {
      // better to fail to minify than output nothing
    }
  }
  return result;
}

{'fraction_non_alphanumeric': 0.14162416633974106, 'fraction_numerical': 0.001569242840329541, 'mean_word_length': 2.7280701754385963, 'pattern_counts': {'":': 2, '<': 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': '4877194', 'n_tokens_mistral': 799, 'n_tokens_neox': 792, 'n_words': 222}