1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.assert = assert;
|
7 | exports.assertReturn = assertReturn;
|
8 | exports.assertUnreachable = assertUnreachable;
|
9 |
|
10 | var _function = require("./is/function");
|
11 |
|
12 | // Copyright 2017-2022 @polkadot/util authors & contributors
|
13 | // SPDX-License-Identifier: Apache-2.0
|
14 |
|
15 | /**
|
16 | * @name assert
|
17 | * @summary Checks for a valid test, if not Error is thrown.
|
18 | * @description
|
19 | * 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.
|
20 | * @example
|
21 | * <BR>
|
22 | *
|
23 | * ```javascript
|
24 | * const { assert } from '@polkadot/util';
|
25 | *
|
26 | * assert(true, 'True should be true'); // passes
|
27 | * assert(false, 'False should not be true'); // Error thrown
|
28 | * assert(false, () => 'message'); // Error with 'message'
|
29 | * ```
|
30 | */
|
31 | function assert(condition, message) {
|
32 | if (!condition) {
|
33 | throw new Error((0, _function.isFunction)(message) ? message() : message);
|
34 | }
|
35 | }
|
36 | /**
|
37 | * @name assertReturn
|
38 | * @description Returns when the value is not undefined/null, otherwise throws assertion error
|
39 | */
|
40 |
|
41 |
|
42 | function assertReturn(value, message) {
|
43 | assert(value !== undefined && value !== null, message);
|
44 | return value;
|
45 | }
|
46 | /**
|
47 | * @name assertUnreachable
|
48 | * @description An assertion helper that ensures all codepaths are followed
|
49 | */
|
50 |
|
51 |
|
52 | function assertUnreachable(x) {
|
53 | throw new Error(`This codepath should be unreachable. Unhandled input: ${x}`);
|
54 | } |
\ | No newline at end of file |