-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass.js
200 lines (158 loc) · 4.41 KB
/
ass.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
// custom assertion module that can be switched off.
// does a lot of JS type checking (runtime)
// historically theArr.includes() wasn't available! maintained for back compatibitily.
const _arrContainsValue = (function(theArr, theValue)
{
return theArr.includes(theValue);
});
// Field = Prop - NB: will include any props from base objects.
const _objContainsField = (function(theObj, theValue)
{
var isContained = false;
for (var v in theObj) {
isContained = (v === theValue);
if (isContained) break;
}
return isContained;
});
const _objMustHaveFields = (function(theObj, theValues)
{
if (theValues.length === 0 || !theObj) return true;
var isContaining = true;
for (var v of theValues) {
if (!_objContainsField(theObj, v)) {
isContaining = false;
break;
}
}
return isContaining;
});
const _objCanOnlyHaveFields = (function(theObj, theValues)
{
var isOkay = true;
if (!theObj) return isOkay;
for (var v in theObj) {
isOkay = (_arrContainsValue(theValues, v ));
if (!isOkay) break;
}
return isOkay;
});
const _checkObject = (function(theObject, theMandatory, theOptional, theMsg)
{
var allFields = theMandatory.concat(theOptional);
ok(_objMustHaveFields(theObject, theMandatory), theMsg);
ok(_objCanOnlyHaveFields(theObject, allFields), theMsg);
});
//--- EXPORTS BELOW.
//---
let _assertionOn = true;
export function setAssertion(value)
{
_assertionOn = value;
}
const TRACE = true;
export function ok(theConstraint, theMessage)
{
if (!_assertionOn) return;
if (!theConstraint)
{
var aMessage = theMessage;
aMessage = "Assertion Error: " + aMessage + '\n\n' + new Date() + '\n';
if (TRACE) console.trace();
throw aMessage;
}
return;
}
export function okFunction(theFn)
{
if (!_assertionOn) return;
ok(typeof theFn === 'function', 'error - not a function: ' + theFn);
}
export function okArray(theArr, allowUndefined = false)
{
if (!_assertionOn) return;
if ( ! (allowUndefined && (theArr === undefined)) )
{
var isArray = (theArr && theArr.constructor === Array);
ok(isArray, 'Error - not an Array: ' + JSON.stringify(theArr));
}
}
export function okObject(theObj, allowNullOrUndefined)
{
if (!_assertionOn) return;
var isObject = false;
if ( allowNullOrUndefined && ((theObj === null) || (theObj === undefined)) )
isObject = true;
else
isObject = (typeof theObj === 'object');
ok(isObject, 'Error - not an Object: ' + JSON.stringify(theObj));
}
export function okString(theString)
{
if (!_assertionOn) return;
ok(typeof theString === 'string', 'error - not a string: ' + theString);
}
export function okNum(theNum)
{
if (!_assertionOn) return;
ok( theNum !== 'NaN' && typeof theNum === 'number' , 'error - not a number: ' + theNum);
}
export function okIfZero(theValue)
{
if (!_assertionOn || theValue || (theValue === 0)) return;
ok(false, "Value is not Zero and is Falsy");
}
export function notUndefined(theValue)
{
if ( !_assertionOn || (theValue !== undefined) ) return;
ok(false, "Value is Undefined - shound not be: " + theValue);
}
//---
let _typeArr = [];
export function regType(theTypeName, theMandatory, theOptional, allowDuplicateOverride)
{
ok(theTypeName);
if (!allowDuplicateOverride)
{
ok(!_typeArr[theTypeName], "Duplidate type: " + theTypeName);
}
var t = _typeArr[theTypeName] = {};
t.mandatory = theMandatory || [];
t.optional = theOptional || [];
return theTypeName;
}
export function okType(theTypeName, theObject, theMsg)
{
if (!_assertionOn) return;
ok( _typeArr[theTypeName], "No such type: " + theTypeName + " " + theMsg);
var m = _typeArr[theTypeName].mandatory;
var o = _typeArr[theTypeName].optional;
_checkObject(theObject, m, o, theMsg);
}
//---
let _enums = {};
export function regEnum(theEnumName, theEnumValues, allowDuplicateOverride)
{
if (!allowDuplicateOverride)
{
ok(!_enums[theEnumName], "Duplidate Enum: " + theEnumName);
}
_enums[theEnumName] = theEnumValues;
return theEnumName;
}
export function okEnum(theEnumName, theValue, theMsg)
{
if (!_assertionOn) return;
var allowed = _enums[theEnumName];
if (theValue !== undefined)
{
const isAllowedValue = allowed.includes(theValue);
ok(isAllowedValue, theMsg + "Enum value error (enum, value) " + theEnumName + " " + theValue);
}
}
export function okImplies(theCondition, theImpliedValue, theMsg) // theCondition -> theImpliedValue
{
if (!_assertionOn) return;
ok(!theCondition || theImpliedValue, "assert implication rule broken " + theMsg);
}