* Bump dev w/ eslint * Mop-up remaining lint issues * Update stringShorten expectation
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
// Copyright 2017-2020 @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 isFunction from './is/function';
|
|
|
|
type MessageFn = () => string;
|
|
|
|
/**
|
|
* @name assert
|
|
* @summary Checks for a valid test, if not Error is thrown.
|
|
* @description
|
|
* Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an Error with the supplied `message`. When `test` passes, `true` is returned.
|
|
* @example
|
|
* <BR>
|
|
*
|
|
* ```javascript
|
|
* const { assert } from '@polkadot/util';
|
|
*
|
|
* assert(true, 'True should be true'); // passes
|
|
* assert(false, 'False should not be true'); // Error thrown
|
|
* assert(false, () => 'message'); // Error with 'message'
|
|
* ```
|
|
*/
|
|
export default function assert (condition: unknown, message: string | MessageFn): asserts condition {
|
|
if (!condition) {
|
|
throw new Error(
|
|
isFunction(message)
|
|
? message()
|
|
: message
|
|
);
|
|
}
|
|
}
|