-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (52 loc) · 1.67 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
/*
* Checks if the given value is a native function.
*
* ref:
* - https://davidwalsh.name/detect-native-function
* - https://github.com/lodash/lodash/blob/master/lodash.js#L11115
*/
var isNil = require('is-nil');
var toSource = require('to-source-code');
function isObject(value) {
if (!value) {
return false;
}
var type = typeof value;
return type === 'object' || type === 'function';
}
// Checks if `value` is a host object in IE < 9.
function isHostObject(value) {
// Many host objects are `Object` objects that can coerce to strings
// despite having improperly defined `toString` methods.
var result = false;
if (!isNil(value) && typeof value.toString !== 'function') {
try {
result = ('' + value) !== '';
} catch (e) {}
}
return result;
}
function isFunction(value) {
var tag = isObject(value) ? Object.prototype.toString.call(value) : '';
return tag === '[object Function]' || tag === '[object GeneratorFunction]';
}
module.exports = function (value) {
if (!isObject(value)) {
return false;
}
if (isFunction(value) || isHostObject(value)) {
var fnToString = Function.prototype.toString;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var reIsNative = new RegExp('^' +
fnToString
.call(hasOwnProperty)
.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
.replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
);
return reIsNative.test(toSource(value));
}
// used to detect host constructors (Safari > 4; really typed array specific)
var reIsHostCtor = /^\[object .+?Constructor\]$/;
return reIsHostCtor.test(toSource(value));
};