forked from LeaVerou/StronglyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
67 lines (54 loc) · 1.83 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>StronglyTyped: Unit tests (sort of...)</title>
</head>
<body>
<h1>StronglyTyped: Unit tests (sort of...)</h1>
<p>There's nothing to see here. Open the console, refresh and look there.</p>
<p>
<strong>StronglyTyped</strong> is a library written by <a href="http://leaverou.me">Lea Verou</a>
to allow strongly typed JavaScript properties and globals in ES5 supporting browsers.
<a href="http://leaverou.me/2011/05/strongly-typed-javascript/">Read More</a>
<script src="strongly-typed.js"></script>
<script>
var o = {};
console || (console = {});
['group', 'groupEnd', 'log'].forEach(function(method) {
console[method] || (console[method] = function(){});
});
var tests = {
'Array': [[], [1, 2, 3], new Array(5), null, 'foo', 5],
'Boolean': [false, new Boolean(true), null, 'foo'],
'Date': [new Date(), 5],
'Function': [function(){}, new Function("5+2"), null, 28],
'Integer': [0, -1, 1, 1.5, Infinity, NaN, 'foo'],
'Number': [0, -1, 1, Infinity, 1.5, NaN, 'foo'],
'String': ['bar', new String('Object string'), null, true]
}
for(var type in tests) {
console.group(type);
var typeTests = tests[type],
testProperty = 'test' + type;
StronglyTyped[type.toLowerCase()](o, testProperty, typeTests[0]);
typeTests.forEach(function(val, i) {
console.log('Attempting: o.' + testProperty + ' = ', val, '...');
try {
o[testProperty] = val;
console.log('o.' + testProperty + ' is now equal to ', o[testProperty]);
}
catch(e) {
console.error(e);
}
});
console.groupEnd();
}
console.group('constant');
StronglyTyped.constant(o, 'testConst', Math.PI);
console.log('o.testConst is ', o.testConst);
console.log('Attempting: o.testConst = ', 5, '...');
console.log('o.testConst is ', o.testConst);
console.groupEnd();
</script>
</body>
</html>