arrayFrom.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _symbols = require("./symbols.js");
  7. /* eslint-disable no-redeclare */
  8. // $FlowFixMe[name-already-bound]
  9. var arrayFrom = Array.from || function (obj, mapFn, thisArg) {
  10. if (obj == null) {
  11. throw new TypeError('Array.from requires an array-like object - not null or undefined');
  12. } // Is Iterable?
  13. var iteratorMethod = obj[_symbols.SYMBOL_ITERATOR];
  14. if (typeof iteratorMethod === 'function') {
  15. var iterator = iteratorMethod.call(obj);
  16. var result = [];
  17. var step;
  18. for (var i = 0; !(step = iterator.next()).done; ++i) {
  19. result.push(mapFn.call(thisArg, step.value, i)); // Infinite Iterators could cause forEach to run forever.
  20. // After a very large number of iterations, produce an error.
  21. // istanbul ignore if (Too big to actually test)
  22. if (i > 9999999) {
  23. throw new TypeError('Near-infinite iteration.');
  24. }
  25. }
  26. return result;
  27. } // Is Array like?
  28. var length = obj.length;
  29. if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
  30. var _result = [];
  31. for (var _i = 0; _i < length; ++_i) {
  32. if (Object.prototype.hasOwnProperty.call(obj, _i)) {
  33. _result.push(mapFn.call(thisArg, obj[_i], _i));
  34. }
  35. }
  36. return _result;
  37. }
  38. return [];
  39. };
  40. var _default = arrayFrom;
  41. exports.default = _default;