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