isEmpty.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var baseKeys = require('./_baseKeys'),
  2. getTag = require('./_getTag'),
  3. isArguments = require('./isArguments'),
  4. isArray = require('./isArray'),
  5. isArrayLike = require('./isArrayLike'),
  6. isBuffer = require('./isBuffer'),
  7. isPrototype = require('./_isPrototype'),
  8. isTypedArray = require('./isTypedArray');
  9. /** `Object#toString` result references. */
  10. var mapTag = '[object Map]',
  11. setTag = '[object Set]';
  12. /** Used for built-in method references. */
  13. var objectProto = Object.prototype;
  14. /** Used to check objects for own properties. */
  15. var hasOwnProperty = objectProto.hasOwnProperty;
  16. /**
  17. * Checks if `value` is an empty object, collection, map, or set.
  18. *
  19. * Objects are considered empty if they have no own enumerable string keyed
  20. * properties.
  21. *
  22. * Array-like values such as `arguments` objects, arrays, buffers, strings, or
  23. * jQuery-like collections are considered empty if they have a `length` of `0`.
  24. * Similarly, maps and sets are considered empty if they have a `size` of `0`.
  25. *
  26. * @static
  27. * @memberOf _
  28. * @since 0.1.0
  29. * @category Lang
  30. * @param {*} value The value to check.
  31. * @returns {boolean} Returns `true` if `value` is empty, else `false`.
  32. * @example
  33. *
  34. * _.isEmpty(null);
  35. * // => true
  36. *
  37. * _.isEmpty(true);
  38. * // => true
  39. *
  40. * _.isEmpty(1);
  41. * // => true
  42. *
  43. * _.isEmpty([1, 2, 3]);
  44. * // => false
  45. *
  46. * _.isEmpty({ 'a': 1 });
  47. * // => false
  48. */
  49. function isEmpty(value) {
  50. if (value == null) {
  51. return true;
  52. }
  53. if (isArrayLike(value) &&
  54. (isArray(value) || typeof value == 'string' || typeof value.splice == 'function' ||
  55. isBuffer(value) || isTypedArray(value) || isArguments(value))) {
  56. return !value.length;
  57. }
  58. var tag = getTag(value);
  59. if (tag == mapTag || tag == setTag) {
  60. return !value.size;
  61. }
  62. if (isPrototype(value)) {
  63. return !baseKeys(value).length;
  64. }
  65. for (var key in value) {
  66. if (hasOwnProperty.call(value, key)) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. module.exports = isEmpty;