<commit_before>/**!
 * koa-locales - index.js
 *
 * Copyright(c) koajs and other contributors.
 * MIT Licensed
 *
 * Authors:
 *   fengmk2 <<EMAIL>> (http://fengmk2.com)
 */

"use strict";

/**
 * Module dependencies.
 */

var debug = require('debug')('koa-locales');
var ini = require('ini');
var util = require('util');
var fs = require('fs');
var path = require('path');
var ms = require('humanize-ms');

module.exports = function (app, options) {
  options = options || {};
  var defaultLocale = formatLocale(options.defaultLocale || 'en-US');
  var queryField = options.queryField || 'locale';
  var cookieField = options.cookieField || 'locale';
  var cookieMaxAge = ms(options.cookieMaxAge || '1y');
  var localeDir = options.dir || path.join(process.cwd(), 'locales');
  var functionName = options.functionName || '__';
  var resources = {};

  if (fs.existsSync(localeDir)) {
    var names = fs.readdirSync(localeDir);
    for (var i = 0; i < names.length; i++) {
      var name = names[i];
      if (!/\.(js|json|properties)$/.test(name)) {
        continue;
      }
      var filepath = path.join(localeDir, name);
      // support en_US.js => en-US.js
      var locale = formatLocale(name.split('.')[0]);
      if (/\.properties$/.test(name)) {
        resources[locale] = ini.parse(fs.readFileSync(filepath, 'utf8'));
      } else {
        resources[locale] = require(filepath);
      }
    }
  }

  debug('init locales with %j, got %j resources', options, Object.keys(resources));

  var ARRAY_INDEX_RE = /\{(\d+)\}/g;
  function formatWithArray(text, values) {
    return text.replace(ARRAY_INDEX_RE, function (orignal, matched) {
      var index = parseInt(matched);
      if (index < values.length) {
        return values[index];<commit_msg>optimize use regexp without caught group<commit_after>/**!
 * koa-locales - index.js
 *
 * Copyright(c) koajs and other contributors.
 * MIT Licensed
 *
 * Authors:
 *   fengmk2 <<EMAIL>> (http://fengmk2.com)
 */

"use strict";

/**
 * Module dependencies.
 */

var debug = require('debug')('koa-locales');
var ini = require('ini');
var util = require('util');
var fs = require('fs');
var path = require('path');
var ms = require('humanize-ms');

module.exports = function (app, options) {
  options = options || {};
  var defaultLocale = formatLocale(options.defaultLocale || 'en-US');
  var queryField = options.queryField || 'locale';
  var cookieField = options.cookieField || 'locale';
  var cookieMaxAge = ms(options.cookieMaxAge || '1y');
  var localeDir = options.dir || path.join(process.cwd(), 'locales');
  var functionName = options.functionName || '__';
  var resources = {};

  if (fs.existsSync(localeDir)) {
    var names = fs.readdirSync(localeDir);
    for (var i = 0; i < names.length; i++) {
      var name = names[i];
      if (!/\.(?:js|json|properties)$/.test(name)) {
        continue;
      }
      var filepath = path.join(localeDir, name);
      // support en_US.js => en-US.js
      var locale = formatLocale(name.split('.')[0]);
      if (/\.properties$/.test(name)) {
        resources[locale] = ini.parse(fs.readFileSync(filepath, 'utf8'));
      } else {
        resources[locale] = require(filepath);
      }
    }
  }

  debug('init locales with %j, got %j resources', options, Object.keys(resources));

  var ARRAY_INDEX_RE = /\{(\d+)\}/g;
  function formatWithArray(text, values) {
    return text.replace(ARRAY_INDEX_RE, function (orignal, matched) {
      var index = parseInt(matched);
      if (index < values.length) {
        return values[index];
{'fraction_non_alphanumeric': 0.17300648069878838, 'fraction_numerical': 0.0033812341504649195, 'mean_word_length': 3.308252427184466, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '1802602', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1142, 'n_words': 268}