<commit_before>'use strict';

// Load rules
var Trie = require('./lib/suffix-trie.js');
var allRules = Trie.fromJson(require('./rules.json'));

// Internals
var extractHostname = require('./lib/clean-host.js');
var getDomain = require('./lib/domain.js');
var getPublicSuffix = require('./lib/public-suffix.js');
var getSubdomain = require('./lib/subdomain.js');
var isValid = require('./lib/is-valid.js');
var tldExists = require('./lib/tld-exists.js');


function parse(url, validHosts, rules, _extractHostname) {
  var hostname = _extractHostname(url);
  var valid = isValid(hostname);
  var suffix = null;
  var domain = null;
  var subdomain = null;

  if (valid) {
    suffix = getPublicSuffix(rules, hostname);
    domain = getDomain(validHosts, suffix, hostname);
    subdomain = getSubdomain(hostname, domain);
  }

  return {
    valid,
    hostname,
    suffix,
    domain,
    subdomain,
  };
}


/**
 * Creates a new instance of tldjs
 * @param  {Object.<rules,validHosts>} options [description]
 * @return {tldjs|Object}                      [description]
 */
function factory(options) {
  var rules = options.rules || allRules || {};
  var validHosts = options.validHosts || [];
  var _extractHostname = options.extractHostname || extractHostname;

  return {
    extractHostname: _extractHostname,
    isValid: isValid,
    tldExists: function (url) {
      var hostname = _extractHostname(url);
      return tldExists(rules, hostname);
    },
    getPublicSuffix: function (url) {
      return parse(url, validHosts, rules, _extractHostname).suffix;
    },
    getDomain: function (url) {
      return parse(url, validHosts, rules, _extractHostname).domain;
    },
    getSubdomain: function (url) {
      return parse(url, validHosts, rules, _extractHostname).subdomain;
    },
    parse: function (url) {
      return parse(url, validHosts, rules, _extractHostname);
    },
    fromUserSettings: factory
  };
}


module.exports = factory({});<commit_msg>Rename suffix into publicSuffix for consistency<commit_after>'use strict';

// Load rules
var Trie = require('./lib/suffix-trie.js');
var allRules = Trie.fromJson(require('./rules.json'));

// Internals
var extractHostname = require('./lib/clean-host.js');
var getDomain = require('./lib/domain.js');
var getPublicSuffix = require('./lib/public-suffix.js');
var getSubdomain = require('./lib/subdomain.js');
var isValid = require('./lib/is-valid.js');
var tldExists = require('./lib/tld-exists.js');


function parse(url, validHosts, rules, _extractHostname) {
  var hostname = _extractHostname(url);
  var valid = isValid(hostname);
  var publicSuffix = null;
  var domain = null;
  var subdomain = null;

  if (valid) {
    publicSuffix = getPublicSuffix(rules, hostname);
    domain = getDomain(validHosts, publicSuffix, hostname);
    subdomain = getSubdomain(hostname, domain);
  }

  return {
    valid,
    hostname,
    publicSuffix,
    domain,
    subdomain,
  };
}


/**
 * Creates a new instance of tldjs
 * @param  {Object.<rules,validHosts>} options [description]
 * @return {tldjs|Object}                      [description]
 */
function factory(options) {
  var rules = options.rules || allRules || {};
  var validHosts = options.validHosts || [];
  var _extractHostname = options.extractHostname || extractHostname;

  return {
    extractHostname: _extractHostname,
    isValid: isValid,
    tldExists: function (url) {
      var hostname = _extractHostname(url);
      return tldExists(rules, hostname);
    },
    getPublicSuffix: function (url) {
      return parse(url, validHosts, rules, _extractHostname).publicSuffix;
    },
    getDomain: function (url) {
      return parse(url, validHosts, rules, _extractHostname).domain;
    },
    getSubdomain: function (url) {
      return parse(url, validHosts, rules, _extractHostname).subdomain;
    },
    parse: function (url) {
      return parse(url, validHosts, rules, _extractHostname);
    },
    fromUserSettings: factory
  };
}


module.exports = factory({});
{'fraction_non_alphanumeric': 0.13672654690618763, 'fraction_numerical': 0.0, 'mean_word_length': 3.608045977011494, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7597726', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1246, 'n_words': 290}