-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtype.js
53 lines (50 loc) · 1.13 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
/*
* Simple data function to store type information
* @param {string} type Usually what is returned from typeof
* @param {string} cls Sanitized @Class via Object.prototype.toString
* @param {string} sub If type and cls the same, and need to specify somehow
* @private
* @example
*
* //for null
* new Type('null');
*
* //for Date
* new Type('object', 'date');
*
* //for Uint8Array
*
* new Type('object', 'typed-array', 'uint8');
*/
export function Type(type, cls, sub) {
if (!type) {
throw new Error('Type class must be initialized at least with `type` information');
}
this.type = type;
this.cls = cls;
this.sub = sub;
}
Type.prototype = {
toString: function(sep) {
sep = sep || ';';
var str = [this.type];
if (this.cls) {
str.push(this.cls);
}
if (this.sub) {
str.push(this.sub);
}
return str.join(sep);
},
toTryTypes: function() {
var _types = [];
if (this.sub) {
_types.push(new Type(this.type, this.cls, this.sub));
}
if (this.cls) {
_types.push(new Type(this.type, this.cls));
}
_types.push(new Type(this.type));
return _types;
}
};