isNative.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import isFunction from './isFunction';
  2. import isHostObject from './internal/isHostObject';
  3. import isObjectLike from './isObjectLike';
  4. /** Used to match `RegExp` [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns). */
  5. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
  6. /** Used to detect host constructors (Safari > 5). */
  7. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  8. /** Used for built-in method references. */
  9. var objectProto = Object.prototype;
  10. /** Used to resolve the decompiled source of functions. */
  11. var funcToString = Function.prototype.toString;
  12. /** Used to check objects for own properties. */
  13. var hasOwnProperty = objectProto.hasOwnProperty;
  14. /** Used to detect if a method is native. */
  15. var reIsNative = RegExp('^' +
  16. funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  17. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  18. );
  19. /**
  20. * Checks if `value` is a native function.
  21. *
  22. * @static
  23. * @memberOf _
  24. * @category Lang
  25. * @param {*} value The value to check.
  26. * @returns {boolean} Returns `true` if `value` is a native function, else `false`.
  27. * @example
  28. *
  29. * _.isNative(Array.prototype.push);
  30. * // => true
  31. *
  32. * _.isNative(_);
  33. * // => false
  34. */
  35. function isNative(value) {
  36. if (value == null) {
  37. return false;
  38. }
  39. if (isFunction(value)) {
  40. return reIsNative.test(funcToString.call(value));
  41. }
  42. return isObjectLike(value) &&
  43. (isHostObject(value) ? reIsNative : reIsHostCtor).test(value);
  44. }
  45. export default isNative;