mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
Informal code review toward ES6
This commit is contained in:
parent
8ef896476a
commit
b5c1efc7f5
1 changed files with 154 additions and 159 deletions
313
src/js/utils.js
313
src/js/utils.js
|
|
@ -184,7 +184,7 @@
|
||||||
if ( typeof count !== 'number' ) {
|
if ( typeof count !== 'number' ) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
var s = count.toFixed(0);
|
let s = count.toFixed(0);
|
||||||
if ( count >= 1000 ) {
|
if ( count >= 1000 ) {
|
||||||
if ( count < 10000 ) {
|
if ( count < 10000 ) {
|
||||||
s = '>' + s.slice(0,1) + 'k';
|
s = '>' + s.slice(0,1) + 'k';
|
||||||
|
|
@ -206,7 +206,7 @@
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
µBlock.dateNowToSensibleString = function() {
|
µ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$/, '')
|
return now.toISOString().replace(/\.\d+Z$/, '')
|
||||||
.replace(/:/g, '.')
|
.replace(/:/g, '.')
|
||||||
.replace('T', '_');
|
.replace('T', '_');
|
||||||
|
|
@ -214,34 +214,33 @@
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
µBlock.LineIterator = function(text, offset) {
|
µBlock.LineIterator = class {
|
||||||
this.text = text;
|
constructor(text, offset) {
|
||||||
this.textLen = this.text.length;
|
this.text = text;
|
||||||
this.offset = offset || 0;
|
this.textLen = this.text.length;
|
||||||
};
|
this.offset = offset || 0;
|
||||||
|
|
||||||
µBlock.LineIterator.prototype.next = function(offset) {
|
|
||||||
if ( offset !== undefined ) {
|
|
||||||
this.offset += offset;
|
|
||||||
}
|
}
|
||||||
var lineEnd = this.text.indexOf('\n', this.offset);
|
next(offset) {
|
||||||
if ( lineEnd === -1 ) {
|
if ( offset !== undefined ) {
|
||||||
lineEnd = this.text.indexOf('\r', this.offset);
|
this.offset += offset;
|
||||||
if ( lineEnd === -1 ) {
|
|
||||||
lineEnd = this.textLen;
|
|
||||||
}
|
}
|
||||||
|
let lineEnd = this.text.indexOf('\n', this.offset);
|
||||||
|
if ( lineEnd === -1 ) {
|
||||||
|
lineEnd = this.text.indexOf('\r', this.offset);
|
||||||
|
if ( lineEnd === -1 ) {
|
||||||
|
lineEnd = this.textLen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const line = this.text.slice(this.offset, lineEnd);
|
||||||
|
this.offset = lineEnd + 1;
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
charCodeAt(offset) {
|
||||||
|
return this.text.charCodeAt(this.offset + offset);
|
||||||
|
}
|
||||||
|
eot() {
|
||||||
|
return this.offset >= this.textLen;
|
||||||
}
|
}
|
||||||
var line = this.text.slice(this.offset, lineEnd);
|
|
||||||
this.offset = lineEnd + 1;
|
|
||||||
return line;
|
|
||||||
};
|
|
||||||
|
|
||||||
µBlock.LineIterator.prototype.charCodeAt = function(offset) {
|
|
||||||
return this.text.charCodeAt(this.offset + offset);
|
|
||||||
};
|
|
||||||
|
|
||||||
µBlock.LineIterator.prototype.eot = function() {
|
|
||||||
return this.offset >= this.textLen;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
@ -249,31 +248,30 @@
|
||||||
// The field iterator is less CPU-intensive than when using native
|
// The field iterator is less CPU-intensive than when using native
|
||||||
// String.split().
|
// String.split().
|
||||||
|
|
||||||
µBlock.FieldIterator = function(sep) {
|
µBlock.FieldIterator = class {
|
||||||
this.text = '';
|
constructor(sep) {
|
||||||
this.sep = sep;
|
this.text = '';
|
||||||
this.sepLen = sep.length;
|
this.sep = sep;
|
||||||
this.offset = 0;
|
this.sepLen = sep.length;
|
||||||
};
|
this.offset = 0;
|
||||||
|
}
|
||||||
µBlock.FieldIterator.prototype.first = function(text) {
|
first(text) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
this.offset = 0;
|
this.offset = 0;
|
||||||
return this.next();
|
return this.next();
|
||||||
};
|
}
|
||||||
|
next() {
|
||||||
µBlock.FieldIterator.prototype.next = function() {
|
let end = this.text.indexOf(this.sep, this.offset);
|
||||||
var end = this.text.indexOf(this.sep, this.offset);
|
if ( end === -1 ) {
|
||||||
if ( end === -1 ) {
|
end = this.text.length;
|
||||||
end = this.text.length;
|
}
|
||||||
|
const field = this.text.slice(this.offset, end);
|
||||||
|
this.offset = end + this.sepLen;
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
remainder() {
|
||||||
|
return this.text.slice(this.offset);
|
||||||
}
|
}
|
||||||
var field = this.text.slice(this.offset, end);
|
|
||||||
this.offset = end + this.sepLen;
|
|
||||||
return field;
|
|
||||||
};
|
|
||||||
|
|
||||||
µBlock.FieldIterator.prototype.remainder = function() {
|
|
||||||
return this.text.slice(this.offset);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
@ -284,95 +282,93 @@
|
||||||
blockStartPrefix: '#block-start-', // ensure no special regex characters
|
blockStartPrefix: '#block-start-', // ensure no special regex characters
|
||||||
blockEndPrefix: '#block-end-', // ensure no special regex characters
|
blockEndPrefix: '#block-end-', // ensure no special regex characters
|
||||||
|
|
||||||
Writer: function() {
|
Writer: class {
|
||||||
this.io = µBlock.CompiledLineIO;
|
constructor() {
|
||||||
this.blockId = undefined;
|
this.io = µBlock.CompiledLineIO;
|
||||||
this.block = undefined;
|
this.blockId = undefined;
|
||||||
this.stringifier = this.io.serialize;
|
this.block = undefined;
|
||||||
this.blocks = new Map();
|
this.stringifier = this.io.serialize;
|
||||||
this.properties = new Map();
|
this.blocks = new Map();
|
||||||
},
|
this.properties = new Map();
|
||||||
|
|
||||||
Reader: function(raw, blockId) {
|
|
||||||
this.io = µBlock.CompiledLineIO;
|
|
||||||
this.block = '';
|
|
||||||
this.len = 0;
|
|
||||||
this.offset = 0;
|
|
||||||
this.line = '';
|
|
||||||
this.parser = this.io.unserialize;
|
|
||||||
this.blocks = new Map();
|
|
||||||
this.properties = new Map();
|
|
||||||
let reBlockStart = new RegExp(
|
|
||||||
'^' + this.io.blockStartPrefix + '(\\d+)\\n',
|
|
||||||
'gm'
|
|
||||||
);
|
|
||||||
let match = reBlockStart.exec(raw);
|
|
||||||
while ( match !== null ) {
|
|
||||||
let beg = match.index + match[0].length;
|
|
||||||
let end = raw.indexOf(this.io.blockEndPrefix + match[1], beg);
|
|
||||||
this.blocks.set(parseInt(match[1], 10), raw.slice(beg, end));
|
|
||||||
reBlockStart.lastIndex = end;
|
|
||||||
match = reBlockStart.exec(raw);
|
|
||||||
}
|
}
|
||||||
if ( blockId !== undefined ) {
|
push(args) {
|
||||||
this.select(blockId);
|
this.block[this.block.length] = this.stringifier(args);
|
||||||
}
|
}
|
||||||
}
|
select(blockId) {
|
||||||
};
|
if ( blockId === this.blockId ) { return; }
|
||||||
|
this.blockId = blockId;
|
||||||
µBlock.CompiledLineIO.Writer.prototype = {
|
this.block = this.blocks.get(blockId);
|
||||||
push: function(args) {
|
if ( this.block === undefined ) {
|
||||||
this.block[this.block.length] = this.stringifier(args);
|
this.blocks.set(blockId, (this.block = []));
|
||||||
},
|
}
|
||||||
select: function(blockId) {
|
}
|
||||||
if ( blockId === this.blockId ) { return; }
|
toString() {
|
||||||
this.blockId = blockId;
|
let result = [];
|
||||||
this.block = this.blocks.get(blockId);
|
for ( let [ id, lines ] of this.blocks ) {
|
||||||
if ( this.block === undefined ) {
|
if ( lines.length === 0 ) { continue; }
|
||||||
this.blocks.set(blockId, (this.block = []));
|
result.push(
|
||||||
|
this.io.blockStartPrefix + id,
|
||||||
|
lines.join('\n'),
|
||||||
|
this.io.blockEndPrefix + id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return result.join('\n');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
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 = {
|
Reader: class {
|
||||||
next: function() {
|
constructor(raw, blockId) {
|
||||||
if ( this.offset === this.len ) {
|
this.io = µBlock.CompiledLineIO;
|
||||||
|
this.block = '';
|
||||||
|
this.len = 0;
|
||||||
|
this.offset = 0;
|
||||||
this.line = '';
|
this.line = '';
|
||||||
return false;
|
this.parser = this.io.unserialize;
|
||||||
|
this.blocks = new Map();
|
||||||
|
this.properties = new Map();
|
||||||
|
let reBlockStart = new RegExp(
|
||||||
|
'^' + this.io.blockStartPrefix + '(\\d+)\\n',
|
||||||
|
'gm'
|
||||||
|
);
|
||||||
|
let match = reBlockStart.exec(raw);
|
||||||
|
while ( match !== null ) {
|
||||||
|
let beg = match.index + match[0].length;
|
||||||
|
let end = raw.indexOf(this.io.blockEndPrefix + match[1], beg);
|
||||||
|
this.blocks.set(parseInt(match[1], 10), raw.slice(beg, end));
|
||||||
|
reBlockStart.lastIndex = end;
|
||||||
|
match = reBlockStart.exec(raw);
|
||||||
|
}
|
||||||
|
if ( blockId !== undefined ) {
|
||||||
|
this.select(blockId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let pos = this.block.indexOf('\n', this.offset);
|
next() {
|
||||||
if ( pos !== -1 ) {
|
if ( this.offset === this.len ) {
|
||||||
this.line = this.block.slice(this.offset, pos);
|
this.line = '';
|
||||||
this.offset = pos + 1;
|
return false;
|
||||||
} else {
|
}
|
||||||
this.line = this.block.slice(this.offset);
|
let pos = this.block.indexOf('\n', this.offset);
|
||||||
this.offset = this.len;
|
if ( pos !== -1 ) {
|
||||||
|
this.line = this.block.slice(this.offset, pos);
|
||||||
|
this.offset = pos + 1;
|
||||||
|
} else {
|
||||||
|
this.line = this.block.slice(this.offset);
|
||||||
|
this.offset = this.len;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
select(blockId) {
|
||||||
|
this.block = this.blocks.get(blockId) || '';
|
||||||
|
this.len = this.block.length;
|
||||||
|
this.offset = 0;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
fingerprint() {
|
||||||
|
return this.line;
|
||||||
|
}
|
||||||
|
args() {
|
||||||
|
return this.parser(this.line);
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
},
|
|
||||||
select: function(blockId) {
|
|
||||||
this.block = this.blocks.get(blockId) || '';
|
|
||||||
this.len = this.block.length;
|
|
||||||
this.offset = 0;
|
|
||||||
return this;
|
|
||||||
},
|
|
||||||
fingerprint: function() {
|
|
||||||
return this.line;
|
|
||||||
},
|
|
||||||
args: function() {
|
|
||||||
return this.parser(this.line);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -408,16 +404,15 @@
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
µBlock.MRUCache = function(size) {
|
µBlock.MRUCache = class {
|
||||||
this.size = size;
|
constructor(size) {
|
||||||
this.array = [];
|
this.size = size;
|
||||||
this.map = new Map();
|
this.array = [];
|
||||||
this.resetTime = Date.now();
|
this.map = new Map();
|
||||||
};
|
this.resetTime = Date.now();
|
||||||
|
}
|
||||||
µBlock.MRUCache.prototype = {
|
add(key, value) {
|
||||||
add: function(key, value) {
|
const found = this.map.has(key);
|
||||||
var found = this.map.has(key);
|
|
||||||
this.map.set(key, value);
|
this.map.set(key, value);
|
||||||
if ( !found ) {
|
if ( !found ) {
|
||||||
if ( this.array.length === this.size ) {
|
if ( this.array.length === this.size ) {
|
||||||
|
|
@ -425,24 +420,24 @@
|
||||||
}
|
}
|
||||||
this.array.unshift(key);
|
this.array.unshift(key);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
remove: function(key) {
|
remove(key) {
|
||||||
if ( this.map.has(key) ) {
|
if ( this.map.has(key) ) {
|
||||||
this.array.splice(this.array.indexOf(key), 1);
|
this.array.splice(this.array.indexOf(key), 1);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
lookup: function(key) {
|
lookup(key) {
|
||||||
var value = this.map.get(key);
|
const value = this.map.get(key);
|
||||||
if ( value !== undefined && this.array[0] !== key ) {
|
if ( value !== undefined && this.array[0] !== key ) {
|
||||||
var i = this.array.indexOf(key);
|
let i = this.array.indexOf(key);
|
||||||
do {
|
do {
|
||||||
this.array[i] = this.array[i-1];
|
this.array[i] = this.array[i-1];
|
||||||
} while ( --i );
|
} while ( --i );
|
||||||
this.array[0] = key;
|
this.array[0] = key;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
},
|
}
|
||||||
reset: function() {
|
reset() {
|
||||||
this.array = [];
|
this.array = [];
|
||||||
this.map.clear();
|
this.map.clear();
|
||||||
this.resetTime = Date.now();
|
this.resetTime = Date.now();
|
||||||
|
|
@ -459,27 +454,27 @@
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
µBlock.decomposeHostname = (function() {
|
µBlock.decomposeHostname = (( ) => {
|
||||||
// For performance purpose, as simple tests as possible
|
// For performance purpose, as simple tests as possible
|
||||||
let reHostnameVeryCoarse = /[g-z_-]/;
|
const reHostnameVeryCoarse = /[g-z_-]/;
|
||||||
let reIPv4VeryCoarse = /\.\d+$/;
|
const reIPv4VeryCoarse = /\.\d+$/;
|
||||||
|
|
||||||
let toBroaderHostname = function(hostname) {
|
const toBroaderHostname = function(hostname) {
|
||||||
let pos = hostname.indexOf('.');
|
const pos = hostname.indexOf('.');
|
||||||
if ( pos !== -1 ) {
|
if ( pos !== -1 ) {
|
||||||
return hostname.slice(pos + 1);
|
return hostname.slice(pos + 1);
|
||||||
}
|
}
|
||||||
return hostname !== '*' && hostname !== '' ? '*' : '';
|
return hostname !== '*' && hostname !== '' ? '*' : '';
|
||||||
};
|
};
|
||||||
|
|
||||||
let toBroaderIPv4Address = function(ipaddress) {
|
const toBroaderIPv4Address = function(ipaddress) {
|
||||||
if ( ipaddress === '*' || ipaddress === '' ) { return ''; }
|
if ( ipaddress === '*' || ipaddress === '' ) { return ''; }
|
||||||
let pos = ipaddress.lastIndexOf('.');
|
const pos = ipaddress.lastIndexOf('.');
|
||||||
if ( pos === -1 ) { return '*'; }
|
if ( pos === -1 ) { return '*'; }
|
||||||
return ipaddress.slice(0, pos);
|
return ipaddress.slice(0, pos);
|
||||||
};
|
};
|
||||||
|
|
||||||
let toBroaderIPv6Address = function(ipaddress) {
|
const toBroaderIPv6Address = function(ipaddress) {
|
||||||
return ipaddress !== '*' && ipaddress !== '' ? '*' : '';
|
return ipaddress !== '*' && ipaddress !== '' ? '*' : '';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -656,7 +651,7 @@
|
||||||
// Rename ./tmp/requests.json.gz to something else if you no longer want
|
// Rename ./tmp/requests.json.gz to something else if you no longer want
|
||||||
// ./assets/requests.json in the build.
|
// ./assets/requests.json in the build.
|
||||||
|
|
||||||
µBlock.loadBenchmarkDataset = (function() {
|
µBlock.loadBenchmarkDataset = (( ) => {
|
||||||
let datasetPromise;
|
let datasetPromise;
|
||||||
let ttlTimer;
|
let ttlTimer;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue