Skip to content

Commit 9e8108e

Browse files
committed
refactor(rtts_assert): Ts'ify rtts_assert
Translate rtts_assert to TypeScript.
1 parent d53c898 commit 9e8108e

File tree

5 files changed

+440
-492
lines changed

5 files changed

+440
-492
lines changed

modules/rtts_assert/rtts_assert.es6

Lines changed: 0 additions & 1 deletion
This file was deleted.

modules/rtts_assert/rtts_assert.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src/rtts_assert';

modules/rtts_assert/src/rtts_assert.es6 renamed to modules/rtts_assert/src/rtts_assert.ts

Lines changed: 71 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,25 @@ function argPositionName(i) {
1616
var primitives;
1717
var genericType;
1818

19-
if (typeof $traceurRuntime === 'object') {
20-
primitives = $traceurRuntime.type;
21-
genericType = $traceurRuntime.genericType;
19+
if (typeof _global['$traceurRuntime'] === 'object') {
20+
primitives = _global['$traceurRuntime'].type;
21+
genericType = _global['$traceurRuntime'].genericType;
2222
} else {
2323
// Allow to work without traceur runtime as well!
2424
primitives = {
2525
any: {name: 'any'},
2626
boolean: {name: 'boolean'},
2727
number: {name: 'number'},
2828
string: {name: 'string'},
29-
symbol: {name: 'symbol'},
30-
void: {name: 'void'}
29+
symbol: {name: 'symbol'}, void: {name: 'void'}
3130
};
3231
genericType = function(type, args) {
33-
return {
34-
type: type,
35-
args: args
36-
}
32+
return { type: type, args: args }
3733
}
3834
}
39-
Object.keys(primitives).forEach(function(name) {
40-
primitives[name].__assertName = name;
41-
});
35+
Object.keys(primitives).forEach(function(name) { primitives[name].__assertName = name; });
4236

43-
export function proxy(){
37+
export function proxy() {
4438
}
4539

4640
function assertArgumentTypes(...params) {
@@ -59,10 +53,10 @@ function assertArgumentTypes(...params) {
5953
//
6054

6155
if (!isType(actual, type, currentArgErrors)) {
62-
6356
// console.log(JSON.stringify(errors, null, ' '));
6457
// TODO(vojta): print "an instance of" only if T starts with uppercase.
65-
errors.push(argPositionName(i) + ' argument has to be an instance of ' + prettyPrint(type) + ', got ' + prettyPrint(actual));
58+
errors.push(argPositionName(i) + ' argument has to be an instance of ' + prettyPrint(type) +
59+
', got ' + prettyPrint(actual));
6660
if (currentArgErrors.length) {
6761
errors.push(currentArgErrors);
6862
}
@@ -74,7 +68,7 @@ function assertArgumentTypes(...params) {
7468
}
7569
}
7670

77-
function prettyPrint(value, depth) {
71+
function prettyPrint(value, depth?) {
7872
if (typeof(depth) === 'undefined') {
7973
depth = 0;
8074
}
@@ -159,7 +153,7 @@ function isType(value, T, errors) {
159153
var isValid;
160154
currentStack = errors;
161155
try {
162-
isValid = T.assert(value) ;
156+
isValid = T.assert(value);
163157
} catch (e) {
164158
fail(e.message);
165159
isValid = false;
@@ -201,38 +195,42 @@ function _isProxy(obj) {
201195

202196
function formatErrors(errors, indent = ' ') {
203197
return errors.map((e) => {
204-
if (typeof e === 'string') return indent + '- ' + e;
205-
return formatErrors(e, indent + ' ');
206-
}).join('\n');
198+
if (typeof e === 'string') return indent + '- ' + e;
199+
return formatErrors(e, indent + ' ');
200+
})
201+
.join('\n');
207202
}
208203

209204

210205
// assert a type of given value and throw if does not pass
211-
function type(actual, T) {
212-
var errors = [];
213-
// currentStack = [];
206+
var type: any =
207+
function(actual, T) {
208+
var errors = [];
209+
// currentStack = [];
210+
211+
if (!isType(actual, T, errors)) {
212+
// console.log(JSON.stringify(errors, null, ' '));
213+
// TODO(vojta): print "an instance of" only if T starts with uppercase.
214+
var msg =
215+
'Expected an instance of ' + prettyPrint(T) + ', got ' + prettyPrint(actual) + '!';
216+
if (errors.length) {
217+
msg += '\n' + formatErrors(errors);
218+
}
214219

215-
if (!isType(actual, T, errors)) {
216-
// console.log(JSON.stringify(errors, null, ' '));
217-
// TODO(vojta): print "an instance of" only if T starts with uppercase.
218-
var msg = 'Expected an instance of ' + prettyPrint(T) + ', got ' + prettyPrint(actual) + '!';
219-
if (errors.length) {
220-
msg += '\n' + formatErrors(errors);
220+
throw new Error(msg);
221+
}
222+
return actual;
221223
}
222224

223-
throw new Error(msg);
224-
}
225-
return actual;
226-
}
227-
228225
function returnType(actual, T) {
229226
var errors = [];
230227
// currentStack = [];
231228

232229
if (!isType(actual, T, errors)) {
233230
// console.log(JSON.stringify(errors, null, ' '));
234231
// TODO(vojta): print "an instance of" only if T starts with uppercase.
235-
var msg = 'Expected to return an instance of ' + prettyPrint(T) + ', got ' + prettyPrint(actual) + '!';
232+
var msg = 'Expected to return an instance of ' + prettyPrint(T) + ', got ' +
233+
prettyPrint(actual) + '!';
236234
if (errors.length) {
237235
msg += '\n' + formatErrors(errors);
238236
}
@@ -244,17 +242,12 @@ function returnType(actual, T) {
244242
}
245243

246244
// TODO(vojta): define these with DSL?
247-
var string = type.string = define('string', function(value) {
248-
return typeof value === 'string';
249-
});
245+
var string = type.string = define('string', function(value) { return typeof value === 'string'; });
250246

251-
var boolean = type.boolean = define('boolean', function(value) {
252-
return typeof value === 'boolean';
253-
});
247+
var boolean = type.boolean =
248+
define('boolean', function(value) { return typeof value === 'boolean'; });
254249

255-
var number = type.number = define('number', function(value) {
256-
return typeof value === 'number';
257-
});
250+
var number = type.number = define('number', function(value) { return typeof value === 'number'; });
258251

259252
function arrayOf(...types) {
260253
return assert.define('array of ' + types.map(prettyPrint).join('/'), function(value) {
@@ -311,46 +304,46 @@ function define(classOrName, check) {
311304
return cls;
312305
}
313306

314-
315-
316-
function assert(value) {
317-
return {
318-
is: function is(...types) {
319-
// var errors = []
320-
var allErrors = [];
321-
var errors;
322-
for (var i = 0; i < types.length; i++) {
323-
var type = types[i];
324-
errors = [];
325-
326-
if (isType(value, type, errors)) {
327-
return true;
307+
var assert: any =
308+
function(value) {
309+
return {
310+
is: function is(...types) {
311+
// var errors = []
312+
var allErrors = [];
313+
var errors;
314+
for (var i = 0; i < types.length; i++) {
315+
var type = types[i];
316+
errors = [];
317+
318+
if (isType(value, type, errors)) {
319+
return true;
320+
}
321+
322+
// if no errors, merge multiple "is not instance of " into x/y/z ?
323+
allErrors.push(prettyPrint(value) + ' is not instance of ' + prettyPrint(type));
324+
if (errors.length) {
325+
allErrors.push(errors);
326+
}
327+
}
328+
329+
// if (types.length > 1) {
330+
// currentStack.push(['has to be ' + types.map(prettyPrint).join(' or '),
331+
// ...allErrors]);
332+
// } else {
333+
currentStack.push(...allErrors);
334+
// }
335+
return false;
328336
}
329-
330-
// if no errors, merge multiple "is not instance of " into x/y/z ?
331-
allErrors.push(prettyPrint(value) + ' is not instance of ' + prettyPrint(type))
332-
if (errors.length) {
333-
allErrors.push(errors);
334-
}
335-
}
336-
337-
// if (types.length > 1) {
338-
// currentStack.push(['has to be ' + types.map(prettyPrint).join(' or '), ...allErrors]);
339-
// } else {
340-
currentStack.push(...allErrors);
341-
// }
342-
return false;
337+
};
343338
}
344-
};
345-
}
346339

347340

348-
// PUBLIC API
341+
// PUBLIC API
349342

350-
// asserting API
343+
// asserting API
351344

352-
// throw if no type provided
353-
assert.type = type;
345+
// throw if no type provided
346+
assert.type = type;
354347
for (var prop in primitives) {
355348
assert.type[prop] = primitives[prop];
356349
}

0 commit comments

Comments
 (0)