esnext.iterator.take.js 767 B

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