isNative.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var isFunction = require('./isFunction'),
  2. isObjectLike = require('../internal/isObjectLike');
  3. /** Used to detect host constructors (Safari > 5). */
  4. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  5. /** Used for native method references. */
  6. var objectProto = Object.prototype;
  7. /** Used to resolve the decompiled source of functions. */
  8. var fnToString = Function.prototype.toString;
  9. /** Used to check objects for own properties. */
  10. var hasOwnProperty = objectProto.hasOwnProperty;
  11. /** Used to detect if a method is native. */
  12. var reIsNative = RegExp('^' +
  13. fnToString.call(hasOwnProperty).replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
  14. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  15. );
  16. /**
  17. * Checks if `value` is a native function.
  18. *
  19. * @static
  20. * @memberOf _
  21. * @category Lang
  22. * @param {*} value The value to check.
  23. * @returns {boolean} Returns `true` if `value` is a native function, else `false`.
  24. * @example
  25. *
  26. * _.isNative(Array.prototype.push);
  27. * // => true
  28. *
  29. * _.isNative(_);
  30. * // => false
  31. */
  32. function isNative(value) {
  33. if (value == null) {
  34. return false;
  35. }
  36. if (isFunction(value)) {
  37. return reIsNative.test(fnToString.call(value));
  38. }
  39. return isObjectLike(value) && reIsHostCtor.test(value);
  40. }
  41. module.exports = isNative;