es.iterator.drop.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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 IS_PURE = require('../internals/is-pure');
  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. // `Iterator.prototype.drop` method
  25. // https://tc39.es/ecma262/#sec-iterator.prototype.drop
  26. $({ target: 'Iterator', proto: true, real: true, forced: IS_PURE }, {
  27. drop: function drop(limit) {
  28. anObject(this);
  29. var remaining = toPositiveInteger(notANaN(+limit));
  30. return new IteratorProxy(getIteratorDirect(this), {
  31. remaining: remaining
  32. });
  33. }
  34. });