From 6e95a9ca1d31db298c5ba2945d0286952f716ddd Mon Sep 17 00:00:00 2001 From: portix Date: Tue, 4 Jun 2013 17:11:11 +0200 Subject: Implementing net.hostMatch --- scripts/lib/net.js | 81 +++++++++++++++++++++++++++++++++++++++++------------- 1 file 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); })(); -- cgit v1.2.3