esnext.iterator.take.js 969 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. var $ = require('../internals/export');
  4. var apply = require('../internals/function-apply');
  5. var anObject = require('../internals/an-object');
  6. var toPositiveInteger = require('../internals/to-positive-integer');
  7. var createIteratorProxy = require('../internals/iterator-create-proxy');
  8. var iteratorClose = require('../internals/iterator-close');
  9. var IteratorProxy = createIteratorProxy(function (args) {
  10. var iterator = this.iterator;
  11. if (!this.remaining--) {
  12. this.done = true;
  13. return iteratorClose(iterator, 'normal', undefined);
  14. }
  15. var result = anObject(apply(this.next, iterator, args));
  16. var done = this.done = !!result.done;
  17. if (!done) return result.value;
  18. });
  19. $({ target: 'Iterator', proto: true, real: true, forced: true }, {
  20. take: function take(limit) {
  21. return new IteratorProxy({
  22. iterator: anObject(this),
  23. remaining: toPositiveInteger(limit)
  24. });
  25. }
  26. });