-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (36 loc) · 1.65 KB
/
index.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
'use strict';
// Public API
module.exports = preciseTypeOf;
/**
* Detects real type of the objects like 'Array()', `new Number(1)`, `new Boolean(true)`, etc
*
* @param {mixed} obj - object to get type of
* @param {object} [options] - object to get type of
* @returns {string} precise type
*/
function preciseTypeOf(obj, options)
{
var type, stamp = Object.prototype.toString.call(obj);
options = options || {};
if (!type && obj === undefined) type = 'undefined';
if (!type && obj === null) type = 'null';
if (!type && obj.constructor && typeof obj.constructor.isBuffer == 'function' && obj.constructor.isBuffer(obj)) type = 'buffer';
if (!type && typeof window == 'object' && obj === window) type = 'global';
if (!type && typeof global == 'object' && obj === global) type = 'global';
if (!type && typeof obj == 'number' && isNaN(obj)) type = 'nan';
if (!type && typeof obj == 'object' && stamp == '[object Number]' && isNaN(obj)) type = 'nan';
if (!type && typeof obj == 'object' && stamp.substr(-6) == 'Event]') type = 'event';
if (!type && stamp.substr(0, 12) == '[object HTML') type = 'html';
if (!type && stamp.substr(0, 12) == '[object Node') type = 'html';
// last resort
if (!type) type = stamp.match(/\[object\s*([^\]]+)\]/)[1].toLowerCase();
// be even more precise by reporting "instance of" names
// Note: only check objects that were created by constructors
if (type == 'object' && options.pojoOnly && obj.constructor) {
// some constructors don't have names
type = obj.constructor.name || 'unknown';
// preserve `object` response for POJOs
if (type == 'Object') type = 'object';
}
return type;
}