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
1 changed files with 41 additions and 58 deletions

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