isEmpty.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var isArguments = require('./isArguments'),
  2. isArray = require('./isArray'),
  3. isArrayLike = require('../internal/isArrayLike'),
  4. isFunction = require('./isFunction'),
  5. isObjectLike = require('../internal/isObjectLike'),
  6. isString = require('./isString'),
  7. keys = require('../object/keys');
  8. /**
  9. * Checks if `value` is empty. A value is considered empty unless it's an
  10. * `arguments` object, array, string, or jQuery-like collection with a length
  11. * greater than `0` or an object with own enumerable properties.
  12. *
  13. * @static
  14. * @memberOf _
  15. * @category Lang
  16. * @param {Array|Object|string} value The value to inspect.
  17. * @returns {boolean} Returns `true` if `value` is empty, else `false`.
  18. * @example
  19. *
  20. * _.isEmpty(null);
  21. * // => true
  22. *
  23. * _.isEmpty(true);
  24. * // => true
  25. *
  26. * _.isEmpty(1);
  27. * // => true
  28. *
  29. * _.isEmpty([1, 2, 3]);
  30. * // => false
  31. *
  32. * _.isEmpty({ 'a': 1 });
  33. * // => false
  34. */
  35. function isEmpty(value) {
  36. if (value == null) {
  37. return true;
  38. }
  39. if (isArrayLike(value) && (isArray(value) || isString(value) || isArguments(value) ||
  40. (isObjectLike(value) && isFunction(value.splice)))) {
  41. return !value.length;
  42. }
  43. return !keys(value).length;
  44. }
  45. module.exports = isEmpty;