Skip to content

Commit

Permalink
refactor: use es6 template strings everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jul 17, 2017
1 parent 3af0141 commit 296e65d
Show file tree
Hide file tree
Showing 56 changed files with 227 additions and 268 deletions.
2 changes: 1 addition & 1 deletion bench/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const BufferWriter = require('../lib/utils/writer');
const StaticWriter = require('../lib/utils/staticwriter');
const bench = require('./bench');

let wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8');
let wtx = fs.readFileSync(`${__dirname}/../test/data/wtx.hex`, 'utf8');
let i, tx, end;

wtx = Buffer.from(wtx.trim(), 'hex');
Expand Down
2 changes: 1 addition & 1 deletion bench/coins-old.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const bench = require('./bench');
const Coins = require('../migrate/coins-old');
const TX = require('../lib/primitives/tx');

let wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8');
let wtx = fs.readFileSync(`${__dirname}/../test/data/wtx.hex`, 'utf8');
wtx = TX.fromRaw(wtx.trim(), 'hex');

let coins = Coins.fromTX(wtx);
Expand Down
2 changes: 1 addition & 1 deletion bench/coins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Coins = require('../lib/coins/coins');
const TX = require('../lib/primitives/tx');
const bench = require('./bench');

let raw = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8');
let raw = fs.readFileSync(`${__dirname}/../test/data/wtx.hex`, 'utf8');
let wtx = TX.fromRaw(raw.trim(), 'hex');
let coins = Coins.fromTX(wtx, 1);
let i, j, end, hash;
Expand Down
4 changes: 2 additions & 2 deletions bench/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let block = Block.fromJSON(json);
let btx = { tx: block.txs[397], view: new CoinView() };

let tx3 = parseTX('../test/data/tx3.hex');
let wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8');
let wtx = fs.readFileSync(`${__dirname}/../test/data/wtx.hex`, 'utf8');
let i, tx, end, flags, input;

wtx = Buffer.from(wtx.trim(), 'hex');
Expand All @@ -29,7 +29,7 @@ for (i = 0; i < tx.inputs.length; i++) {
}

function parseTX(file) {
let data = fs.readFileSync(__dirname + '/' + file, 'utf8');
let data = fs.readFileSync(`${__dirname}/${file}`, 'utf8');
let parts = data.trim().split(/\n+/);
let raw = parts[0];
let tx = TX.fromRaw(raw.trim(), 'hex');
Expand Down
10 changes: 5 additions & 5 deletions browser/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const WSProxy = require('./wsproxy');
const fs = require('fs');
const server, proxy;

const index = fs.readFileSync(__dirname + '/index.html');
const indexjs = fs.readFileSync(__dirname + '/index.js');
const bcoin = fs.readFileSync(__dirname + '/bcoin.js');
const master = fs.readFileSync(__dirname + '/bcoin-master.js');
const worker = fs.readFileSync(__dirname + '/bcoin-worker.js');
const index = fs.readFileSync(`${__dirname}/index.html`);
const indexjs = fs.readFileSync(`${__dirname}/index.js`);
const bcoin = fs.readFileSync(`${__dirname}/bcoin.js`);
const master = fs.readFileSync(`${__dirname}/bcoin-master.js`);
const worker = fs.readFileSync(`${__dirname}/bcoin-worker.js`);

proxy = new WSProxy({
pow: process.argv.indexOf('--pow') !== -1,
Expand Down
5 changes: 3 additions & 2 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'use strict';

const assert = require('assert');
const path = require('path');
const AsyncObject = require('../utils/asyncobject');
const Network = require('../protocol/network');
const Logger = require('../node/logger');
Expand Down Expand Up @@ -2397,8 +2398,8 @@ ChainOptions.prototype.fromOptions = function fromOptions(options) {
assert(typeof options.prefix === 'string');
this.prefix = options.prefix;
this.location = this.spv
? this.prefix + '/spvchain'
: this.prefix + '/chain';
? path.join(this.prefix, 'spvchain')
: path.join(this.prefix, 'chain');
}

if (options.location != null) {
Expand Down
4 changes: 2 additions & 2 deletions lib/btc/amount.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ Amount.from = function from(unit, value, num) {
*/

Amount.prototype.inspect = function inspect() {
return '<Amount: ' + this.toString() + '>';
return `<Amount: ${this.toString()}>`;
};

/**
Expand Down Expand Up @@ -360,7 +360,7 @@ Amount.serialize = function serialize(value, exp, num) {
if (lo.length === 0)
lo += '0';

result = hi + '.' + lo;
result = `${hi}.${lo}`;

if (negative)
result = '-' + result;
Expand Down
12 changes: 6 additions & 6 deletions lib/btc/uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ URI.prototype.toString = function toString() {
str += this.address.toString();

if (this.amount !== -1)
query.push('amount=' + Amount.btc(this.amount));
query.push(`amount=${Amount.btc(this.amount)}`);

if (this.label)
query.push('label=' + escape(this.label));
query.push(`label=${escape(this.label)}`);

if (this.message)
query.push('message=' + escape(this.message));
query.push(`message=${escape(this.message)}`);

if (this.request)
query.push('r=' + escape(this.request));
query.push(`r=${escape(this.request)}`);

if (query.length > 0)
str += '?' + query.join('&');
Expand All @@ -184,7 +184,7 @@ URI.prototype.toString = function toString() {
*/

URI.prototype.inspect = function inspect() {
return '<URI: ' + this.toString() + '>';
return `<URI: ${this.toString()}>`;
};

/*
Expand Down Expand Up @@ -240,7 +240,7 @@ function parsePairs(str) {
data.r = unescape(value);
break;
default:
assert(false, 'Unknown querystring key: ' + value);
assert(false, `Unknown querystring key: ${value}.`);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ exports.sign = function sign(alg, msg, key) {
*/

function normalizeAlg(alg, hash) {
return alg.toUpperCase() + '-' + hash.toUpperCase();
return `${alg.toUpperCase()}-${hash.toUpperCase()}`;
}
13 changes: 5 additions & 8 deletions lib/db/ldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ LDB.getName = function getName(db) {
break;
}

return {
name: name,
ext: ext
};
return [name, ext];
};

/**
Expand All @@ -81,18 +78,18 @@ LDB.getName = function getName(db) {
*/

LDB.getBackend = function getBackend(options) {
let result = LDB.getName(options.db);
let backend = backends.get(result.name);
let [name, ext] = LDB.getName(options.db);
let backend = backends.get(name);
let location = options.location;

if (typeof location !== 'string') {
assert(result.name === 'memory', 'Location required.');
assert(name === 'memory', 'Location required.');
location = 'memory';
}

return {
backend: backend,
location: location + '.' + result.ext
location: `${location}.${ext}`
};
};

Expand Down
2 changes: 1 addition & 1 deletion lib/hd/mnemonic.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ Mnemonic.prototype.toString = function toString() {
*/

Mnemonic.prototype.inspect = function inspect() {
return '<Mnemonic: ' + this.getPhrase() + '>';
return `<Mnemonic: ${this.getPhrase()}>`;
};

/**
Expand Down
7 changes: 4 additions & 3 deletions lib/http/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'use strict';

const assert = require('assert');
const path = require('path');
const EventEmitter = require('events');
const URL = require('url');
const {StringDecoder} = require('string_decoder');
Expand Down Expand Up @@ -205,7 +206,7 @@ HTTPBase.prototype.basicAuth = function basicAuth(options) {
assert(typeof realm === 'string');

function fail(res) {
res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
res.setHeader('WWW-Authenticate', `Basic realm="${realm}"`);
res.setStatus(401);
res.end();
}
Expand Down Expand Up @@ -879,8 +880,8 @@ HTTPBaseOptions.prototype.fromOptions = function fromOptions(options) {
if (options.prefix != null) {
assert(typeof options.prefix === 'string');
this.prefix = options.prefix;
this.keyFile = this.prefix + '/key.pem';
this.certFile = this.prefix + '/cert.pem';
this.keyFile = path.join(this.prefix, 'key.pem');
this.certFile = path.join(this.prefix, 'cert.pem');
}

if (options.ssl != null) {
Expand Down
6 changes: 3 additions & 3 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ RequestOptions.prototype.getHeaders = function getHeaders() {
headers['Content-Length'] = this.body.length + '';

if (this.auth) {
let auth = this.auth.username + ':' + this.auth.password;
headers['Authorization'] =
'Basic ' + Buffer.from(auth, 'utf8').toString('base64');
let auth = `${this.auth.username}:${this.auth.password}`;
let data = Buffer.from(auth, 'utf8');
headers['Authorization'] = `Basic ${data.toString('base64')}`;
}

return headers;
Expand Down
10 changes: 6 additions & 4 deletions lib/net/packets.js
Original file line number Diff line number Diff line change
Expand Up @@ -1709,11 +1709,13 @@ RejectPacket.fromError = function fromError(err, obj) {
*/

RejectPacket.prototype.inspect = function inspect() {
let code = RejectPacket.codesByVal[this.code] || this.code;
let hash = this.hash ? util.revHex(this.hash) : null;
return '<Reject:'
+ ' msg=' + this.message
+ ' code=' + (RejectPacket.codesByVal[this.code] || this.code)
+ ' reason=' + this.reason
+ ' hash=' + (this.hash ? util.revHex(this.hash) : null)
+ ` msg=${this.message}`
+ ` code=${code}`
+ ` reason=${this.reason}`
+ ` hash=${hash}`
+ '>';
};

Expand Down
8 changes: 4 additions & 4 deletions lib/net/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2229,10 +2229,10 @@ Peer.prototype.hasCompact = function hasCompact() {

Peer.prototype.inspect = function inspect() {
return '<Peer:'
+ ' handshake=' + this.handshake
+ ' host=' + this.hostname()
+ ' outbound=' + this.outbound
+ ' ping=' + this.minPing
+ ` handshake=${this.handshake}`
+ ` host=${this.hostname()}`
+ ` outbound=${this.outbound}`
+ ` ping=${this.minPing}`
+ '>';
};

Expand Down
7 changes: 3 additions & 4 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -4377,10 +4377,9 @@ BroadcastItem.prototype.handleReject = function handleReject(peer) {
*/

BroadcastItem.prototype.inspect = function inspect() {
return '<BroadcastItem:'
+ ' type=' + (this.type === invTypes.TX ? 'tx' : 'block')
+ ' hash=' + util.revHex(this.hash)
+ '>';
let type = this.type === invTypes.TX ? 'tx' : 'block';
let hash = util.revHex(this.hash);
return `<BroadcastItem: type=${type} hash=${hash}>`;
};

/**
Expand Down
31 changes: 16 additions & 15 deletions lib/net/upnp.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ UPNP.prototype._discover = async function discover() {

msg = ''
+ 'M-SEARCH * HTTP/1.1\r\n'
+ 'HOST: ' + this.host + ':' + this.port + '\r\n'
+ `HOST: ${this.host}:${this.port}\r\n`
+ 'MAN: ssdp:discover\r\n'
+ 'MX: 10\r\n'
+ 'ST: ssdp:all\r\n';
Expand Down Expand Up @@ -337,25 +337,25 @@ function UPNPService(options) {
*/

UPNPService.prototype.createRequest = function createRequest(action, args) {
let type = JSON.stringify(this.serviceType);
let params = '';

for (let arg of args) {
params += '<' + arg[0]+ '>';
if (arg.length > 1)
params += arg[1];
params += '</' + arg[0] + '>';
for (let [key, value] of args) {
params += `<${key}>`;
if (value != null)
params += value;
params += `</${key}>`;
}

return ''
+ '<?xml version="1.0"?>'
+ '<s:Envelope '
+ 'xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" '
+ 's:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
+ '<s:Envelope'
+ ' xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"'
+ ' s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
+ '<s:Body>'
+ '<u:' + action + ' xmlns:u='
+ JSON.stringify(this.serviceType) + '>'
+ params
+ '</u:' + action + '>'
+ `<u:${action} xmlns:u=${type}>`
+ `${params}`
+ `</u:${action}>`
+ '</s:Body>'
+ '</s:Envelope>';
};
Expand All @@ -369,6 +369,7 @@ UPNPService.prototype.createRequest = function createRequest(action, args) {
*/

UPNPService.prototype.soapRequest = async function soapRequest(action, args) {
let type = this.serviceType;
let req = this.createRequest(action, args);
let res, xml, err;

Expand All @@ -381,7 +382,7 @@ UPNPService.prototype.soapRequest = async function soapRequest(action, args) {
'Content-Type': 'text/xml; charset="utf-8"',
'Content-Length': Buffer.byteLength(req, 'utf8') + '',
'Connection': 'close',
'SOAPAction': JSON.stringify(this.serviceType + '#' + action)
'SOAPAction': JSON.stringify(`${type}#${action}`)
},
body: req
});
Expand Down Expand Up @@ -701,7 +702,7 @@ function parseHost(uri) {
assert(data.protocol === 'http:' || data.protocol === 'https:',
'Bad URL for location.');

return data.protocol + '//' + data.host;
return `${data.protocol}//${data.host}`;
}

function prependHost(host, uri) {
Expand Down
4 changes: 2 additions & 2 deletions lib/node/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Config(module) {

this.module = module;
this.network = 'main';
this.prefix = path.join(HOME, '.' + module);
this.prefix = path.join(HOME, `.${module}`);

this.options = Object.create(null);
this.data = Object.create(null);
Expand Down Expand Up @@ -592,7 +592,7 @@ Config.prototype.getPrefix = function getPrefix() {
return prefix;
}

prefix = path.join(HOME, '.' + this.module);
prefix = path.join(HOME, `.${this.module}`);
network = this.str('network');

if (network) {
Expand Down
Loading

0 comments on commit 296e65d

Please sign in to comment.