Informal code review toward ES6

This commit is contained in:
Raymond Hill 2019-09-11 08:08:30 -04:00
parent 8ef896476a
commit b5c1efc7f5
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 154 additions and 159 deletions

View File

@ -184,7 +184,7 @@
if ( typeof count !== 'number' ) {
return '';
}
var s = count.toFixed(0);
let s = count.toFixed(0);
if ( count >= 1000 ) {
if ( count < 10000 ) {
s = '>' + s.slice(0,1) + 'k';
@ -206,7 +206,7 @@
/******************************************************************************/
µBlock.dateNowToSensibleString = function() {
var now = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
const now = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
return now.toISOString().replace(/\.\d+Z$/, '')
.replace(/:/g, '.')
.replace('T', '_');
@ -214,34 +214,33 @@
/******************************************************************************/
µBlock.LineIterator = function(text, offset) {
µBlock.LineIterator = class {
constructor(text, offset) {
this.text = text;
this.textLen = this.text.length;
this.offset = offset || 0;
};
µBlock.LineIterator.prototype.next = function(offset) {
}
next(offset) {
if ( offset !== undefined ) {
this.offset += offset;
}
var lineEnd = this.text.indexOf('\n', this.offset);
let lineEnd = this.text.indexOf('\n', this.offset);
if ( lineEnd === -1 ) {
lineEnd = this.text.indexOf('\r', this.offset);
if ( lineEnd === -1 ) {
lineEnd = this.textLen;
}
}
var line = this.text.slice(this.offset, lineEnd);
const line = this.text.slice(this.offset, lineEnd);
this.offset = lineEnd + 1;
return line;
};
µBlock.LineIterator.prototype.charCodeAt = function(offset) {
}
charCodeAt(offset) {
return this.text.charCodeAt(this.offset + offset);
};
µBlock.LineIterator.prototype.eot = function() {
}
eot() {
return this.offset >= this.textLen;
}
};
/******************************************************************************/
@ -249,31 +248,30 @@
// The field iterator is less CPU-intensive than when using native
// String.split().
µBlock.FieldIterator = function(sep) {
µBlock.FieldIterator = class {
constructor(sep) {
this.text = '';
this.sep = sep;
this.sepLen = sep.length;
this.offset = 0;
};
µBlock.FieldIterator.prototype.first = function(text) {
}
first(text) {
this.text = text;
this.offset = 0;
return this.next();
};
µBlock.FieldIterator.prototype.next = function() {
var end = this.text.indexOf(this.sep, this.offset);
}
next() {
let end = this.text.indexOf(this.sep, this.offset);
if ( end === -1 ) {
end = this.text.length;
}
var field = this.text.slice(this.offset, end);
const field = this.text.slice(this.offset, end);
this.offset = end + this.sepLen;
return field;
};
µBlock.FieldIterator.prototype.remainder = function() {
}
remainder() {
return this.text.slice(this.offset);
}
};
/******************************************************************************/
@ -284,16 +282,42 @@
blockStartPrefix: '#block-start-', // ensure no special regex characters
blockEndPrefix: '#block-end-', // ensure no special regex characters
Writer: function() {
Writer: class {
constructor() {
this.io = µBlock.CompiledLineIO;
this.blockId = undefined;
this.block = undefined;
this.stringifier = this.io.serialize;
this.blocks = new Map();
this.properties = new Map();
}
push(args) {
this.block[this.block.length] = this.stringifier(args);
}
select(blockId) {
if ( blockId === this.blockId ) { return; }
this.blockId = blockId;
this.block = this.blocks.get(blockId);
if ( this.block === undefined ) {
this.blocks.set(blockId, (this.block = []));
}
}
toString() {
let result = [];
for ( let [ id, lines ] of this.blocks ) {
if ( lines.length === 0 ) { continue; }
result.push(
this.io.blockStartPrefix + id,
lines.join('\n'),
this.io.blockEndPrefix + id
);
}
return result.join('\n');
}
},
Reader: function(raw, blockId) {
Reader: class {
constructor(raw, blockId) {
this.io = µBlock.CompiledLineIO;
this.block = '';
this.len = 0;
@ -318,36 +342,7 @@
this.select(blockId);
}
}
};
µBlock.CompiledLineIO.Writer.prototype = {
push: function(args) {
this.block[this.block.length] = this.stringifier(args);
},
select: function(blockId) {
if ( blockId === this.blockId ) { return; }
this.blockId = blockId;
this.block = this.blocks.get(blockId);
if ( this.block === undefined ) {
this.blocks.set(blockId, (this.block = []));
}
},
toString: function() {
let result = [];
for ( let [ id, lines ] of this.blocks ) {
if ( lines.length === 0 ) { continue; }
result.push(
this.io.blockStartPrefix + id,
lines.join('\n'),
this.io.blockEndPrefix + id
);
}
return result.join('\n');
}
};
µBlock.CompiledLineIO.Reader.prototype = {
next: function() {
next() {
if ( this.offset === this.len ) {
this.line = '';
return false;
@ -361,19 +356,20 @@
this.offset = this.len;
}
return true;
},
select: function(blockId) {
}
select(blockId) {
this.block = this.blocks.get(blockId) || '';
this.len = this.block.length;
this.offset = 0;
return this;
},
fingerprint: function() {
}
fingerprint() {
return this.line;
},
args: function() {
}
args() {
return this.parser(this.line);
}
}
};
/******************************************************************************/
@ -408,16 +404,15 @@
/******************************************************************************/
µBlock.MRUCache = function(size) {
µBlock.MRUCache = class {
constructor(size) {
this.size = size;
this.array = [];
this.map = new Map();
this.resetTime = Date.now();
};
µBlock.MRUCache.prototype = {
add: function(key, value) {
var found = this.map.has(key);
}
add(key, value) {
const found = this.map.has(key);
this.map.set(key, value);
if ( !found ) {
if ( this.array.length === this.size ) {
@ -425,24 +420,24 @@
}
this.array.unshift(key);
}
},
remove: function(key) {
}
remove(key) {
if ( this.map.has(key) ) {
this.array.splice(this.array.indexOf(key), 1);
}
},
lookup: function(key) {
var value = this.map.get(key);
}
lookup(key) {
const value = this.map.get(key);
if ( value !== undefined && this.array[0] !== key ) {
var i = this.array.indexOf(key);
let i = this.array.indexOf(key);
do {
this.array[i] = this.array[i-1];
} while ( --i );
this.array[0] = key;
}
return value;
},
reset: function() {
}
reset() {
this.array = [];
this.map.clear();
this.resetTime = Date.now();
@ -459,27 +454,27 @@
/******************************************************************************/
µBlock.decomposeHostname = (function() {
µBlock.decomposeHostname = (( ) => {
// For performance purpose, as simple tests as possible
let reHostnameVeryCoarse = /[g-z_-]/;
let reIPv4VeryCoarse = /\.\d+$/;
const reHostnameVeryCoarse = /[g-z_-]/;
const reIPv4VeryCoarse = /\.\d+$/;
let toBroaderHostname = function(hostname) {
let pos = hostname.indexOf('.');
const toBroaderHostname = function(hostname) {
const pos = hostname.indexOf('.');
if ( pos !== -1 ) {
return hostname.slice(pos + 1);
}
return hostname !== '*' && hostname !== '' ? '*' : '';
};
let toBroaderIPv4Address = function(ipaddress) {
const toBroaderIPv4Address = function(ipaddress) {
if ( ipaddress === '*' || ipaddress === '' ) { return ''; }
let pos = ipaddress.lastIndexOf('.');
const pos = ipaddress.lastIndexOf('.');
if ( pos === -1 ) { return '*'; }
return ipaddress.slice(0, pos);
};
let toBroaderIPv6Address = function(ipaddress) {
const toBroaderIPv6Address = function(ipaddress) {
return ipaddress !== '*' && ipaddress !== '' ? '*' : '';
};
@ -656,7 +651,7 @@
// Rename ./tmp/requests.json.gz to something else if you no longer want
// ./assets/requests.json in the build.
µBlock.loadBenchmarkDataset = (function() {
µBlock.loadBenchmarkDataset = (( ) => {
let datasetPromise;
let ttlTimer;