-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtypes.js
49 lines (44 loc) · 1.26 KB
/
types.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
var util = require('../core').util;
function typeOf(data) {
if (data === null && typeof data === 'object') {
return 'null';
} else if (data !== undefined && isBinary(data)) {
return 'Binary';
} else if (data !== undefined && data.constructor) {
return data.wrapperName || util.typeName(data.constructor);
} else if (data !== undefined && typeof data === 'object') {
// this object is the result of Object.create(null), hence the absence of a
// defined constructor
return 'Object';
} else {
return 'undefined';
}
}
function isBinary(data) {
var types = [
'Buffer', 'File', 'Blob', 'ArrayBuffer', 'DataView',
'Int8Array', 'Uint8Array', 'Uint8ClampedArray',
'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array',
'Float32Array', 'Float64Array'
];
if (util.isNode()) {
var Stream = util.stream.Stream;
if (util.Buffer.isBuffer(data) || data instanceof Stream) {
return true;
}
}
for (var i = 0; i < types.length; i++) {
if (data !== undefined && data.constructor) {
if (util.isType(data, types[i])) return true;
if (util.typeName(data.constructor) === types[i]) return true;
}
}
return false;
}
/**
* @api private
*/
module.exports = {
typeOf: typeOf,
isBinary: isBinary
};