index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * lodash (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright jQuery Foundation and other contributors <https://jquery.org/>
  5. * Released under MIT license <https://lodash.com/license>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. */
  9. /** `Object#toString` result references. */
  10. var objectTag = '[object Object]';
  11. /**
  12. * Checks if `value` is a host object in IE < 9.
  13. *
  14. * @private
  15. * @param {*} value The value to check.
  16. * @returns {boolean} Returns `true` if `value` is a host object, else `false`.
  17. */
  18. function isHostObject(value) {
  19. // Many host objects are `Object` objects that can coerce to strings
  20. // despite having improperly defined `toString` methods.
  21. var result = false;
  22. if (value != null && typeof value.toString != 'function') {
  23. try {
  24. result = !!(value + '');
  25. } catch (e) {}
  26. }
  27. return result;
  28. }
  29. /**
  30. * Creates a unary function that invokes `func` with its argument transformed.
  31. *
  32. * @private
  33. * @param {Function} func The function to wrap.
  34. * @param {Function} transform The argument transform.
  35. * @returns {Function} Returns the new function.
  36. */
  37. function overArg(func, transform) {
  38. return function(arg) {
  39. return func(transform(arg));
  40. };
  41. }
  42. /** Used for built-in method references. */
  43. var funcProto = Function.prototype,
  44. objectProto = Object.prototype;
  45. /** Used to resolve the decompiled source of functions. */
  46. var funcToString = funcProto.toString;
  47. /** Used to check objects for own properties. */
  48. var hasOwnProperty = objectProto.hasOwnProperty;
  49. /** Used to infer the `Object` constructor. */
  50. var objectCtorString = funcToString.call(Object);
  51. /**
  52. * Used to resolve the
  53. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  54. * of values.
  55. */
  56. var objectToString = objectProto.toString;
  57. /** Built-in value references. */
  58. var getPrototype = overArg(Object.getPrototypeOf, Object);
  59. /**
  60. * Checks if `value` is object-like. A value is object-like if it's not `null`
  61. * and has a `typeof` result of "object".
  62. *
  63. * @static
  64. * @memberOf _
  65. * @since 4.0.0
  66. * @category Lang
  67. * @param {*} value The value to check.
  68. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  69. * @example
  70. *
  71. * _.isObjectLike({});
  72. * // => true
  73. *
  74. * _.isObjectLike([1, 2, 3]);
  75. * // => true
  76. *
  77. * _.isObjectLike(_.noop);
  78. * // => false
  79. *
  80. * _.isObjectLike(null);
  81. * // => false
  82. */
  83. function isObjectLike(value) {
  84. return !!value && typeof value == 'object';
  85. }
  86. /**
  87. * Checks if `value` is a plain object, that is, an object created by the
  88. * `Object` constructor or one with a `[[Prototype]]` of `null`.
  89. *
  90. * @static
  91. * @memberOf _
  92. * @since 0.8.0
  93. * @category Lang
  94. * @param {*} value The value to check.
  95. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  96. * @example
  97. *
  98. * function Foo() {
  99. * this.a = 1;
  100. * }
  101. *
  102. * _.isPlainObject(new Foo);
  103. * // => false
  104. *
  105. * _.isPlainObject([1, 2, 3]);
  106. * // => false
  107. *
  108. * _.isPlainObject({ 'x': 0, 'y': 0 });
  109. * // => true
  110. *
  111. * _.isPlainObject(Object.create(null));
  112. * // => true
  113. */
  114. function isPlainObject(value) {
  115. if (!isObjectLike(value) ||
  116. objectToString.call(value) != objectTag || isHostObject(value)) {
  117. return false;
  118. }
  119. var proto = getPrototype(value);
  120. if (proto === null) {
  121. return true;
  122. }
  123. var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
  124. return (typeof Ctor == 'function' &&
  125. Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString);
  126. }
  127. module.exports = isPlainObject;