esnext.iterator.drop.js 1.1 KB

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