esnext.iterator.drop.js 917 B

123456789101112131415161718192021222324252627282930
  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. var iterator = this.iterator;
  9. var next = this.next;
  10. var result, done;
  11. while (this.remaining) {
  12. this.remaining--;
  13. result = anObject(next.call(iterator));
  14. done = this.done = !!result.done;
  15. if (done) return;
  16. }
  17. result = anObject(next.call(iterator, arg));
  18. done = this.done = !!result.done;
  19. if (!done) return result.value;
  20. });
  21. $({ target: 'Iterator', proto: true, real: true }, {
  22. drop: function drop(limit) {
  23. return new IteratorProxy({
  24. iterator: anObject(this),
  25. remaining: toPositiveInteger(limit)
  26. });
  27. }
  28. });