diff options
author | portix <portix@gmx.net> | 2013-06-04 17:11:11 +0200 |
---|---|---|
committer | portix <portix@gmx.net> | 2013-06-04 17:11:11 +0200 |
commit | 6e95a9ca1d31db298c5ba2945d0286952f716ddd (patch) | |
tree | f50ea40b555d99fedc9adc5ef310b9a0294a5163 | |
parent | 8d350c6fee5d63d202dcc4de55e0842b12d1546c (diff) | |
download | dwb-6e95a9ca1d31db298c5ba2945d0286952f716ddd.zip |
Implementing net.hostMatch
-rw-r--r-- | scripts/lib/net.js | 81 |
1 files changed, 62 insertions, 19 deletions
diff --git a/scripts/lib/net.js b/scripts/lib/net.js index a53ee533..b2c6cfa8 100644 --- a/scripts/lib/net.js +++ b/scripts/lib/net.js @@ -1,36 +1,79 @@ (function() { var tldEnd = new RegExp("\\.tld$"); - /** - * Checks if a domain matches checking for .tld which matches all top level - * domains - * @name domainMatch - * @memberOf net - * @function - * - * @param {String} domain - * The domain to test - * @param {String} match - * A domain that may contain .tld as top level domain - * - * @returns {Boolean} - * Whether the domains match - * */ Object.defineProperties(net, { + /** + * Checks if two domain matches checking for .tld which matches all top level + * domains + * @name domainMatch + * @memberOf net + * @function + * + * @param {String} domain + * The domain to test + * @param {String} match + * A domain that may contain .tld as top level domain + * + * @returns {Boolean} + * Whether the domains match + * + * @example + * net.domainMatch("example.com", "example.tld"); // true + * net.domainMatch("example.com", "example.org"); // false + * */ "domainMatch" : { value : function(domain, match) { var result = false; if (tldEnd.test(match)) { - result = domain.substring(0, domain.indexOf(".")) === match.substring(0, match.indexOf(".")); + return domain.substring(0, domain.indexOf(".")) === match.substring(0, match.indexOf(".")); } else { - result = domain === match; + return domain === match; } - return result; } - } + }, + /** + * Checks if hostnames match checking for .tld which matches all top level + * domains + * @name hostMatch + * @memberOf net + * @function + * + * @param {String} domain + * The host to test + * @param {String} match + * A host name that may contain .tld as top level domain + * + * @returns {Boolean} + * Whether the hosts match + * @example + * net.hostMatch("www.example.com", "www.example.tld"); // true + * net.hostMatch("www.example.com", "example.tld"); // false + * net.hostMatch("www.example.com", "www.example.com"); // false + * */ + "hostMatch" : + { + value : function(host, match) { + if (tldEnd.test(match)) + { + var domain = net.domainFromHost(host); + if (domain == host) + return net.domainMatch(host, match); + + var domainStart = host.indexOf(domain); + if (host.substring(0, domainStart) != match.substring(0, domainStart)) + return false; + + return net.domainMatch(host.indexOf(domainStart), match.indexOf(domainStart)); + } + else + { + return host == match; + } + } + }, }); Object.freeze(net); })(); |