<commit_before>/**
 * Redirects naked(root domain) requests to www or its reverse.
 *
 * @author GONZO <<EMAIL>>
 */


var subdomainParser = function() {
  const parseDomain = require('parse-domain');
  const parsedDomain = {};

  return {
    get: function(hostname) {
      // Cache, Touch
      if (parsedDomain[hostname] == undefined) {
        try {
          let ps = parseDomain(hostname);
          parsedDomain[hostname] = [ps.subdomain, ps.domain + '.' + ps.tld];
        } catch (e) {
          parsedDomain[hostname] = [null, null];
        }
      }
      return parsedDomain[hostname];
    }
  }
}();

module.exports = function(reverse, subDomain, status) {
  let options;

  if (arguments.length === 1 && typeof arguments[0] === 'object') {
    options = arguments[0];
  } else {
    options = {
      reverse: reverse,
      subDomain: subDomain,
      status: status
    }
  }

  if (options.except) {
    if (typeof options.except === 'string') {
      options.except = [options.except];
    }

    // Assertion
    if (typeof options.except.length !== 'number') {
      throw new Error('Except option must be set with an array or string.');
    }

    let UrlPattern;

    try {
      UrlPattern = require('url-pattern');
    }
    catch (e) {
      console.error(e)
      throw new Error('The third party module `url-pattern` is required.');
    }

    for (let i = 0; i < options.except.length; i++) {
      options.except[i] = new UrlPattern(options.except[i]);
    }
  }

  return function(req, res, next) {
    let domain = subdomainParser.get(req.hostname);
    options.subDomain = options.subDomain || 'www';

    if (options.except) {
      for (let i = 0; i < options.except.length; i++) {
        if (options.except[i].match(req.url)) {
          return next();
        }
      }
    }

    if (typeof(options.reverse) === 'string' && options.subDomain === undefined) {
      options.subDomain = options.reverse;
      options.reverse = false;
    }
    if (options.status === undefined) {
      options.status = 302;
    }

    if (domain[0] == '' && !options.reverse) {
      res.redirect(options.status, req.protocol + '://' + options.subDomain + '.' + domain[1] + req.url);
      return;
    } else if (domain[0] == options.subDomain && options.reverse) {
      res.redirect(options.status, req.protocol + '://' + domain[1] + req.url);
      return;
    }

    return next();<commit_msg>change variables declartion.<commit_after>/**
 * Redirects naked(root domain) requests to www or its reverse.
 *
 * @author GONZO <<EMAIL>>
 */


var subdomainParser = function() {
  const parseDomain = require('parse-domain');
  const parsedDomain = {};

  return {
    get: function(hostname) {
      // Cache, Touch
      if (parsedDomain[hostname] == undefined) {
        try {
          var ps = parseDomain(hostname);
          parsedDomain[hostname] = [ps.subdomain, ps.domain + '.' + ps.tld];
        } catch (e) {
          parsedDomain[hostname] = [null, null];
        }
      }
      return parsedDomain[hostname];
    }
  }
}();

module.exports = function(reverse, subDomain, status) {
  var options;

  if (arguments.length === 1 && typeof arguments[0] === 'object') {
    options = arguments[0];
  } else {
    options = {
      reverse: reverse,
      subDomain: subDomain,
      status: status
    }
  }

  if (options.except) {
    if (typeof options.except === 'string') {
      options.except = [options.except];
    }

    // Assertion
    if (typeof options.except.length !== 'number') {
      throw new Error('Except option must be set with an array or string.');
    }

    var UrlPattern;

    try {
      UrlPattern = require('url-pattern');
    }
    catch (e) {
      console.error(e)
      throw new Error('The third party module `url-pattern` is required.');
    }

    for (var i = 0; i < options.except.length; i++) {
      options.except[i] = new UrlPattern(options.except[i]);
    }
  }

  return function(req, res, next) {
    var domain = subdomainParser.get(req.hostname);
    options.subDomain = options.subDomain || 'www';

    if (options.except) {
      for (var i = 0; i < options.except.length; i++) {
        if (options.except[i].match(req.url)) {
          return next();
        }
      }
    }

    if (typeof(options.reverse) === 'string' && options.subDomain === undefined) {
      options.subDomain = options.reverse;
      options.reverse = false;
    }
    if (options.status === undefined) {
      options.status = 302;
    }

    if (domain[0] == '' && !options.reverse) {
      res.redirect(options.status, req.protocol + '://' + options.subDomain + '.' + domain[1] + req.url);
      return;
    } else if (domain[0] == options.subDomain && options.reverse) {
      res.redirect(options.status, req.protocol + '://' + domain[1] + req.url);
      return;
    }

    return next();
{'fraction_non_alphanumeric': 0.15550978372811536, 'fraction_numerical': 0.004943357363542739, 'mean_word_length': 2.405329593267882, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2379735', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1504, 'n_words': 353}