-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Expose bound methods * Add documentation and simplify #init signature * const -> var, damned ES2015 habits * Remove use of Function.bind to maintain IE8 compat * Update README examples and make custom factory name explicit * Test destructured bound method
- Loading branch information
Thomas Parisot
committed
Feb 13, 2017
1 parent
fa633a7
commit 981bbae
Showing
16 changed files
with
501 additions
and
424 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,47 @@ | ||
"use strict"; | ||
|
||
var tld = require('./lib/tld.js').init(); | ||
tld.rules = require('./rules.json'); | ||
var allRules = require('./rules.json'); | ||
|
||
module.exports = tld; | ||
var cleanHostValue = require('./lib/clean-host.js'); | ||
var escapeRegExp = require('./lib/escape-regexp.js'); | ||
var getRulesForTld = require('./lib/tld-rules.js'); | ||
var getDomain = require('./lib/domain.js'); | ||
var getSubdomain = require('./lib/subdomain.js'); | ||
var isValid = require('./lib/is-valid.js'); | ||
var getPublicSuffix = require('./lib/public-suffix.js'); | ||
var tldExists = require('./lib/tld-exists.js'); | ||
|
||
/** | ||
* 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 || []; | ||
|
||
return { | ||
cleanHostValue: cleanHostValue, | ||
escapeRegExp: escapeRegExp, | ||
getRulesForTld: getRulesForTld, | ||
getDomain: function (host) { | ||
return getDomain(rules, validHosts, host); | ||
}, | ||
getSubdomain: function (host) { | ||
return getSubdomain(rules, validHosts, host); | ||
}, | ||
isValid: function (host) { | ||
return isValid(validHosts, host); | ||
}, | ||
getPublicSuffix: function (host) { | ||
return getPublicSuffix(rules, host); | ||
}, | ||
tldExists: function (tld) { | ||
return tldExists(rules, tld); | ||
}, | ||
fromUserSettings: factory | ||
}; | ||
} | ||
|
||
module.exports = factory({ validHosts: [], rules: allRules }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var some = require('./polyfills/array-some.js'); | ||
|
||
/** | ||
* Returns the best rule for a given host based on candidates | ||
* | ||
* @static | ||
* @param host {String} Hostname to check rules against | ||
* @param rules {Array} List of rules used to work on | ||
* @return {Object} Candidate object, with a normal and exception state | ||
*/ | ||
module.exports = function getCandidateRule (host, rules, options) { | ||
var rule = {'normal': null, 'exception': null}; | ||
|
||
options = options || { lazy: false }; | ||
|
||
some(rules, function (r) { | ||
var pattern; | ||
|
||
// sld matching or validHost? escape the loop immediately (except if it's an exception) | ||
if ('.' + host === r.getNormalXld()) { | ||
if (options.lazy || r.exception || r.isHost) { | ||
rule.normal = r; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// otherwise check as a complete host | ||
// if it's an exception, we want to loop a bit more to a normal rule | ||
pattern = '.+' + r.getNormalPattern() + '$'; | ||
|
||
if ((new RegExp(pattern)).test(host)) { | ||
rule[r.exception ? 'exception' : 'normal'] = r; | ||
return !r.exception; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
// favouring the exception if encountered | ||
// previously we were copy-altering a rule, creating inconsistent results based on rule order order | ||
// @see https://github.com/oncletom/tld.js/pull/35 | ||
if (rule.normal && rule.exception) { | ||
return rule.exception; | ||
} | ||
|
||
return rule.normal; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var URL = require('url'); | ||
|
||
/** | ||
* Utility to cleanup the base host value. Also removes url fragments. | ||
* | ||
* Works for: | ||
* - hostname | ||
* - //hostname | ||
* - scheme://hostname | ||
* - scheme+scheme://hostname | ||
* | ||
* @param {string} value | ||
* @return {String} | ||
*/ | ||
|
||
// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) | ||
var hasPrefixRE = /^(([a-z][a-z0-9+.-]*)?:)?\/\//; | ||
var invalidHostnameChars = /[^A-Za-z0-9.-]/; | ||
|
||
function trim(value) { | ||
return String(value).replace(/(^\s+|\s+$)/g, ''); | ||
} | ||
|
||
module.exports = function cleanHostValue(value){ | ||
value = trim(value).toLowerCase(); | ||
|
||
var parts = URL.parse(hasPrefixRE.test(value) ? value : '//' + value, null, true); | ||
|
||
if (parts.hostname && !invalidHostnameChars.test(parts.hostname)) { return parts.hostname; } | ||
if (!invalidHostnameChars.test(value)) { return value; } | ||
return ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var Rule = require('./rule.js'); | ||
var isValid = require('./is-valid.js'); | ||
var cleanHostValue = require('./clean-host.js'); | ||
var extractTldFromHost = require('./from-host.js'); | ||
var getCandidateRule = require('./canditate-rule.js'); | ||
var getRulesForTld = require('./tld-rules.js'); | ||
|
||
/** | ||
* Detects the domain based on rules and upon and a host string | ||
* | ||
* @api | ||
* @param {string} host | ||
* @return {String} | ||
*/ | ||
module.exports = function getDomain (allRules, validHosts, host) { | ||
var domain = null, hostTld, rules, rule; | ||
var _validHosts = validHosts || []; | ||
|
||
if (isValid(_validHosts, host) === false) { | ||
return null; | ||
} | ||
|
||
host = cleanHostValue(host); | ||
hostTld = extractTldFromHost(host); | ||
rules = getRulesForTld(allRules, hostTld, new Rule({"firstLevel": hostTld, "isHost": _validHosts.indexOf(hostTld) !== -1})); | ||
rule = getCandidateRule(host, rules); | ||
|
||
if (rule === null) { | ||
return null; | ||
} | ||
|
||
host.replace(new RegExp(rule.getPattern()), function (m, d) { | ||
domain = d; | ||
}); | ||
|
||
return domain; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Escapes RegExp specific chars. | ||
* | ||
* @since 1.3.1 | ||
* @see https://github.com/oncletom/tld.js/pull/33 | ||
* @param {String|Mixed} s | ||
* @returns {string} Escaped string for a safe use in a `new RegExp` expression | ||
*/ | ||
module.exports = function escapeRegExp(s) { | ||
return String(s).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Utility to extract the TLD from a host string | ||
* | ||
* @param {string} host | ||
* @return {String} | ||
*/ | ||
module.exports = function extractTldFromHost(host){ | ||
return host.split('.').pop(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Checking if a host string is valid | ||
* It's usually a preliminary check before trying to use getDomain or anything else | ||
* | ||
* Beware: it does not check if the TLD exists. | ||
* | ||
* @api | ||
* @param host {String} | ||
* @return {Boolean} | ||
*/ | ||
module.exports = function isValid (validHosts, host) { | ||
return typeof host === 'string' && (validHosts.indexOf(host) !== -1 || (host.indexOf('.') !== -1 && host[0] !== '.')); | ||
}; |
Oops, something went wrong.