-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassert.util.ts
127 lines (112 loc) · 3.92 KB
/
assert.util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { ObjectUtil } from '@app-core/util';
import { Nullable, NullableOrUndefined } from '@app-core/types';
import { Function0, isFFunction0, TFunction0 } from '@app-core/types/function';
import { IllegalArgumentError } from '@app-core/errors';
/**
* Helper functions to validate given information.
*/
export class AssertUtil {
constructor() {
throw new SyntaxError('AssertUtil is an utility class');
}
/**
* Checks if the given `value` is `true`.
*
* @param value
* Value to check
* @param message
* Custom message to include more information about the error
*
* @return `true` if `value` is `true`,
* {IllegalArgumentError} if `value` is `undefined`, `null` or `false`
*
* @throws {IllegalArgumentError} if `value` is `undefined`, `null` or `false`
*/
static isTrue(value: NullableOrUndefined<boolean>,
message?: Nullable<string>): boolean;
/**
* Checks if the given `value` is `true`.
*
* @param value
* Value to check
* @param errorSupplier
* {@link TFunction0} used to provide the returned {@link Error}
*
* @return `true` if `value` is `true`,
* {IllegalArgumentError} if `value` is `null` or `undefined`
* if `value` is `false`:
* Custom {@link Error} if `errorSupplier` is defined,
* {IllegalArgumentError} otherwise
*
* @throws {IllegalArgumentError} if `value` is `undefined`, `null` or `false`
*/
static isTrue(value: NullableOrUndefined<boolean>,
errorSupplier?: Nullable<TFunction0<Error>>): boolean;
static isTrue(value: NullableOrUndefined<boolean>,
errorSupplierOrMessage?: Nullable<TFunction0<Error> | string>): boolean {
if (value) {
return true;
}
AssertUtil.notNullOrUndefined(
value,
'value is null or undefined'
);
if (Function0.isFunction(errorSupplierOrMessage) || isFFunction0(errorSupplierOrMessage)) {
throw Function0.of(errorSupplierOrMessage)
.apply();
}
throw new IllegalArgumentError(
errorSupplierOrMessage
? errorSupplierOrMessage
: 'value is false'
);
}
/**
* Checks if the given `value` is `undefined` or `null`.
*
* @param value
* Value to check
* @param message
* Custom message to include more information about the error
*
* @return `true` if it is neither `undefined` nor `null`,
* {IllegalArgumentError} otherwise
*
* @throws {IllegalArgumentError} if `value` is `undefined` or `null`
*/
static notNullOrUndefined(value: NullableOrUndefined<any>,
message?: Nullable<string>): boolean;
/**
* Checks if the given `value` is `undefined` or `null`.
*
* @param value
* Value to check
* @param errorSupplier
* {@link TFunction0} used to provide the returned {@link Error}
*
* @return `true` if it is neither `undefined` nor `null`,
* if `value` is `undefined` or `null`:
* Custom {@link Error} if `errorSupplier` is defined,
* {IllegalArgumentError} otherwise
*
* @throws if `value` is `undefined` or `null`: custom {@link Error} if `errorSupplier` is defined,
* {IllegalArgumentError} otherwise
*/
static notNullOrUndefined(value: NullableOrUndefined<any>,
errorSupplier?: Nullable<TFunction0<Error>>): boolean;
static notNullOrUndefined(value: NullableOrUndefined<any>,
errorSupplierOrMessage?: Nullable<TFunction0<Error> | string>): boolean {
if (ObjectUtil.nonNullOrUndefined(value)) {
return true;
}
if (Function0.isFunction(errorSupplierOrMessage) || isFFunction0(errorSupplierOrMessage)) {
throw Function0.of(errorSupplierOrMessage)
.apply();
}
throw new IllegalArgumentError(
errorSupplierOrMessage
? errorSupplierOrMessage
: 'value is null or undefined'
);
}
}