isEmpty.js.flow 967 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. * @providesModule isEmpty
  8. * @flow
  9. */
  10. 'use strict';
  11. /* eslint-disable fb-www/typeof-undefined */
  12. /* eslint-disable no-unused-vars */
  13. const invariant = require('./invariant');
  14. /**
  15. * Checks if a value is empty.
  16. */
  17. function isEmpty(value: mixed): boolean {
  18. if (Array.isArray(value)) {
  19. return value.length === 0;
  20. } else if (typeof value === 'object') {
  21. if (value) {
  22. invariant(!isIterable(value) || value.size === undefined, 'isEmpty() does not support iterable collections.');
  23. for (const _ in value) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. } else {
  29. return !value;
  30. }
  31. }
  32. function isIterable(value: any): boolean {
  33. if (typeof Symbol === 'undefined') {
  34. return false;
  35. }
  36. return value[Symbol.iterator];
  37. }
  38. module.exports = isEmpty;