isIterableObject.js 939 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.isIterableObject = isIterableObject;
  6. /**
  7. * Returns true if the provided object is an Object (i.e. not a string literal)
  8. * and implements the Iterator protocol.
  9. *
  10. * This may be used in place of [Array.isArray()][isArray] to determine if
  11. * an object should be iterated-over e.g. Array, Map, Set, Int8Array,
  12. * TypedArray, etc. but excludes string literals.
  13. *
  14. * @example
  15. * ```ts
  16. * isIterableObject([ 1, 2, 3 ]) // true
  17. * isIterableObject(new Map()) // true
  18. * isIterableObject('ABC') // false
  19. * isIterableObject({ key: 'value' }) // false
  20. * isIterableObject({ length: 1, 0: 'Alpha' }) // false
  21. * ```
  22. */
  23. function isIterableObject(maybeIterable) {
  24. return (
  25. typeof maybeIterable === 'object' &&
  26. typeof (maybeIterable === null || maybeIterable === void 0
  27. ? void 0
  28. : maybeIterable[Symbol.iterator]) === 'function'
  29. );
  30. }