-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtts_assert.js
245 lines (243 loc) · 7.17 KB
/
rtts_assert.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
System.register("rtts_assert/rtts_assert", [], function($__export) {
"use strict";
var __moduleName = "rtts_assert/rtts_assert";
var POSITION_NAME,
primitives,
string,
boolean,
number,
currentStack;
function argPositionName(i) {
var position = (i / 2) + 1;
return POSITION_NAME[position] || (position + 'th');
}
function proxy() {}
function assertArgumentTypes() {
for (var params = [],
$__2 = 0; $__2 < arguments.length; $__2++)
params[$__2] = arguments[$__2];
var actual,
type;
var currentArgErrors;
var errors = [];
var msg;
for (var i = 0,
l = params.length; i < l; i = i + 2) {
actual = params[i];
type = params[i + 1];
currentArgErrors = [];
if (!isType(actual, type, currentArgErrors)) {
errors.push(argPositionName(i) + ' argument has to be an instance of ' + prettyPrint(type) + ', got ' + prettyPrint(actual));
if (currentArgErrors.length) {
errors.push(currentArgErrors);
}
}
}
if (errors.length) {
throw new Error('Invalid arguments given!\n' + formatErrors(errors));
}
}
function prettyPrint(value) {
if (typeof value === 'undefined') {
return 'undefined';
}
if (typeof value === 'string') {
return '"' + value + '"';
}
if (typeof value === 'boolean') {
return value.toString();
}
if (value === null) {
return 'null';
}
if (typeof value === 'object') {
if (value.__assertName) {
return value.__assertName;
}
if (value.map) {
return '[' + value.map(prettyPrint).join(', ') + ']';
}
var properties = Object.keys(value);
return '{' + properties.map((function(p) {
return p + ': ' + prettyPrint(value[p]);
})).join(', ') + '}';
}
return value.__assertName || value.name || value.toString();
}
function isType(value, T, errors) {
if (T === primitives.void) {
return typeof value === 'undefined';
}
if (_isProxy(value)) {
return true;
}
if (T === primitives.any || value === null) {
return true;
}
if (T === primitives.string) {
return typeof value === 'string';
}
if (T === primitives.number) {
return typeof value === 'number';
}
if (T === primitives.boolean) {
return typeof value === 'boolean';
}
if (typeof T.assert === 'function') {
var parentStack = currentStack;
var isValid;
currentStack = errors;
try {
isValid = T.assert(value);
} catch (e) {
fail(e.message);
isValid = false;
}
currentStack = parentStack;
if (typeof isValid === 'undefined') {
isValid = errors.length === 0;
}
return isValid;
}
return value instanceof T;
}
function _isProxy(obj) {
if (!obj || !obj.constructor || !obj.constructor.annotations)
return false;
return obj.constructor.annotations.filter((function(a) {
return a instanceof proxy;
})).length > 0;
}
function formatErrors(errors) {
var indent = arguments[1] !== (void 0) ? arguments[1] : ' ';
return errors.map((function(e) {
if (typeof e === 'string')
return indent + '- ' + e;
return formatErrors(e, indent + ' ');
})).join('\n');
}
function type(actual, T) {
var errors = [];
if (!isType(actual, T, errors)) {
var msg = 'Expected an instance of ' + prettyPrint(T) + ', got ' + prettyPrint(actual) + '!';
if (errors.length) {
msg += '\n' + formatErrors(errors);
}
throw new Error(msg);
}
return actual;
}
function returnType(actual, T) {
var errors = [];
if (!isType(actual, T, errors)) {
var msg = 'Expected to return an instance of ' + prettyPrint(T) + ', got ' + prettyPrint(actual) + '!';
if (errors.length) {
msg += '\n' + formatErrors(errors);
}
throw new Error(msg);
}
return actual;
}
function arrayOf() {
for (var types = [],
$__3 = 0; $__3 < arguments.length; $__3++)
types[$__3] = arguments[$__3];
return assert.define('array of ' + types.map(prettyPrint).join('/'), function(value) {
var $__5;
if (assert(value).is(Array)) {
for (var $__0 = value[$traceurRuntime.toProperty(Symbol.iterator)](),
$__1 = void 0; !($__1 = $__0.next()).done; ) {
var item = $__1.value;
{
($__5 = assert(item)).is.apply($__5, $traceurRuntime.spread(types));
}
}
}
});
}
function structure(definition) {
var properties = Object.keys(definition);
return assert.define('object with properties ' + properties.join(', '), function(value) {
if (assert(value).is(Object)) {
for (var $__0 = properties[$traceurRuntime.toProperty(Symbol.iterator)](),
$__1 = void 0; !($__1 = $__0.next()).done; ) {
var property = $__1.value;
{
assert(value[property]).is(definition[property]);
}
}
}
});
}
function fail(message) {
currentStack.push(message);
}
function define(classOrName, check) {
var cls = classOrName;
if (typeof classOrName === 'string') {
cls = function() {};
cls.__assertName = classOrName;
}
cls.assert = function(value) {
return check(value);
};
return cls;
}
function assert(value) {
return {is: function is() {
var $__5;
for (var types = [],
$__4 = 0; $__4 < arguments.length; $__4++)
types[$__4] = arguments[$__4];
var allErrors = [];
var errors;
for (var $__0 = types[$traceurRuntime.toProperty(Symbol.iterator)](),
$__1 = void 0; !($__1 = $__0.next()).done; ) {
var type = $__1.value;
{
errors = [];
if (isType(value, type, errors)) {
return true;
}
allErrors.push(prettyPrint(value) + ' is not instance of ' + prettyPrint(type));
if (errors.length) {
allErrors.push(errors);
}
}
}
($__5 = currentStack).push.apply($__5, $traceurRuntime.spread(allErrors));
return false;
}};
}
$__export("proxy", proxy);
return {
setters: [],
execute: function() {
POSITION_NAME = ['', '1st', '2nd', '3rd'];
primitives = $traceurRuntime.type;
string = define('string', function(value) {
return typeof value === 'string';
});
boolean = define('boolean', function(value) {
return typeof value === 'boolean';
});
number = define('number', function(value) {
return typeof value === 'number';
});
currentStack = [];
assert.type = type;
assert.argumentTypes = assertArgumentTypes;
assert.returnType = returnType;
assert.define = define;
assert.fail = fail;
assert.string = string;
assert.number = number;
assert.boolean = boolean;
assert.arrayOf = arrayOf;
assert.structure = structure;
$__export("assert", assert);
}
};
});
//# sourceMappingURL=/Users/deast/Angular/angular/modules/rtts_assert/src/rtts_assert.map
//# sourceMappingURL=./rtts_assert.map