isPlainObject.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * lodash 4.0.1 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="es" include="clone,extend,isPlainObject,isObject,isArray,last,isEqual" --development --output src/lib/lodash`
  4. * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. import isHostObject from './internal/isHostObject';
  10. import isObjectLike from './isObjectLike';
  11. /** `Object#toString` result references. */
  12. var objectTag = '[object Object]';
  13. /** Used for built-in method references. */
  14. var objectProto = Object.prototype;
  15. /** Used to resolve the decompiled source of functions. */
  16. var funcToString = Function.prototype.toString;
  17. /** Used to infer the `Object` constructor. */
  18. var objectCtorString = funcToString.call(Object);
  19. /**
  20. * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  21. * of values.
  22. */
  23. var objectToString = objectProto.toString;
  24. /** Built-in value references. */
  25. var getPrototypeOf = Object.getPrototypeOf;
  26. /**
  27. * Checks if `value` is a plain object, that is, an object created by the
  28. * `Object` constructor or one with a `[[Prototype]]` of `null`.
  29. *
  30. * @static
  31. * @memberOf _
  32. * @category Lang
  33. * @param {*} value The value to check.
  34. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  35. * @example
  36. *
  37. * function Foo() {
  38. * this.a = 1;
  39. * }
  40. *
  41. * _.isPlainObject(new Foo);
  42. * // => false
  43. *
  44. * _.isPlainObject([1, 2, 3]);
  45. * // => false
  46. *
  47. * _.isPlainObject({ 'x': 0, 'y': 0 });
  48. * // => true
  49. *
  50. * _.isPlainObject(Object.create(null));
  51. * // => true
  52. */
  53. function isPlainObject(value) {
  54. if (!isObjectLike(value) || objectToString.call(value) != objectTag || isHostObject(value)) {
  55. return false;
  56. }
  57. var proto = objectProto;
  58. if (typeof value.constructor == 'function') {
  59. proto = getPrototypeOf(value);
  60. }
  61. if (proto === null) {
  62. return true;
  63. }
  64. var Ctor = proto.constructor;
  65. return (typeof Ctor == 'function' &&
  66. Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString);
  67. }
  68. export default isPlainObject;