UNPKG

3.87 kBJavaScriptView Raw
1/**
2 * lodash 3.9.1 (Custom Build) <https://lodash.com/>
3 * Build: `lodash modern modularize exports="npm" -o ./`
4 * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
5 * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
6 * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7 * Available under MIT license <https://lodash.com/license>
8 */
9
10/** `Object#toString` result references. */
11var funcTag = '[object Function]';
12
13/** Used to detect host constructors (Safari > 5). */
14var reIsHostCtor = /^\[object .+?Constructor\]$/;
15
16/**
17 * Checks if `value` is object-like.
18 *
19 * @private
20 * @param {*} value The value to check.
21 * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
22 */
23function isObjectLike(value) {
24 return !!value && typeof value == 'object';
25}
26
27/** Used for native method references. */
28var objectProto = Object.prototype;
29
30/** Used to resolve the decompiled source of functions. */
31var fnToString = Function.prototype.toString;
32
33/** Used to check objects for own properties. */
34var hasOwnProperty = objectProto.hasOwnProperty;
35
36/**
37 * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
38 * of values.
39 */
40var objToString = objectProto.toString;
41
42/** Used to detect if a method is native. */
43var reIsNative = RegExp('^' +
44 fnToString.call(hasOwnProperty).replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
45 .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
46);
47
48/**
49 * Gets the native function at `key` of `object`.
50 *
51 * @private
52 * @param {Object} object The object to query.
53 * @param {string} key The key of the method to get.
54 * @returns {*} Returns the function if it's native, else `undefined`.
55 */
56function getNative(object, key) {
57 var value = object == null ? undefined : object[key];
58 return isNative(value) ? value : undefined;
59}
60
61/**
62 * Checks if `value` is classified as a `Function` object.
63 *
64 * @static
65 * @memberOf _
66 * @category Lang
67 * @param {*} value The value to check.
68 * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
69 * @example
70 *
71 * _.isFunction(_);
72 * // => true
73 *
74 * _.isFunction(/abc/);
75 * // => false
76 */
77function isFunction(value) {
78 // The use of `Object#toString` avoids issues with the `typeof` operator
79 // in older versions of Chrome and Safari which return 'function' for regexes
80 // and Safari 8 equivalents which return 'object' for typed array constructors.
81 return isObject(value) && objToString.call(value) == funcTag;
82}
83
84/**
85 * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
86 * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
87 *
88 * @static
89 * @memberOf _
90 * @category Lang
91 * @param {*} value The value to check.
92 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
93 * @example
94 *
95 * _.isObject({});
96 * // => true
97 *
98 * _.isObject([1, 2, 3]);
99 * // => true
100 *
101 * _.isObject(1);
102 * // => false
103 */
104function isObject(value) {
105 // Avoid a V8 JIT bug in Chrome 19-20.
106 // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
107 var type = typeof value;
108 return !!value && (type == 'object' || type == 'function');
109}
110
111/**
112 * Checks if `value` is a native function.
113 *
114 * @static
115 * @memberOf _
116 * @category Lang
117 * @param {*} value The value to check.
118 * @returns {boolean} Returns `true` if `value` is a native function, else `false`.
119 * @example
120 *
121 * _.isNative(Array.prototype.push);
122 * // => true
123 *
124 * _.isNative(_);
125 * // => false
126 */
127function isNative(value) {
128 if (value == null) {
129 return false;
130 }
131 if (isFunction(value)) {
132 return reIsNative.test(fnToString.call(value));
133 }
134 return isObjectLike(value) && reIsHostCtor.test(value);
135}
136
137module.exports = getNative;