isCollection.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isCollection;
  6. var _symbols = require("../polyfills/symbols");
  7. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  8. /**
  9. * Returns true if the provided object is an Object (i.e. not a string literal)
  10. * and is either Iterable or Array-like.
  11. *
  12. * This may be used in place of [Array.isArray()][isArray] to determine if an
  13. * object should be iterated-over. It always excludes string literals and
  14. * includes Arrays (regardless of if it is Iterable). It also includes other
  15. * Array-like objects such as NodeList, TypedArray, and Buffer.
  16. *
  17. * @example
  18. *
  19. * isCollection([ 1, 2, 3 ]) // true
  20. * isCollection('ABC') // false
  21. * isCollection({ length: 1, 0: 'Alpha' }) // true
  22. * isCollection({ key: 'value' }) // false
  23. * isCollection(new Map()) // true
  24. *
  25. * @param obj
  26. * An Object value which might implement the Iterable or Array-like protocols.
  27. * @return {boolean} true if Iterable or Array-like Object.
  28. */
  29. function isCollection(obj) {
  30. if (obj == null || _typeof(obj) !== 'object') {
  31. return false;
  32. } // Is Array like?
  33. var length = obj.length;
  34. if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
  35. return true;
  36. } // Is Iterable?
  37. return typeof obj[_symbols.SYMBOL_ITERATOR] === 'function';
  38. }