-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
167 lines (146 loc) · 4.94 KB
/
index.js
File metadata and controls
167 lines (146 loc) · 4.94 KB
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
import types from './types';
import { Type } from './type';
var toString = Object.prototype.toString;
/**
* Function to store type checks
* @private
*/
function TypeChecker() {
this.checks = [];
}
TypeChecker.prototype = {
add: function(func) {
this.checks.push(func);
return this;
},
addBeforeFirstMatch: function(obj, func) {
var match = this.getFirstMatch(obj);
if (match) {
this.checks.splice(match.index, 0, func);
} else {
this.add(func);
}
},
addTypeOf: function(type, res) {
return this.add(function(obj, tpeOf) {
if (tpeOf === type) {
return new Type(res);
}
});
},
addClass: function(cls, res, sub) {
return this.add(function(obj, tpeOf, objCls) {
if (objCls === cls) {
return new Type(types.OBJECT, res, sub);
}
});
},
getFirstMatch: function(obj) {
var typeOf = typeof obj;
var cls = toString.call(obj);
for (var i = 0, l = this.checks.length; i < l; i++) {
var res = this.checks[i].call(this, obj, typeOf, cls);
if (typeof res !== 'undefined') {
return { result: res, func: this.checks[i], index: i };
}
}
},
getType: function(obj) {
var match = this.getFirstMatch(obj);
return match && match.result;
}
};
var main = new TypeChecker();
//TODO add iterators
main
.addTypeOf(types.NUMBER, types.NUMBER)
.addTypeOf(types.UNDEFINED, types.UNDEFINED)
.addTypeOf(types.STRING, types.STRING)
.addTypeOf(types.BOOLEAN, types.BOOLEAN)
.addTypeOf(types.FUNCTION, types.FUNCTION)
.addTypeOf(types.SYMBOL, types.SYMBOL)
.add(function(obj) {
if (obj === null) {
return new Type(types.NULL);
}
})
.addClass('[object String]', types.STRING)
.addClass('[object Boolean]', types.BOOLEAN)
.addClass('[object Number]', types.NUMBER)
.addClass('[object Array]', types.ARRAY)
.addClass('[object RegExp]', types.REGEXP)
.addClass('[object Error]', types.ERROR)
.addClass('[object Date]', types.DATE)
.addClass('[object Arguments]', types.ARGUMENTS)
.addClass('[object ArrayBuffer]', types.ARRAY_BUFFER)
.addClass('[object Int8Array]', types.TYPED_ARRAY, 'int8')
.addClass('[object Uint8Array]', types.TYPED_ARRAY, 'uint8')
.addClass('[object Uint8ClampedArray]', types.TYPED_ARRAY, 'uint8clamped')
.addClass('[object Int16Array]', types.TYPED_ARRAY, 'int16')
.addClass('[object Uint16Array]', types.TYPED_ARRAY, 'uint16')
.addClass('[object Int32Array]', types.TYPED_ARRAY, 'int32')
.addClass('[object Uint32Array]', types.TYPED_ARRAY, 'uint32')
.addClass('[object Float32Array]', types.TYPED_ARRAY, 'float32')
.addClass('[object Float64Array]', types.TYPED_ARRAY, 'float64')
.addClass('[object Bool16x8]', types.SIMD, 'bool16x8')
.addClass('[object Bool32x4]', types.SIMD, 'bool32x4')
.addClass('[object Bool8x16]', types.SIMD, 'bool8x16')
.addClass('[object Float32x4]', types.SIMD, 'float32x4')
.addClass('[object Int16x8]', types.SIMD, 'int16x8')
.addClass('[object Int32x4]', types.SIMD, 'int32x4')
.addClass('[object Int8x16]', types.SIMD, 'int8x16')
.addClass('[object Uint16x8]', types.SIMD, 'uint16x8')
.addClass('[object Uint32x4]', types.SIMD, 'uint32x4')
.addClass('[object Uint8x16]', types.SIMD, 'uint8x16')
.addClass('[object DataView]', types.DATA_VIEW)
.addClass('[object Map]', types.MAP)
.addClass('[object WeakMap]', types.WEAK_MAP)
.addClass('[object Set]', types.SET)
.addClass('[object WeakSet]', types.WEAK_SET)
.addClass('[object Promise]', types.PROMISE)
.addClass('[object Blob]', types.BLOB)
.addClass('[object File]', types.FILE)
.addClass('[object FileList]', types.FILE_LIST)
.addClass('[object XMLHttpRequest]', types.XHR)
.add(function(obj) {
if ((typeof Promise === types.FUNCTION && obj instanceof Promise) ||
(typeof obj.then === types.FUNCTION)) {
return new Type(types.OBJECT, types.PROMISE);
}
})
.add(function(obj) {
if (typeof Buffer !== 'undefined' && obj instanceof Buffer) {// eslint-disable-line no-undef
return new Type(types.OBJECT, types.BUFFER);
}
})
.add(function(obj) {
if (typeof Node !== 'undefined' && obj instanceof Node) {
return new Type(types.OBJECT, types.HTML_ELEMENT, obj.nodeName);
}
})
.add(function(obj) {
// probably at the begginging should be enough these checks
if (obj.Boolean === Boolean && obj.Number === Number && obj.String === String && obj.Date === Date) {
return new Type(types.OBJECT, types.HOST);
}
})
.add(function() {
return new Type(types.OBJECT);
});
/**
* Get type information of anything
*
* @param {any} obj Anything that could require type information
* @return {Type} type info
* @private
*/
function getGlobalType(obj) {
return main.getType(obj);
}
getGlobalType.checker = main;
getGlobalType.TypeChecker = TypeChecker;
getGlobalType.Type = Type;
Object.keys(types).forEach(function(typeName) {
getGlobalType[typeName] = types[typeName];
});
export default getGlobalType;