-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.mjs
More file actions
108 lines (99 loc) · 2.24 KB
/
Copy pathdebug.mjs
File metadata and controls
108 lines (99 loc) · 2.24 KB
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
/**
* Throws the parameter
* @function
* @param {any} value
* @throws {any}
*/
export function croak(value){
throw value
}
/**
* Check if value is an instance of a given type
* @function
* @param {any} value - value to check
* @param {...any} types - list of constructors, instances or string
* @throws {any} - croaks the value type if check fails
*/
export const good = type_check.bind(void 0, true);
/**
* Check if value is not an instance of a given type
* @function
* @param {any} value - value to check
* @param {...any} types - list of constructors, instances or string
* @throws {any} - croaks the value type if check fails
*/
export const crap = type_check.bind(void 0, false);
/**
* pause the execution and start debugging
* @function
*/
export function pause() {
debugger
}
/**
* Enable/disable debugging
* @function
* @param {any} value
*/
export function DEBUG(v) {
DEBUGGING = v
}
/**
* throws expr if is not strictly equal to val
* @function
* @param {any} expr
* @param {any} val
* @throws {any} - croacks the expr if check fails
*/
export function ASSERT(expr, val, msg) {
return val === expr || croak(msg || expr)
}
/**
* Assert expr is truthy
* @function
* @param {any} expr
* @throws {any} - croacks the boolean value of expr if check fails
*/
export function ASSERT_T(expr, msg) {
return ASSERT(!!expr, true, msg)
}
/**
* Assert expr is falsy
* @function
* @param {any} expr
* @throws {any} - croacks the boolean value of expr if check fails
*/
export function ASSERT_F(expr, msg) {
return ASSERT(!!expr, false, msg)
}
/**
* current debugging state
*/
export var DEBUGGING = false
/**
* Checks if the type the value is compatible
* with the types array of constructor types
* If the proper param is false then it will
* perform the opposite check
* @function
* @private
* @param {any} proper
* @param {any} value
* @param {...any} types
* @returns {boolean}
*/
function type_check(proper, value, ...types) {
return types.some((type) => {
switch(true) {
case typeof type == 'string':
return proper
? type == typeof value
: type != typeof value;
case (value instanceof type):
case value.constructor == type:
return proper
default:
return !proper
}
}) || croak(value.constructor)
}