esnext.iterator.concat.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var aCallable = require('../internals/a-callable');
  5. var anObject = require('../internals/an-object');
  6. var getIteratorMethod = require('../internals/get-iterator-method');
  7. var createIteratorProxy = require('../internals/iterator-create-proxy');
  8. var $Array = Array;
  9. var IteratorProxy = createIteratorProxy(function () {
  10. while (true) {
  11. var iterator = this.iterator;
  12. if (!iterator) {
  13. var iterableIndex = this.nextIterableIndex++;
  14. var iterables = this.iterables;
  15. if (iterableIndex >= iterables.length) {
  16. this.done = true;
  17. return;
  18. }
  19. var entry = iterables[iterableIndex];
  20. this.iterables[iterableIndex] = null;
  21. iterator = this.iterator = call(entry.method, entry.iterable);
  22. this.next = iterator.next;
  23. }
  24. var result = anObject(call(this.next, iterator));
  25. if (result.done) {
  26. this.iterator = null;
  27. this.next = null;
  28. continue;
  29. }
  30. return result.value;
  31. }
  32. });
  33. // `Iterator.concat` method
  34. // https://github.com/tc39/proposal-iterator-sequencing
  35. $({ target: 'Iterator', stat: true, forced: true }, {
  36. concat: function concat() {
  37. var length = arguments.length;
  38. var iterables = $Array(length);
  39. for (var index = 0; index < length; index++) {
  40. var item = anObject(arguments[index]);
  41. iterables[index] = {
  42. iterable: item,
  43. method: aCallable(getIteratorMethod(item))
  44. };
  45. }
  46. return new IteratorProxy({
  47. iterables: iterables,
  48. nextIterableIndex: 0,
  49. iterator: null,
  50. next: null
  51. });
  52. }
  53. });