-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runtime.ts
94 lines (72 loc) · 2.43 KB
/
Runtime.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
import { NullPointerException, WrongParameterException, WrongTypeException } from "../errors";
import * as Checkers from "../validators/misc-validators";
let useRuntimeCheckers = true;
export class Runtime {
public static useRuntimeExceptions(value: boolean): void {
useRuntimeCheckers = value;
}
public static notNull<T>(obj: T): T {
if (useRuntimeCheckers && obj === null) {
throw new NullPointerException();
}
return obj;
}
public static exists<T>(obj: T): T {
if (useRuntimeCheckers && (typeof obj !== "boolean" && !obj)) {
throw new Error("Variable ");
}
return obj;
}
public static isArray<T>(obj: T[]): T[] {
if (useRuntimeCheckers && !Checkers.isArray(obj)) {
throw new WrongTypeException("Array");
}
return obj;
}
public static isString(obj: string): string {
if (useRuntimeCheckers && !Checkers.isString(obj)) {
throw new WrongTypeException("string");
}
return obj;
}
public static isNumber(obj: number): number {
if (useRuntimeCheckers && !Checkers.isNumber(obj)) {
throw new WrongTypeException("number");
}
return obj;
}
public static isFunction<T>(obj: T): T {
if (useRuntimeCheckers && !Checkers.isFunction(obj)) {
throw new WrongTypeException("function");
}
return obj;
}
// tslint:disable
// eslint-disable-next-line @typescript-eslint/ban-types
public static checkFunction(func: Function, args: any[] = [], thisArg = this): boolean {
try {
func.apply(thisArg, args);
return true;
} catch (e) {
return false;
}
}
public static isBoolean(obj: boolean): boolean {
if (useRuntimeCheckers && !Checkers.isBoolean(obj)) {
throw new WrongTypeException("boolean");
}
return obj;
}
public static min(obj: number, value: number): number {
if (useRuntimeCheckers && obj <= value) {
throw new WrongParameterException(`Number ${obj} must be greater than ${value}`);
}
return obj;
}
public static max(obj: number, value: number): number {
if (useRuntimeCheckers && obj >= value) {
throw new WrongParameterException(`Number ${obj} must be lower than ${value}`);
}
return obj;
}
}