From 6bf6425f761e270c00a174f99965a287087b9817 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 3 Dec 2017 10:54:42 +0100 Subject: [PATCH] Expand assert to use message: string | () => string (#39) * Expand assert to use message: string | () => string * Update docs --- package.json | 2 +- src/assert.js | 12 +++++++++--- src/assert.spec.js | 6 ++++++ src/bn/toHex.js | 5 ++--- src/buffer/toHex.js | 5 ++--- src/buffer/toU8a.js | 5 ++--- src/hex/toBn.js | 5 ++--- src/hex/toBuffer.js | 9 +++------ src/number/toBuffer.js | 2 +- src/u8a/toBuffer.js | 9 +++------ yarn.lock | 6 +++--- 11 files changed, 34 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 6252765a3..b67ee3e5a 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/api-jsonrpc": "^0.5.9", + "@polkadot/api-jsonrpc": "^0.5.11", "@polkadot/dev": "^0.12.6" }, "dependencies": { diff --git a/src/assert.js b/src/assert.js index 98a119772..f6a96f7d2 100644 --- a/src/assert.js +++ b/src/assert.js @@ -2,10 +2,11 @@ // @flow const ExtError = require('./ext/error'); +const isFunction = require('./is/function'); /** @name assert - @signature assert (test: boolean, message: string, code: number = ExtError.CODES.ASSERT, data: any): void + @signature assert (test: any, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: any): void @summary Checks for a valid test, if not ExtError is thrown. @description Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned. @@ -14,11 +15,16 @@ const ExtError = require('./ext/error'); assert(true, 'True should be true'); // true returned assert(false, 'False should not be true'); // ExtError thrown + assert(false, () => 'message'); // ExtError with 'message' */ -module.exports = function assert (test: any, message: string, code: number = ExtError.CODES.ASSERT, data: any): boolean { +module.exports = function assert (test: any, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: any): boolean { if (test) { return true; } - throw new ExtError(message, code, data); + if (isFunction(message)) { + message = ((message: any): () => string)(); + } + + throw new ExtError(((message: any): string), code, data); }; diff --git a/src/assert.spec.js b/src/assert.spec.js index 4d98732e7..d992b52c5 100644 --- a/src/assert.spec.js +++ b/src/assert.spec.js @@ -15,6 +15,12 @@ describe('assert', () => { ).toThrow(/error thrown/); }); + it('should throw an error when message: () => string', () => { + expect( + () => assert(false, () => 'message from function') + ).toThrow(/message from function/); + }); + it('throws a valid constructed ExtError', () => { try { assert(false, 'error', -666, { some: 'data' }); diff --git a/src/bn/toHex.js b/src/bn/toHex.js index bff7193b0..5080f15d6 100644 --- a/src/bn/toHex.js +++ b/src/bn/toHex.js @@ -3,6 +3,7 @@ const BN = require('bn.js'); +const assert = require('../assert'); const hexAddPrefix = require('../hex/addPrefix'); const isBN = require('../is/bn'); @@ -25,9 +26,7 @@ module.exports = function bnToHex (value?: BN): string { return ZERO_STR; } - if (!isBN(value)) { - throw new Error(`Cannot convert from non-BN value '${(value: any)}' to hex`); - } + assert(isBN(value), `Cannot convert from non-BN value '${(value: any)}' to hex`); return hexAddPrefix(value.toString(16)); }; diff --git a/src/buffer/toHex.js b/src/buffer/toHex.js index 17653628a..e56a564cd 100644 --- a/src/buffer/toHex.js +++ b/src/buffer/toHex.js @@ -1,6 +1,7 @@ // ISC, Copyright 2017 Jaco Greeff // @flow +const assert = require('../assert'); const isBuffer = require('../is/buffer'); const hexAddPrefix = require('../hex/addPrefix'); @@ -22,9 +23,7 @@ module.exports = function bufferToHex (value?: Buffer): string { return ZERO_HEX; } - if (!isBuffer(value)) { - throw new Error(`Cannot convert non-buffer to hex`); - } + assert(isBuffer(value), `Cannot convert non-buffer to hex`); return hexAddPrefix( value.toString('hex') diff --git a/src/buffer/toU8a.js b/src/buffer/toU8a.js index ba0a9ab19..883e4d1b7 100644 --- a/src/buffer/toU8a.js +++ b/src/buffer/toU8a.js @@ -1,6 +1,7 @@ // ISC, Copyright 2017 Jaco Greeff // @flow +const assert = require('../assert'); const isBuffer = require('../is/buffer'); /** @@ -19,9 +20,7 @@ module.exports = function bufferToU8a (buffer?: Buffer): Uint8Array { return new Uint8Array([]); } - if (!isBuffer(buffer)) { - throw new Error('Cannot convert non-buffer to Uint8Array'); - } + assert(isBuffer(buffer), 'Cannot convert non-buffer to Uint8Array'); const array = new Uint8Array(buffer.length); diff --git a/src/hex/toBn.js b/src/hex/toBn.js index d124e6c57..a9773383d 100644 --- a/src/hex/toBn.js +++ b/src/hex/toBn.js @@ -3,6 +3,7 @@ const BN = require('bn.js'); +const assert = require('../assert'); const hexStripPrefix = require('./stripPrefix'); const isHex = require('../is/hex'); @@ -24,9 +25,7 @@ module.exports = function hexToBn (value?: string): BN { return ZERO_BN; } - if (!isHex(value)) { - throw new Error(`Cannot convert from non-hex value '${value}' to BN`); - } + assert(isHex(value), `Cannot convert from non-hex value '${value}' to BN`); return new BN(hexStripPrefix(value), 16); }; diff --git a/src/hex/toBuffer.js b/src/hex/toBuffer.js index 43c30633d..8a28f6f30 100644 --- a/src/hex/toBuffer.js +++ b/src/hex/toBuffer.js @@ -1,11 +1,10 @@ // ISC, Copyright 2017 Jaco Greeff // @flow +const assert = require('../assert'); const isHex = require('../is/hex'); const hexStripPrefix = require('./stripPrefix'); -const EMPTY_BUFFER = Buffer.from([]); - /** @name hexToBuffer @signature hexToBuffer (value?: string): Buffer @@ -19,12 +18,10 @@ const EMPTY_BUFFER = Buffer.from([]); */ module.exports = function hexToBuffer (value?: string): Buffer { if (!value) { - return EMPTY_BUFFER; + return Buffer.from([]); } - if (!isHex(value)) { - throw new Error(`Cannot convert non-hex value '${value}' to Buffer`); - } + assert(isHex(value), `Cannot convert non-hex value '${value}' to Buffer`); return Buffer.from(hexStripPrefix(value), 'hex'); }; diff --git a/src/number/toBuffer.js b/src/number/toBuffer.js index 2ea53a65a..8b339b5c6 100644 --- a/src/number/toBuffer.js +++ b/src/number/toBuffer.js @@ -15,7 +15,7 @@ const numberToHex = require('./toHex'); @example import { numberToBuffer } from '@polkadot/util'; - const buffer = numberToBuffer('0x1234'); // => Buffer.from([0x12, 0x34]) + const buffer = numberToBuffer(0x1234); // => Buffer.from([0x12, 0x34]) */ module.exports = function numberToBuffer (value?: number): Buffer { if (isUndefined(value) || isNull(value) || isNaN(value)) { diff --git a/src/u8a/toBuffer.js b/src/u8a/toBuffer.js index a4c083a4c..ae3b41935 100644 --- a/src/u8a/toBuffer.js +++ b/src/u8a/toBuffer.js @@ -1,10 +1,9 @@ // ISC, Copyright 2017 Jaco Greeff // @flow +const assert = require('../assert'); const isUint8Array = require('../is/uint8Array'); -const EMPTY_BUFFER = Buffer.from([]); - /** @name u8aToBuffer @signature u8aToBuffer (value?: UInt8Array): Buffer @@ -18,12 +17,10 @@ const EMPTY_BUFFER = Buffer.from([]); */ module.exports = function u8aToBuffer (value?: Uint8Array): Buffer { if (!value) { - return EMPTY_BUFFER; + return Buffer.from([]); } - if (!isUint8Array(value)) { - throw new Error('Cannot convert non-Uint8Array value to Buffer'); - } + assert(isUint8Array(value), 'Cannot convert non-Uint8Array value to Buffer'); return Buffer.from(value); }; diff --git a/yarn.lock b/yarn.lock index d3ac36b96..8861b84bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -54,9 +54,9 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" -"@polkadot/api-jsonrpc@^0.5.9": - version "0.5.9" - resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.5.9.tgz#615311c4f7c73b95cb52e910250ecc712f695732" +"@polkadot/api-jsonrpc@^0.5.11": + version "0.5.11" + resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.5.11.tgz#a03190a8645c942a0893fa8ec1b4990432c62242" dependencies: babel-runtime "^6.26.0"