<commit_before>
/**
 * Module dependencies
 */

var fs = require('fs')
  , path = require('path')
  , glob = require('glob')
  , Q = require('q')
  , mkdirp = require('mkdirp');

/**
 * File
 *
 * @constructor
 */

function File() {
  this.localesFolder = pcf.localesFolder;
  this.locales = pcf.locales;
  this.newline = '\n';
}

/**
 * Write new translations to files.
 *
 * @param {Object} newTranslations
 * @param {Function} callback
 * @return {void}
 * @api private
 */

File.prototype.writeTranslations = function(newTranslations, callback) {
  if(!fs.existsSync(pcf.output)) {
    mkdirp.sync(pcf.output);
  }

  var translations = this._sortMaptoArray(newTranslations);
  for(var locale in this.locales) {
    var p = path.dirname(pcf.output) + '/' + locale + '.locale';
    if(fs.existsSync(p)) {
      fs.unlinkSync(p);
    }

    for(var i = 0; i < translations[locale].length; i++) {
      fs.appendFileSync(p, JSON.stringify(translations[locale][i]) + this.newline);
    }
  }
  if(typeof callback === 'function') {
    callback();
  }
};

/**
 * Write new translations to files.
 *
 * @param {Object} newTranslations
 * @param {Function} callback
 * @return {void}
 * @api private
 */

File.prototype.writeSingleLocaleTranslations = function(newTranslations, locale, callback) {
  if(!fs.existsSync(pcf.output)) {
    mkdirp.sync(pcf.output);
  }

  var p = pcf.store + '/' + locale + '.locale';
  if(fs.existsSync(p)) {
    fs.unlinkSync(p);
  }

  for(var i = 0; i < newTranslations.length; i++) {
    fs.appendFileSync(p, JSON.stringify(newTranslations[i]) + this.newline);
  }

  if(typeof callback === 'function') {
    callback();
  }
};

/**
 * Sort translation map to an array
 *
 * @param {Object} newTranslations
 * @return {Object}
 * @api private
 */

File.prototype._sortMaptoArray = function(newTranslations) {
  var translations = {};

  for(var locale in this.locales) {
    translations[locale] = [];
    for(var key in newTranslations[locale]) {
      translations[locale].push(newTranslations[locale][key]);
    }

    translations[locale] = translations[locale].sort(function(a, b) {
      return b.timestamp - a.timestamp;
    });
  }

  return translations;
};

/**
 * Get all translations. If locale parameter is provided
 * than only the locales translations is provided. Otherwise
 * all languages is included in the translation object.
 *
 * @param {=String} locale
 * @param {?Object} opts (opts.returnType) Specify the return
 * type for the object. Could be either `array` or `json`<commit_msg>Fixed edit ordinal problem<commit_after>
/**
 * Module dependencies
 */

var fs = require('fs')
  , path = require('path')
  , glob = require('glob')
  , Q = require('q')
  , mkdirp = require('mkdirp');

/**
 * File
 *
 * @constructor
 */

function File() {
  this.localesFolder = pcf.localesFolder;
  this.locales = pcf.locales;
  this.newline = '\n';
}

/**
 * Write new translations to files.
 *
 * @param {Object} newTranslations
 * @param {Function} callback
 * @return {void}
 * @api private
 */

File.prototype.writeTranslations = function(newTranslations, callback) {
  if(!fs.existsSync(pcf.output)) {
    mkdirp.sync(pcf.output);
  }

  var translations = this._sortMaptoArray(newTranslations);
  for(var locale in this.locales) {
    var p = path.dirname(pcf.output) + '/' + locale + '.locale';
    if(fs.existsSync(p)) {
      fs.unlinkSync(p);
    }

    for(var i = 0; i < translations[locale].length; i++) {
      fs.appendFileSync(p, JSON.stringify(translations[locale][i]) + this.newline);
    }
  }
  if(typeof callback === 'function') {
    callback();
  }
};

/**
 * Write new translations to files.
 *
 * @param {Object} newTranslations
 * @param {Function} callback
 * @return {void}
 * @api private
 */

File.prototype.writeSingleLocaleTranslations = function(newTranslations, locale, callback) {
  if(!fs.existsSync(pcf.output)) {
    mkdirp.sync(pcf.output);
  }

  var p = pcf.store + '/' + locale + '.locale';
  if(fs.existsSync(p)) {
    fs.unlinkSync(p);
  }

  for(var i = 0; i < newTranslations.length; i++) {
    fs.appendFileSync(p, JSON.stringify(newTranslations[i]) + this.newline);
  }

  if(typeof callback === 'function') {
    callback();
  }
};

/**
 * Sort translation map to an array
 *
 * @param {Object} newTranslations
 * @return {Object}
 * @api private
 */

File.prototype._sortMaptoArray = function(newTranslations) {
  var translations = {};

  for(var locale in this.locales) {
    translations[locale] = [];
    for(var key in newTranslations[locale]) {
      translations[locale].push(newTranslations[locale][key]);
    }

    translations[locale] = translations[locale].sort(function(a, b) {
      if(b.timestamp > a.timestamp) {
        return 1;
      }
      else if(b.timestamp < a.timestamp) {
        return -1;
      }
      else if(b.key > a.key) {
        return 1;
      }
      else if(b.key < a.key) {
        return -1;
      }
      else {
        return 0;
      }
    });
  }

  return translations;
};

/**
 * Get all translations. If locale parameter is provided
 * than only the locales translations is provided. Otherwise
 * all languages is included in the translation object.
 *
 * @param {=String} locale
 * @param {?Object} opts (opts.returnType) Specify the return
 * type for the object. Could be either `array` or `json`
{'fraction_non_alphanumeric': 0.15377411500280952, 'fraction_numerical': 0.0016857089342573516, 'mean_word_length': 3.254980079681275, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '575130', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1771, 'n_words': 403}