From 5908aaa32ffa8f256d955b794a521f0d11636b4a Mon Sep 17 00:00:00 2001 From: Janez Urevc Date: Sat, 15 Sep 2018 22:24:24 +0200 Subject: [PATCH 1/4] Automated liting fixes to the files in src/util. --- src/util/buffer.js | 36 ++++++++++++++++++------------------ src/util/js.js | 28 ++++++++++++++-------------- src/util/preconditions.js | 22 ++++++++++------------ 3 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/util/buffer.js b/src/util/buffer.js index c78b9396..37e712ff 100644 --- a/src/util/buffer.js +++ b/src/util/buffer.js @@ -1,17 +1,17 @@ -'use strict'; -var buffer = require('buffer'); -var assert = require('assert'); -var js = require('./js'); -var $ = require('./preconditions'); +const buffer = require('buffer'); +const assert = require('assert'); + +const js = require('./js'); +const $ = require('./preconditions'); function equals(a, b) { if (a.length !== b.length) { return false; } - var length = a.length; - for (var i = 0; i < length; i++) { + const length = a.length; + for (let i = 0; i < length; i++) { if (a[i] !== b[i]) { return false; } @@ -30,8 +30,8 @@ module.exports = { fill: function fill(buffer, value) { $.checkArgumentType(buffer, 'Buffer', 'buffer'); $.checkArgumentType(value, 'number', 'value'); - var length = buffer.length; - for (var i = 0; i < length; i++) { + const length = buffer.length; + for (let i = 0; i < length; i++) { buffer[i] = value; } return buffer; @@ -43,8 +43,8 @@ module.exports = { * @param {Buffer} original * @return {Buffer} */ - copy: function(original) { - var buffer = new Buffer(original.length); + copy(original) { + const buffer = new Buffer(original.length); original.copy(buffer); return buffer; }, @@ -68,8 +68,8 @@ module.exports = { */ emptyBuffer: function emptyBuffer(bytes) { $.checkArgumentType(bytes, 'number', 'bytes'); - var result = new buffer.Buffer(bytes); - for (var i = 0; i < bytes; i++) { + const result = new buffer.Buffer(bytes); + for (let i = 0; i < bytes; i++) { result.write('\0', i); } return result; @@ -82,7 +82,7 @@ module.exports = { */ concat: buffer.Buffer.concat, - equals: equals, + equals, equal: equals, /** @@ -104,7 +104,7 @@ module.exports = { */ integerAsBuffer: function integerAsBuffer(integer) { $.checkArgumentType(integer, 'number', 'integer'); - var bytes = []; + const bytes = []; bytes.push((integer >> 24) & 0xff); bytes.push((integer >> 16) & 0xff); bytes.push((integer >> 8) & 0xff); @@ -152,8 +152,8 @@ module.exports = { * @return {Buffer} */ reverse: function reverse(param) { - var ret = new buffer.Buffer(param.length); - for (var i = 0; i < param.length; i++) { + const ret = new buffer.Buffer(param.length); + for (let i = 0; i < param.length; i++) { ret[i] = param[param.length - i - 1]; } return ret; @@ -170,7 +170,7 @@ module.exports = { hexToBuffer: function hexToBuffer(string) { assert(js.isHexa(string)); return new buffer.Buffer(string, 'hex'); - } + }, }; module.exports.NULL_HASH = module.exports.fill(new Buffer(32), 0); diff --git a/src/util/js.js b/src/util/js.js index c53e1e17..e7585e70 100644 --- a/src/util/js.js +++ b/src/util/js.js @@ -1,6 +1,6 @@ -'use strict'; -var _ = require('lodash'); + +const _ = require('lodash'); /** * Determines whether a string contains only hexadecimal values @@ -9,7 +9,7 @@ var _ = require('lodash'); * @param {string} value * @return {boolean} true if the string is the hexa representation of a number */ -var isHexa = function isHexa(value) { +const isHexa = function isHexa(value) { if (!_.isString(value)) { return false; } @@ -28,7 +28,7 @@ module.exports = { * @return {Object|boolean} false if the argument is not a JSON string. */ isValidJSON: function isValidJSON(arg) { - var parsed; + let parsed; if (!_.isString(arg)) { return false; } @@ -37,18 +37,18 @@ module.exports = { } catch (e) { return false; } - if (typeof(parsed) === 'object') { + if (typeof (parsed) === 'object') { return true; } return false; }, - isHexa: isHexa, + isHexa, isHexaString: isHexa, /** * Clone an array */ - cloneArray: function(array) { + cloneArray(array) { return [].concat(array); }, @@ -60,11 +60,11 @@ module.exports = { * @return {Object} The target object */ defineImmutable: function defineImmutable(target, values) { - Object.keys(values).forEach(function(key){ + Object.keys(values).forEach((key) => { Object.defineProperty(target, key, { configurable: false, enumerable: true, - value: values[key] + value: values[key], }); }); return target; @@ -76,9 +76,9 @@ module.exports = { * @return {Boolean} */ isNaturalNumber: function isNaturalNumber(value) { - return typeof value === 'number' && - isFinite(value) && - Math.floor(value) === value && - value >= 0; - } + return typeof value === 'number' + && isFinite(value) + && Math.floor(value) === value + && value >= 0; + }, }; diff --git a/src/util/preconditions.js b/src/util/preconditions.js index 43afc045..d5b06c2f 100644 --- a/src/util/preconditions.js +++ b/src/util/preconditions.js @@ -1,34 +1,32 @@ -'use strict'; -var errors = require('../errors'); -var _ = require('lodash'); + +const _ = require('lodash'); +const errors = require('../errors'); module.exports = { - checkState: function(condition, message) { + checkState(condition, message) { if (!condition) { throw new errors.InvalidState(message); } }, - checkArgument: function(condition, argumentName, message, docsPath) { + checkArgument(condition, argumentName, message, docsPath) { if (!condition) { throw new errors.InvalidArgument(argumentName, message, docsPath); } }, - checkArgumentType: function(argument, type, argumentName) { + checkArgumentType(argument, type, argumentName) { argumentName = argumentName || '(unknown name)'; if (_.isString(type)) { if (type === 'Buffer') { - var buffer = require('buffer'); // './buffer' fails on cordova & RN + const buffer = require('buffer'); // './buffer' fails on cordova & RN if (!buffer.Buffer.isBuffer(argument)) { throw new errors.InvalidArgumentType(argument, type, argumentName); } } else if (typeof argument !== type) { throw new errors.InvalidArgumentType(argument, type, argumentName); } - } else { - if (!(argument instanceof type)) { - throw new errors.InvalidArgumentType(argument, type.name, argumentName); - } + } else if (!(argument instanceof type)) { + throw new errors.InvalidArgumentType(argument, type.name, argumentName); } - } + }, }; From 0bc856e18ae335f21627c493935b630ac8aef927 Mon Sep 17 00:00:00 2001 From: Janez Urevc Date: Sat, 15 Sep 2018 22:32:01 +0200 Subject: [PATCH 2/4] Make src/util/buffer.js comply with the style guide. --- scripts/lint.js | 1 + src/util/buffer.js | 68 ++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/scripts/lint.js b/scripts/lint.js index 2190b3fc..28fc1af7 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -19,5 +19,6 @@ const toTest = [ 'src/crypto/ecdsa.js', 'src/crypto/random.js', 'src/crypto/signature.js', + 'src/util/buffer.js', ].join(' '); process.exit(shell.exec(`./node_modules/.bin/eslint ${toTest}`).code); diff --git a/src/util/buffer.js b/src/util/buffer.js index 37e712ff..85bf6836 100644 --- a/src/util/buffer.js +++ b/src/util/buffer.js @@ -1,5 +1,3 @@ - - const buffer = require('buffer'); const assert = require('assert'); @@ -10,8 +8,8 @@ function equals(a, b) { if (a.length !== b.length) { return false; } - const length = a.length; - for (let i = 0; i < length; i++) { + const { length } = a; + for (let i = 0; i < length; i += 1) { if (a[i] !== b[i]) { return false; } @@ -23,18 +21,18 @@ module.exports = { /** * Fill a buffer with a value. * - * @param {Buffer} buffer + * @param {Buffer} buff * @param {number} value * @return {Buffer} */ - fill: function fill(buffer, value) { - $.checkArgumentType(buffer, 'Buffer', 'buffer'); + fill: function fill(buff, value) { + $.checkArgumentType(buff, 'Buffer', 'buffer'); $.checkArgumentType(value, 'number', 'value'); - const length = buffer.length; - for (let i = 0; i < length; i++) { - buffer[i] = value; + const { length } = buff; + for (let i = 0; i < length; i += 1) { + buff[i] = value; } - return buffer; + return buff; }, /** @@ -44,9 +42,9 @@ module.exports = { * @return {Buffer} */ copy(original) { - const buffer = new Buffer(original.length); - original.copy(buffer); - return buffer; + const buff = Buffer.alloc(original.length); + original.copy(buff); + return buff; }, /** @@ -68,8 +66,8 @@ module.exports = { */ emptyBuffer: function emptyBuffer(bytes) { $.checkArgumentType(bytes, 'number', 'bytes'); - const result = new buffer.Buffer(bytes); - for (let i = 0; i < bytes; i++) { + const result = Buffer.alloc(bytes); + for (let i = 0; i < bytes; i += 1) { result.write('\0', i); } return result; @@ -93,7 +91,7 @@ module.exports = { */ integerAsSingleByteBuffer: function integerAsSingleByteBuffer(integer) { $.checkArgumentType(integer, 'number', 'integer'); - return new buffer.Buffer([integer & 0xff]); + return Buffer.from([integer & 0xff]); }, /** @@ -109,28 +107,28 @@ module.exports = { bytes.push((integer >> 16) & 0xff); bytes.push((integer >> 8) & 0xff); bytes.push(integer & 0xff); - return new Buffer(bytes); + return Buffer.from(bytes); }, /** * Transform the first 4 values of a Buffer into a number, in little endian encoding * - * @param {Buffer} buffer + * @param {Buffer} buff * @return {number} */ - integerFromBuffer: function integerFromBuffer(buffer) { - $.checkArgumentType(buffer, 'Buffer', 'buffer'); - return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]; + integerFromBuffer: function integerFromBuffer(buff) { + $.checkArgumentType(buff, 'Buffer', 'buffer'); + return (buff[0] << 24) | (buff[1] << 16) | (buff[2] << 8) | (buff[3]); }, /** * Transforms the first byte of an array into a number ranging from -128 to 127 - * @param {Buffer} buffer + * @param {Buffer} buff * @return {number} */ - integerFromSingleByteBuffer: function integerFromBuffer(buffer) { - $.checkArgumentType(buffer, 'Buffer', 'buffer'); - return buffer[0]; + integerFromSingleByteBuffer: function integerFromBuffer(buff) { + $.checkArgumentType(buff, 'Buffer', 'buffer'); + return buff[0]; }, /** @@ -138,12 +136,12 @@ module.exports = { * * Shorthand for buffer.toString('hex') * - * @param {Buffer} buffer + * @param {Buffer} buff * @return {string} */ - bufferToHex: function bufferToHex(buffer) { - $.checkArgumentType(buffer, 'Buffer', 'buffer'); - return buffer.toString('hex'); + bufferToHex: function bufferToHex(buff) { + $.checkArgumentType(buff, 'Buffer', 'buffer'); + return buff.toString('hex'); }, /** @@ -152,8 +150,8 @@ module.exports = { * @return {Buffer} */ reverse: function reverse(param) { - const ret = new buffer.Buffer(param.length); - for (let i = 0; i < param.length; i++) { + const ret = Buffer.alloc(param.length); + for (let i = 0; i < param.length; i += 1) { ret[i] = param[param.length - i - 1]; } return ret; @@ -169,9 +167,9 @@ module.exports = { */ hexToBuffer: function hexToBuffer(string) { assert(js.isHexa(string)); - return new buffer.Buffer(string, 'hex'); + return Buffer.from(string, 'hex'); }, }; -module.exports.NULL_HASH = module.exports.fill(new Buffer(32), 0); -module.exports.EMPTY_BUFFER = new Buffer(0); +module.exports.NULL_HASH = module.exports.fill(Buffer.alloc(32), 0); +module.exports.EMPTY_BUFFER = Buffer.alloc(0); From 6490ba05b6a3dd86ab0dfe873f272c870904acf8 Mon Sep 17 00:00:00 2001 From: Janez Urevc Date: Sat, 15 Sep 2018 22:35:31 +0200 Subject: [PATCH 3/4] Make src/util/js.js comply with the style guide. --- README.md | 4 ++-- scripts/lint.js | 1 + src/util/js.js | 4 +--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1ee8d493..1ed820fd 100644 --- a/README.md +++ b/README.md @@ -133,8 +133,8 @@ $ npm test | transaction/unspentoutput.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | | unit.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | | uri.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | -| util/buffer.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | -| util/js.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | +| util/buffer.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | +| util/js.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | | util/preconditions.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | diff --git a/scripts/lint.js b/scripts/lint.js index 28fc1af7..4302004a 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -20,5 +20,6 @@ const toTest = [ 'src/crypto/random.js', 'src/crypto/signature.js', 'src/util/buffer.js', + 'src/util/js.js', ].join(' '); process.exit(shell.exec(`./node_modules/.bin/eslint ${toTest}`).code); diff --git a/src/util/js.js b/src/util/js.js index e7585e70..6f686a31 100644 --- a/src/util/js.js +++ b/src/util/js.js @@ -1,5 +1,3 @@ - - const _ = require('lodash'); /** @@ -77,7 +75,7 @@ module.exports = { */ isNaturalNumber: function isNaturalNumber(value) { return typeof value === 'number' - && isFinite(value) + && Number.isFinite(value) && Math.floor(value) === value && value >= 0; }, From 1be574808efa77cbd32ca7031ef67985475ec3c0 Mon Sep 17 00:00:00 2001 From: Janez Urevc Date: Sat, 15 Sep 2018 22:43:12 +0200 Subject: [PATCH 4/4] Make src/util/preconditions.js comply with the style guide. --- README.md | 2 +- scripts/lint.js | 3 +-- src/util/preconditions.js | 6 ++---- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1ed820fd..ce547c14 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ $ npm test | uri.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | | util/buffer.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | | util/js.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | -| util/preconditions.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | +| util/preconditions.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | diff --git a/scripts/lint.js b/scripts/lint.js index 4302004a..13a341bb 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -16,10 +16,9 @@ const toTest = [ 'src/block/', 'src/mnemonic/', 'src/transaction/', + 'src/util/', 'src/crypto/ecdsa.js', 'src/crypto/random.js', 'src/crypto/signature.js', - 'src/util/buffer.js', - 'src/util/js.js', ].join(' '); process.exit(shell.exec(`./node_modules/.bin/eslint ${toTest}`).code); diff --git a/src/util/preconditions.js b/src/util/preconditions.js index d5b06c2f..182ea140 100644 --- a/src/util/preconditions.js +++ b/src/util/preconditions.js @@ -1,6 +1,5 @@ - - const _ = require('lodash'); +const buffer = require('buffer'); const errors = require('../errors'); module.exports = { @@ -18,11 +17,10 @@ module.exports = { argumentName = argumentName || '(unknown name)'; if (_.isString(type)) { if (type === 'Buffer') { - const buffer = require('buffer'); // './buffer' fails on cordova & RN if (!buffer.Buffer.isBuffer(argument)) { throw new errors.InvalidArgumentType(argument, type, argumentName); } - } else if (typeof argument !== type) { + } else if (typeof argument !== type) { // eslint-disable-line valid-typeof throw new errors.InvalidArgumentType(argument, type, argumentName); } } else if (!(argument instanceof type)) {