esnext.iterator.take.js 1.1 KB

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