-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype.js
93 lines (75 loc) · 2.39 KB
/
type.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
function Type(name, members) {
this.name = name;
this.members = members || [];
}
/**
* @param {String} memberName
* @return {boolean}
*/
Type.prototype.hasMember = function(memberName) {
return this.members.findIndex(function(name) {
return name === memberName;
}) >= 0;
};
/**
* @param {Type} otherType - Potential subtype
* @return {boolean}
*/
Type.prototype.satisfies = function(otherType) {
return this === otherType;
};
function AnyType() {
Type.call(this, '__Any', []);
}
AnyType.prototype = Object.create(Type.prototype);
/**
* Any member is possible
* @return {boolean}
*/
AnyType.prototype.hasMember = function() {
return true;
};
/**
* All types are subtypes of Any
* @param {Type} otherType
* @return {boolean}
*/
AnyType.prototype.satisfies = function(otherType) {
return true;
};
/** Built-in boolean type */
Type.BooleanType = new Type('boolean',
Object.getOwnPropertyNames(Boolean.prototype));
/** Built-in label type which really should never exist */
Type.LabelType = new Type('label', []);
/** Built-in number type */
Type.NumberType = new Type('number',
Object.getOwnPropertyNames(Number.prototype));
/** Built-in String type */
Type.StringType = new Type('String',
Object.getOwnPropertyNames(String.prototype)
.concat(Object.getOwnPropertyNames('')));
/** Built-in Array type */
Type.ArrayType = new Type('Array', Object.getOwnPropertyNames(Array.prototype)
.concat(Object.getOwnPropertyNames([])));
/** Built-in Object type */
Type.ObjectType = new Type('Object',
Object.getOwnPropertyNames(Object.prototype));
/** Built-in Date type */
Type.DateType = new Type('Date', Object.getOwnPropertyNames(Date.prototype)
.concat(Object.getOwnPropertyNames(new Date())));
/** Built-in Error type */
Type.ErrorType = new Type('Error', Object.getOwnPropertyNames(Error.prototype)
.concat(Object.getOwnPropertyNames(new Error('a'))));
/** Built-in RegExp type */
Type.RegExpType = new Type('RegExp',
Object.getOwnPropertyNames(RegExp.prototype)
.concat(Object.getOwnPropertyNames(/a/)));
/** Built-in Function type */
Type.FunctionType = new Type('Function',
Object.getOwnPropertyNames(Function.prototype)
.concat(Object.getOwnPropertyNames(function() {return 12;})));
/** Additionally export type hole */
Type.AnyType = new AnyType();
/** Export Type */
module.exports = Type;