IterableToArrayLike.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var callBound = require('call-bind/callBound');
  3. var $arrayPush = callBound('Array.prototype.push');
  4. var getIteratorMethod = require('../helpers/getIteratorMethod');
  5. var AdvanceStringIndex = require('./AdvanceStringIndex');
  6. var GetIterator = require('./GetIterator');
  7. var GetMethod = require('./GetMethod');
  8. var IsArray = require('./IsArray');
  9. var IteratorStep = require('./IteratorStep');
  10. var IteratorValue = require('./IteratorValue');
  11. var ToObject = require('./ToObject');
  12. var ES = {
  13. AdvanceStringIndex: AdvanceStringIndex,
  14. GetMethod: GetMethod,
  15. IsArray: IsArray
  16. };
  17. // https://262.ecma-international.org/7.0/#sec-iterabletoarraylike
  18. module.exports = function IterableToArrayLike(items) {
  19. var usingIterator = getIteratorMethod(ES, items);
  20. if (typeof usingIterator !== 'undefined') {
  21. var iterator = GetIterator(items, usingIterator);
  22. var values = [];
  23. var next = true;
  24. while (next) {
  25. next = IteratorStep(iterator);
  26. if (next) {
  27. var nextValue = IteratorValue(next);
  28. $arrayPush(values, nextValue);
  29. }
  30. }
  31. return values;
  32. }
  33. return ToObject(items);
  34. };