UNPKG

1.27 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var call = require('../internals/function-call');
4var aCallable = require('../internals/a-callable');
5var anObject = require('../internals/an-object');
6var getIteratorDirect = require('../internals/get-iterator-direct');
7var createIteratorProxy = require('../internals/iterator-create-proxy');
8var callWithSafeIterationClosing = require('../internals/call-with-safe-iteration-closing');
9var IS_PURE = require('../internals/is-pure');
10
11var IteratorProxy = createIteratorProxy(function () {
12 var iterator = this.iterator;
13 var predicate = this.predicate;
14 var next = this.next;
15 var result, done, value;
16 while (true) {
17 result = anObject(call(next, iterator));
18 done = this.done = !!result.done;
19 if (done) return;
20 value = result.value;
21 if (callWithSafeIterationClosing(iterator, predicate, [value, this.counter++], true)) return value;
22 }
23});
24
25// `Iterator.prototype.filter` method
26// https://github.com/tc39/proposal-iterator-helpers
27$({ target: 'Iterator', proto: true, real: true, forced: IS_PURE }, {
28 filter: function filter(predicate) {
29 anObject(this);
30 aCallable(predicate);
31 return new IteratorProxy(getIteratorDirect(this), {
32 predicate: predicate
33 });
34 }
35});