Expand assert to use message: string | () => string (#39)

* Expand assert to use message: string | () => string

* Update docs
This commit is contained in:
Jaco Greeff
2017-12-03 10:54:42 +01:00
committed by GitHub
parent 7644b7f77f
commit 6bf6425f76
11 changed files with 34 additions and 32 deletions
+1 -1
View File
@@ -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": {
+9 -3
View File
@@ -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);
};
+6
View File
@@ -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' });
+2 -3
View File
@@ -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));
};
+2 -3
View File
@@ -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')
+2 -3
View File
@@ -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);
+2 -3
View File
@@ -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);
};
+3 -6
View File
@@ -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');
};
+1 -1
View File
@@ -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)) {
+3 -6
View File
@@ -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);
};
+3 -3
View File
@@ -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"