8
0

isPlainObject.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * lodash 3.10.1 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize modern exports="es" include="clone,extend,isPlainObject,isObject,isArray,last,isEqual" --development --output src/lib/lodash`
  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. import baseForIn from '../internal/baseForIn';
  10. import isArguments from './isArguments';
  11. import isObjectLike from '../internal/isObjectLike';
  12. /** `Object#toString` result references. */
  13. var objectTag = '[object Object]';
  14. /** Used for native method references. */
  15. var objectProto = Object.prototype;
  16. /** Used to check objects for own properties. */
  17. var hasOwnProperty = objectProto.hasOwnProperty;
  18. /**
  19. * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  20. * of values.
  21. */
  22. var objToString = objectProto.toString;
  23. /**
  24. * Checks if `value` is a plain object, that is, an object created by the
  25. * `Object` constructor or one with a `[[Prototype]]` of `null`.
  26. *
  27. * **Note:** This method assumes objects created by the `Object` constructor
  28. * have no inherited enumerable properties.
  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. var Ctor;
  55. // Exit early for non `Object` objects.
  56. if (!(isObjectLike(value) && objToString.call(value) == objectTag && !isArguments(value)) ||
  57. (!hasOwnProperty.call(value, 'constructor') && (Ctor = value.constructor, typeof Ctor == 'function' && !(Ctor instanceof Ctor)))) {
  58. return false;
  59. }
  60. // IE < 9 iterates inherited properties before own properties. If the first
  61. // iterated property is an object's own property then there are no inherited
  62. // enumerable properties.
  63. var result;
  64. // In most environments an object's own properties are iterated before
  65. // its inherited properties. If the last iterated property is an object's
  66. // own property then there are no inherited enumerable properties.
  67. baseForIn(value, function(subValue, key) {
  68. result = key;
  69. });
  70. return result === undefined || hasOwnProperty.call(value, result);
  71. }
  72. export default isPlainObject;