-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
141 lines (128 loc) · 7.94 KB
/
Copy pathtypes.ts
File metadata and controls
141 lines (128 loc) · 7.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
'use strict';
type TypeCheckFunction = (arg: any) => boolean;
interface TypeCheckers {
string: TypeCheckFunction;
number: TypeCheckFunction;
boolean: TypeCheckFunction;
array: TypeCheckFunction;
intArray: TypeCheckFunction;
floatArray: TypeCheckFunction;
object: TypeCheckFunction;
someObject: TypeCheckFunction;
function: TypeCheckFunction;
symbol: TypeCheckFunction;
date: TypeCheckFunction;
regexp: TypeCheckFunction;
error: TypeCheckFunction;
undefined: TypeCheckFunction;
buffer: TypeCheckFunction;
null: TypeCheckFunction;
arrayBuffer: TypeCheckFunction;
map: TypeCheckFunction;
weakMap: TypeCheckFunction;
set: TypeCheckFunction;
weakSet: TypeCheckFunction;
dataView: TypeCheckFunction;
float32Array: TypeCheckFunction;
float64Array: TypeCheckFunction;
int8Array: TypeCheckFunction;
int16Array: TypeCheckFunction;
int32Array: TypeCheckFunction;
uInt8Array: TypeCheckFunction;
uInt16Array: TypeCheckFunction;
uInt32Array: TypeCheckFunction;
uInt8ClampedArray: TypeCheckFunction;
generator: TypeCheckFunction;
promise: TypeCheckFunction;
scalar: TypeCheckFunction;
}
const types: TypeCheckers = {
string: function(arg: any){ return typeof arg === 'string' || Object.prototype.toString.call(arg) === '[object String]'; }
, number: function(arg: any){ return typeof arg === 'number' || Object.prototype.toString.call(arg) === '[object Number]'; }
, boolean: function(arg: any){ return typeof arg === 'boolean' || Object.prototype.toString.call(arg) === '[object Boolean]'; }
, array: function(arg: any){ return Array.isArray(arg); }
, intArray: function(arg: any){ return types.int8Array(arg) || types.int16Array(arg) || types.int32Array(arg) || types.uInt8Array(arg) || types.uInt16Array(arg) || types.uInt32Array(arg) || types.uInt8ClampedArray(arg); }
, floatArray: function(arg: any){ return Array.isArray(arg) || types.float32Array(arg); }
, object: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Object]' && !types.promise(arg);}
, someObject: function(arg: any){ return /\[object [^\]]+\]/i.test(Object.prototype.toString.call(arg));}
, function: function(arg: any){ return typeof arg === 'function'; }
, symbol: function(arg: any){ return typeof arg === 'symbol'; }
, date: function(arg: any){ return arg instanceof Date && !isNaN(arg.valueOf()); }
, regexp: function(arg: any){ return arg instanceof RegExp; }
, error: function(arg: any){ return arg instanceof Error; }
, undefined: function(arg: any){ return typeof arg === 'undefined'; }
, buffer: function(arg: any){ return typeof global === 'object' && typeof global.Buffer === 'function' && global.Buffer.isBuffer(arg); }
, null: function(arg: any){ return null === arg || Object.prototype.toString.call(arg) === '[object Null]'; }
, arrayBuffer: function(arg: any){ return Object.prototype.toString.call(arg) === '[object ArrayBuffer]'; }
, map: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Map]'; }
, weakMap: function(arg: any){ return Object.prototype.toString.call(arg) === '[object WeakMap]'; }
, set: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Set]'; }
, weakSet: function(arg: any){ return Object.prototype.toString.call(arg) === '[object WeakSet]'; }
, dataView: function(arg: any){ return Object.prototype.toString.call(arg) === '[object DataView]'; }
, float32Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Float32Array]'; }
, float64Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Float64Array]'; }
, int8Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Int8Array]'; }
, int16Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Int16Array]'; }
, int32Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Int32Array]'; }
, uInt8Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Uint8Array]'; }
, uInt16Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Uint16Array]'; }
, uInt32Array: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Uint32Array]'; }
, uInt8ClampedArray: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Uint8ClampedArray]'; }
, generator: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Generator]'; }
, promise: function(arg: any){ return Object.prototype.toString.call(arg) === '[object Promise]' || (Object.prototype.toString.call(arg) === '[object Object]' && types.function(arg.then) && types.function(arg.catch)); }
, scalar: function(arg: any){ return this.string(arg) || this.number(arg) || this.boolean(arg) || this.symbol(arg) || this.date(arg); }
};
// Type for the return value of the main type function
type TypeName = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'null' | 'undefined' |
'function' | 'date' | 'regexp' | 'error' | 'buffer' | 'symbol' | 'map' |
'weakMap' | 'set' | 'weakSet' | 'promise' | 'dataView' | 'float32Array' |
'float64Array' | 'int8Array' | 'int16Array' | 'int32Array' | 'generator' |
'uInt8Array' | 'uInt16Array' | 'uInt32Array' | 'intArray' | 'floatArray' |
'uInt8ClampedArray' | 'arrayBuffer' | 'unknown';
// Define the main type function interface
interface TypeFunction extends TypeCheckers {
(item: any): TypeName;
}
// this is the exported method, it returns
// the type as string
const type: any = function(item: any): TypeName {
return types.string(item) ? 'string' :
types.number(item) ? 'number' :
types.boolean(item) ? 'boolean' :
types.object(item) ? 'object' :
types.array(item) ? 'array' :
types.null(item) ? 'null' :
types.undefined(item) ? 'undefined' :
types.function(item) ? 'function' :
types.date(item) ? 'date' :
types.regexp(item) ? 'regexp' :
types.error(item) ? 'error' :
types.buffer(item) ? 'buffer' :
types.symbol(item) ? 'symbol' :
types.map(item) ? 'map' :
types.weakMap(item) ? 'weakMap' :
types.set(item) ? 'set' :
types.weakSet(item) ? 'weakSet' :
types.promise(item) ? 'promise' :
types.dataView(item) ? 'dataView' :
types.float32Array(item) ? 'float32Array' :
types.float64Array(item) ? 'float64Array' :
types.int8Array(item) ? 'int8Array' :
types.int16Array(item) ? 'int16Array' :
types.int32Array(item) ? 'int32Array' :
types.generator(item) ? 'generator' :
types.uInt8Array(item) ? 'uInt8Array' :
types.uInt16Array(item) ? 'uInt16Array' :
types.uInt32Array(item) ? 'uInt32Array' :
types.intArray(item) ? 'intArray' :
types.floatArray(item) ? 'floatArray' :
types.uInt8ClampedArray(item) ? 'uInt8ClampedArray' :
types.arrayBuffer(item) ? 'arrayBuffer' :
types.someObject(item) ? 'object' : 'unknown';
};
// apply the class methods to the type function
(Object.keys(types) as Array<keyof TypeCheckers>).forEach((key) => {
if (!type[key]) type[key] = types[key].bind(types);
else throw new Error('Failed to map property «'+key+'» of types to type :(');
});
export default type as TypeFunction;