esnext.iterator.drop.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  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 call = require('../internals/function-call');
  6. var anObject = require('../internals/an-object');
  7. var toPositiveInteger = require('../internals/to-positive-integer');
  8. var createIteratorProxy = require('../internals/iterator-create-proxy');
  9. var IteratorProxy = createIteratorProxy(function (args) {
  10. var iterator = this.iterator;
  11. var next = this.next;
  12. var result, done;
  13. while (this.remaining) {
  14. this.remaining--;
  15. result = anObject(call(next, iterator));
  16. done = this.done = !!result.done;
  17. if (done) return;
  18. }
  19. result = anObject(apply(next, iterator, args));
  20. done = this.done = !!result.done;
  21. if (!done) return result.value;
  22. });
  23. $({ target: 'Iterator', proto: true, real: true, forced: true }, {
  24. drop: function drop(limit) {
  25. return new IteratorProxy({
  26. iterator: anObject(this),
  27. remaining: toPositiveInteger(limit)
  28. });
  29. }
  30. });