<commit_before>var q = require('q');
var fs = require('fs');

var DEFAULT_REGEXP = "(^|[^\\w_\\$])transl\\([\\\"\\\']([^\\\"\\\']+)[\\\"\\\']\\)";

var translate = function(content, dictionary) {
  if (!content) {
    return new Error('No content to transform (translate).');
  }

  var res = content.trim().split('.').reduce(function(dict, key) {
    if (!dict) {
      return null;
    }
    return dict[key];
  }, dictionary);

  if (!res) {
    return new Error('No such content (' + content + ') in locale file');
  } else {
    return '\"' + res + '\"';
  }
}

var getDictionaryFromFile = function(path, transformer) {
  if (typeof transformer !== 'function') {
    transformer = JSON.parse;
  }

  filecontent = fs.readFileSync(path+'.json', {
      encoding: 'utf8'
  });

  return transformer(filecontent);
};

module.exports = (function() {
  var Translator = function(options){
    options = options || {};

    var regexp = DEFAULT_REGEXP;
    if (options.syntaxFnName) {
      regexp = regexp.replace('transl', options.syntaxFnName || 'transl');
    }

    this.syntaxPattern = new RegExp(regexp, 'g');

    if (typeof options.translate === 'function') {
      this._translate = option.translatorFn;
    } else {
      this._translate = translate;
    }

    var dictionaryTransformer;
    if (typeof options.dictionaryTransformer === 'function') {
      dictionaryTransformer = options.dictionaryTransformer;
    }

    if (typeof options === 'string') {
      this.dictionaryObject = getDictionaryFromFile(options, dictionaryTransformer);
    } else if (options.dictionaryFilePath) {
      this.dictionaryObject = getDictionaryFromFile(options.dictionaryFilePath, dictionaryTransformer);
    } else if (options.dictionaryObject) {
      this.dictionaryObject = options.dictionaryObject;
    } else {
      throw new Error('Please supply a dictionary either pass `dictionaryFilePath` or `dictionaryObject` option');
    }

    return this;
  };

  Translator.prototype.translate = function(content) {
    var resultPromise = q.defer();
    var self = this;

    resultPromise.resolve(content.replace(self.syntaxPattern, function (s, prefix, data) {
      var res = data;

      res = self._translate(res, self.dictionaryObject);

      if (res instanceof Error) {
        return resultPromise.reject(res);
      }

      return prefix + res;
    }, content));
<commit_msg>Rename dictionaryTransformer option into fileToDictFn<commit_after>var q = require('q');
var fs = require('fs');

var DEFAULT_REGEXP = "(^|[^\\w_\\$])transl\\([\\\"\\\']([^\\\"\\\']+)[\\\"\\\']\\)";

var translate = function(content, dictionary) {
  if (!content) {
    return new Error('No content to transform (translate).');
  }

  var res = content.trim().split('.').reduce(function(dict, key) {
    if (!dict) {
      return null;
    }
    return dict[key];
  }, dictionary);

  if (!res) {
    return new Error('No such content (' + content + ') in locale file');
  } else {
    return '\"' + res + '\"';
  }
}

var getDictionaryFromFile = function(path, transformer) {
  if (typeof transformer !== 'function') {
    transformer = JSON.parse;
  }

  filecontent = fs.readFileSync(path+'.json', {
      encoding: 'utf8'
  });

  return transformer(filecontent);
};

module.exports = (function() {
  var Translator = function(options){
    options = options || {};

    var regexp = DEFAULT_REGEXP;
    if (options.syntaxFnName) {
      regexp = regexp.replace('transl', options.syntaxFnName || 'transl');
    }

    this.syntaxPattern = new RegExp(regexp, 'g');

    if (typeof options.translate === 'function') {
      this._translate = option.translatorFn;
    } else {
      this._translate = translate;
    }

    var fileToDictFn;
    if (typeof options.dictionaryTransformer === 'function') {
      fileToDictFn = options.fileToDictFn;
    }

    if (typeof options === 'string') {
      this.dictionaryObject = getDictionaryFromFile(options, fileToDictFn);
    } else if (options.dictionaryFilePath) {
      this.dictionaryObject = getDictionaryFromFile(options.dictionaryFilePath, fileToDictFn);
    } else if (options.dictionaryObject) {
      this.dictionaryObject = options.dictionaryObject;
    } else {
      throw new Error('Please supply a dictionary either pass `dictionaryFilePath` or `dictionaryObject` option');
    }

    return this;
  };

  Translator.prototype.translate = function(content) {
    var resultPromise = q.defer();
    var self = this;

    resultPromise.resolve(content.replace(self.syntaxPattern, function (s, prefix, data) {
      var res = data;

      res = self._translate(res, self.dictionaryObject);

      if (res instanceof Error) {
        return resultPromise.reject(res);
      }

      return prefix + res;
    }, content));

{'fraction_non_alphanumeric': 0.1510383889238515, 'fraction_numerical': 0.0004195510803440319, 'mean_word_length': 3.19349164467898, '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': '870167', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1373, 'n_words': 328}