isEmpty.js 980 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. *
  8. */
  9. 'use strict';
  10. /* eslint-disable fb-www/typeof-undefined */
  11. /* eslint-disable no-unused-vars */
  12. var invariant = require('./invariant');
  13. /**
  14. * Checks if a value is empty.
  15. */
  16. function isEmpty(value) {
  17. if (Array.isArray(value)) {
  18. return value.length === 0;
  19. } else if (typeof value === 'object') {
  20. if (value) {
  21. !(!isIterable(value) || value.size === undefined) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'isEmpty() does not support iterable collections.') : invariant(false) : void 0;
  22. for (var _ in value) {
  23. return false;
  24. }
  25. }
  26. return true;
  27. } else {
  28. return !value;
  29. }
  30. }
  31. function isIterable(value) {
  32. if (typeof Symbol === 'undefined') {
  33. return false;
  34. }
  35. return value[Symbol.iterator];
  36. }
  37. module.exports = isEmpty;