Files
common/packages/util/src/assert.ts
T
Jaco Greeff 89030f41b3 2019 (#287)
* 2019

* Bump deps

* Cleanup deps

* Update dev (markdown plugin)

* Bump deps (hopefully finally fixes doc gen)

* Fix inherit plugin
2019-01-04 14:50:50 +01:00

38 lines
1.3 KiB
TypeScript

// Copyright 2017-2019 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import ExtError from './ext/error';
import isFunction from './is/function';
type MessageFn = () => string;
/**
* @name assert
* @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.
* @example
* <BR>
*
* ```javascript
* const { assert } from '@polkadot/util';
*
* assert(true, 'True should be true'); // true returned
* assert(false, 'False should not be true'); // ExtError thrown
* assert(false, () => 'message'); // ExtError with 'message'
* ```
*/
export default function assert (test: any, message: string | MessageFn, code: number = ExtError.CODES.ASSERT, data?: any): boolean {
if (test) {
return true;
}
if (isFunction(message)) {
message = message();
}
throw new ExtError(message, code, data);
}