is-empty.js 883 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var isNil = require('./type/is-nil');
  2. var isArrayLike = require('./type/is-array-like');
  3. var getType = require('./type/get-type');
  4. var isPrototype = require('./type/is-prototype');
  5. var hasOwnProperty = Object.prototype.hasOwnProperty;
  6. function isEmpty(value) {
  7. /**
  8. * isEmpty(null) => true
  9. * isEmpty() => true
  10. * isEmpty(true) => true
  11. * isEmpty(1) => true
  12. * isEmpty([1, 2, 3]) => false
  13. * isEmpty('abc') => false
  14. * isEmpty({ a: 1 }) => false
  15. */
  16. if (isNil(value)) {
  17. return true;
  18. }
  19. if (isArrayLike(value)) {
  20. return !value.length;
  21. }
  22. var type = getType(value);
  23. if (type === 'Map' || type === 'Set') {
  24. return !value.size;
  25. }
  26. if (isPrototype(value)) {
  27. return !Object.keys(value).length;
  28. }
  29. for (var key in value) {
  30. if (hasOwnProperty.call(value, key)) {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. module.exports = isEmpty;