UNPKG

1.29 kBJavaScriptView Raw
1var util = require('../core').util;
2
3function typeOf(data) {
4 if (data === null && typeof data === 'object') {
5 return 'null';
6 } else if (data !== undefined && isBinary(data)) {
7 return 'Binary';
8 } else if (data !== undefined && data.constructor) {
9 return data.wrapperName || util.typeName(data.constructor);
10 } else if (data !== undefined && typeof data === 'object') {
11 // this object is the result of Object.create(null), hence the absence of a
12 // defined constructor
13 return 'Object';
14 } else {
15 return 'undefined';
16 }
17}
18
19function isBinary(data) {
20 var types = [
21 'Buffer', 'File', 'Blob', 'ArrayBuffer', 'DataView',
22 'Int8Array', 'Uint8Array', 'Uint8ClampedArray',
23 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array',
24 'Float32Array', 'Float64Array'
25 ];
26 if (util.isNode()) {
27 var Stream = util.stream.Stream;
28 if (util.Buffer.isBuffer(data) || data instanceof Stream) {
29 return true;
30 }
31 }
32
33 for (var i = 0; i < types.length; i++) {
34 if (data !== undefined && data.constructor) {
35 if (util.isType(data, types[i])) return true;
36 if (util.typeName(data.constructor) === types[i]) return true;
37 }
38 }
39
40 return false;
41}
42
43/**
44 * @api private
45 */
46module.exports = {
47 typeOf: typeOf,
48 isBinary: isBinary
49};