arrayFrom.mjs 1.3 KB

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