Use const, let instead of var

This commit is contained in:
Raymond Hill 2019-02-24 09:01:58 -05:00
parent 1c26afe874
commit c161d45230
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -37,7 +37,7 @@ Naming convention from https://en.wikipedia.org/wiki/URI_scheme#Examples
/******************************************************************************/ /******************************************************************************/
var punycode = self.punycode; const punycode = self.punycode;
// Favorite regex tool: http://regex101.com/ // Favorite regex tool: http://regex101.com/
@ -46,15 +46,15 @@ var punycode = self.punycode;
// <http://jsperf.com/old-uritools-vs-new-uritools> // <http://jsperf.com/old-uritools-vs-new-uritools>
// Performance improvements welcomed. // Performance improvements welcomed.
// jsperf: <http://jsperf.com/old-uritools-vs-new-uritools> // jsperf: <http://jsperf.com/old-uritools-vs-new-uritools>
var reRFC3986 = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/; const reRFC3986 = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/;
// Derived // Derived
var reSchemeFromURI = /^[^:\/?#]+:/; const reSchemeFromURI = /^[^:\/?#]+:/;
var reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/; const reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/;
var reOriginFromURI = /^(?:[^:\/?#]+:)\/\/[^\/?#]+/; const reOriginFromURI = /^(?:[^:\/?#]+:)\/\/[^\/?#]+/;
var reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; const reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//;
var rePathFromURI = /^(?:[^:\/?#]+:)?(?:\/\/[^\/?#]*)?([^?#]*)/; const rePathFromURI = /^(?:[^:\/?#]+:)?(?:\/\/[^\/?#]*)?([^?#]*)/;
var reMustNormalizeHostname = /[^0-9a-z._-]/; const reMustNormalizeHostname = /[^0-9a-z._-]/;
// These are to parse authority field, not parsed by above official regex // These are to parse authority field, not parsed by above official regex
// IPv6 is seen as an exception: a non-compatible IPv6 is first tried, and // IPv6 is seen as an exception: a non-compatible IPv6 is first tried, and
@ -64,20 +64,20 @@ var reMustNormalizeHostname = /[^0-9a-z._-]/;
// https://github.com/gorhill/httpswitchboard/issues/211 // https://github.com/gorhill/httpswitchboard/issues/211
// "While a hostname may not contain other characters, such as the // "While a hostname may not contain other characters, such as the
// "underscore character (_), other DNS names may contain the underscore" // "underscore character (_), other DNS names may contain the underscore"
var reHostPortFromAuthority = /^(?:[^@]*@)?([^:]*)(:\d*)?$/; const reHostPortFromAuthority = /^(?:[^@]*@)?([^:]*)(:\d*)?$/;
var reIPv6PortFromAuthority = /^(?:[^@]*@)?(\[[0-9a-f:]*\])(:\d*)?$/i; const reIPv6PortFromAuthority = /^(?:[^@]*@)?(\[[0-9a-f:]*\])(:\d*)?$/i;
var reHostFromNakedAuthority = /^[0-9a-z._-]+[0-9a-z]$/i; const reHostFromNakedAuthority = /^[0-9a-z._-]+[0-9a-z]$/i;
var reHostFromAuthority = /^(?:[^@]*@)?([^:]+)(?::\d*)?$/; const reHostFromAuthority = /^(?:[^@]*@)?([^:]+)(?::\d*)?$/;
var reIPv6FromAuthority = /^(?:[^@]*@)?(\[[0-9a-f:]+\])(?::\d*)?$/i; const reIPv6FromAuthority = /^(?:[^@]*@)?(\[[0-9a-f:]+\])(?::\d*)?$/i;
// Coarse (but fast) tests // Coarse (but fast) tests
var reValidHostname = /^([a-z\d]+(-*[a-z\d]+)*)(\.[a-z\d]+(-*[a-z\d])*)*$/; const reValidHostname = /^([a-z\d]+(-*[a-z\d]+)*)(\.[a-z\d]+(-*[a-z\d])*)*$/;
var reIPAddressNaive = /^\d+\.\d+\.\d+\.\d+$|^\[[\da-zA-Z:]+\]$/; const reIPAddressNaive = /^\d+\.\d+\.\d+\.\d+$|^\[[\da-zA-Z:]+\]$/;
/******************************************************************************/ /******************************************************************************/
var reset = function(o) { const reset = function(o) {
o.scheme = ''; o.scheme = '';
o.hostname = ''; o.hostname = '';
o._ipv4 = undefined; o._ipv4 = undefined;
@ -89,7 +89,7 @@ var reset = function(o) {
return o; return o;
}; };
var resetAuthority = function(o) { const resetAuthority = function(o) {
o.hostname = ''; o.hostname = '';
o._ipv4 = undefined; o._ipv4 = undefined;
o._ipv6 = undefined; o._ipv6 = undefined;
@ -101,7 +101,7 @@ var resetAuthority = function(o) {
// This will be exported // This will be exported
var URI = { const URI = {
scheme: '', scheme: '',
authority: '', authority: '',
hostname: '', hostname: '',
@ -143,7 +143,7 @@ URI.set = function(uri) {
if ( uri === undefined ) { if ( uri === undefined ) {
return reset(URI); return reset(URI);
} }
var matches = reRFC3986.exec(uri); let matches = reRFC3986.exec(uri);
if ( !matches ) { if ( !matches ) {
return reset(URI); return reset(URI);
} }
@ -200,7 +200,7 @@ URI.assemble = function(bits) {
if ( bits === undefined ) { if ( bits === undefined ) {
bits = this.allBits; bits = this.allBits;
} }
var s = []; const s = [];
if ( this.scheme && (bits & this.schemeBit) ) { if ( this.scheme && (bits & this.schemeBit) ) {
s.push(this.scheme, ':'); s.push(this.scheme, ':');
} }
@ -232,20 +232,16 @@ URI.originFromURI = function(uri) {
/******************************************************************************/ /******************************************************************************/
URI.schemeFromURI = function(uri) { URI.schemeFromURI = function(uri) {
var matches = reSchemeFromURI.exec(uri); const matches = reSchemeFromURI.exec(uri);
if ( !matches ) { if ( !matches ) { return ''; }
return '';
}
return matches[0].slice(0, -1).toLowerCase(); return matches[0].slice(0, -1).toLowerCase();
}; };
/******************************************************************************/ /******************************************************************************/
URI.authorityFromURI = function(uri) { URI.authorityFromURI = function(uri) {
var matches = reAuthorityFromURI.exec(uri); const matches = reAuthorityFromURI.exec(uri);
if ( !matches ) { if ( !matches ) { return ''; }
return '';
}
return matches[1].slice(2).toLowerCase(); return matches[1].slice(2).toLowerCase();
}; };
@ -259,11 +255,11 @@ URI.authorityFromURI = function(uri) {
// Revisit punycode dependency when above issue is fixed in Firefox. // Revisit punycode dependency when above issue is fixed in Firefox.
URI.hostnameFromURI = function(uri) { URI.hostnameFromURI = function(uri) {
var matches = reCommonHostnameFromURL.exec(uri); let matches = reCommonHostnameFromURL.exec(uri);
if ( matches !== null ) { return matches[1]; } if ( matches !== null ) { return matches[1]; }
matches = reAuthorityFromURI.exec(uri); matches = reAuthorityFromURI.exec(uri);
if ( matches === null ) { return ''; } if ( matches === null ) { return ''; }
var authority = matches[1].slice(2); const authority = matches[1].slice(2);
// Assume very simple authority (most common case for µBlock) // Assume very simple authority (most common case for µBlock)
if ( reHostFromNakedAuthority.test(authority) ) { if ( reHostFromNakedAuthority.test(authority) ) {
return authority.toLowerCase(); return authority.toLowerCase();
@ -273,7 +269,7 @@ URI.hostnameFromURI = function(uri) {
matches = reIPv6FromAuthority.exec(authority); matches = reIPv6FromAuthority.exec(authority);
if ( matches === null ) { return ''; } if ( matches === null ) { return ''; }
} }
var hostname = matches[1]; let hostname = matches[1];
while ( hostname.endsWith('.') ) { while ( hostname.endsWith('.') ) {
hostname = hostname.slice(0, -1); hostname = hostname.slice(0, -1);
} }
@ -300,23 +296,21 @@ const psl = publicSuffixList;
/******************************************************************************/ /******************************************************************************/
URI.entityFromDomain = function(domain) { URI.entityFromDomain = function(domain) {
var pos = domain.indexOf('.'); const pos = domain.indexOf('.');
return pos !== -1 ? domain.slice(0, pos) + '.*' : ''; return pos !== -1 ? domain.slice(0, pos) + '.*' : '';
}; };
/******************************************************************************/ /******************************************************************************/
URI.pathFromURI = function(uri) { URI.pathFromURI = function(uri) {
var matches = rePathFromURI.exec(uri); const matches = rePathFromURI.exec(uri);
return matches !== null ? matches[1] : ''; return matches !== null ? matches[1] : '';
}; };
/******************************************************************************/ /******************************************************************************/
URI.domainFromURI = function(uri) { URI.domainFromURI = function(uri) {
if ( !uri ) { if ( !uri ) { return ''; }
return '';
}
return this.domainFromHostname(this.hostnameFromURI(uri)); return this.domainFromHostname(this.hostnameFromURI(uri));
}; };
@ -351,23 +345,19 @@ URI.normalizedURI = function() {
/******************************************************************************/ /******************************************************************************/
URI.rootURL = function() { URI.rootURL = function() {
if ( !this.hostname ) { if ( !this.hostname ) { return ''; }
return '';
}
return this.assemble(this.schemeBit | this.hostnameBit); return this.assemble(this.schemeBit | this.hostnameBit);
}; };
/******************************************************************************/ /******************************************************************************/
URI.isValidHostname = function(hostname) { URI.isValidHostname = function(hostname) {
var r;
try { try {
r = reValidHostname.test(hostname); return reValidHostname.test(hostname);
} }
catch (e) { catch (e) {
return false;
} }
return r; return false;
}; };
/******************************************************************************/ /******************************************************************************/
@ -379,15 +369,13 @@ URI.parentHostnameFromHostname = function(hostname) {
// `example.org` => `example.org` // `example.org` => `example.org`
// `www.example.org` => `example.org` // `www.example.org` => `example.org`
// `tomato.www.example.org` => `example.org` // `tomato.www.example.org` => `example.org`
var domain = this.domainFromHostname(hostname); const domain = this.domainFromHostname(hostname);
// `locahost` === `` => bye // `locahost` === `` => bye
// `example.org` === `example.org` => bye // `example.org` === `example.org` => bye
// `www.example.org` !== `example.org` => stay // `www.example.org` !== `example.org` => stay
// `tomato.www.example.org` !== `example.org` => stay // `tomato.www.example.org` !== `example.org` => stay
if ( domain === '' || domain === hostname ) { if ( domain === '' || domain === hostname ) { return; }
return undefined;
}
// Parent is hostname minus first label // Parent is hostname minus first label
return hostname.slice(hostname.indexOf('.') + 1); return hostname.slice(hostname.indexOf('.') + 1);
@ -403,22 +391,17 @@ URI.parentHostnamesFromHostname = function(hostname) {
// the list of hostnames by making it reusable (junkyard etc.) and which // the list of hostnames by making it reusable (junkyard etc.) and which
// has its own element counter property in order to avoid memory // has its own element counter property in order to avoid memory
// alloc/dealloc. // alloc/dealloc.
var domain = this.domainFromHostname(hostname); const domain = this.domainFromHostname(hostname);
if ( domain === '' || domain === hostname ) { if ( domain === '' || domain === hostname ) {
return []; return [];
} }
var nodes = []; const nodes = [];
var pos;
for (;;) { for (;;) {
pos = hostname.indexOf('.'); const pos = hostname.indexOf('.');
if ( pos < 0 ) { if ( pos < 0 ) { break; }
break;
}
hostname = hostname.slice(pos + 1); hostname = hostname.slice(pos + 1);
nodes.push(hostname); nodes.push(hostname);
if ( hostname === domain ) { if ( hostname === domain ) { break; }
break;
}
} }
return nodes; return nodes;
}; };
@ -429,7 +412,7 @@ URI.parentHostnamesFromHostname = function(hostname) {
// ordered from self up to domain inclusively. // ordered from self up to domain inclusively.
URI.allHostnamesFromHostname = function(hostname) { URI.allHostnamesFromHostname = function(hostname) {
var nodes = this.parentHostnamesFromHostname(hostname); const nodes = this.parentHostnamesFromHostname(hostname);
nodes.unshift(hostname); nodes.unshift(hostname);
return nodes; return nodes;
}; };