esnext.iterator.drop.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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 IteratorProxy = createIteratorProxy(function () {
  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(call(next, iterator));
  20. done = this.done = !!result.done;
  21. if (!done) return result.value;
  22. });
  23. // `Iterator.prototype.drop` method
  24. // https://github.com/tc39/proposal-iterator-helpers
  25. $({ target: 'Iterator', proto: true, real: true }, {
  26. drop: function drop(limit) {
  27. return new IteratorProxy(getIteratorDirect(this), {
  28. remaining: toPositiveInteger(notANaN(+limit))
  29. });
  30. }
  31. });