Skip to content

Commit

Permalink
[Fix] filter: properly increment the counter
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 17, 2023
1 parent 1080288 commit 14aa2d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Iterator.prototype.filter/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ module.exports = function filter(predicate) {
}

var sentinel = {};
var counter = 0; // step 3.a
var closure = function () {
var counter = 0; // step 3.a
// eslint-disable-next-line no-constant-condition
while (true) { // step 3.b
var next = IteratorStep(iterated); // step 3.b.i
Expand Down
44 changes: 44 additions & 0 deletions test/Iterator.prototype.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,50 @@ module.exports = {

st.end();
});

t.test('262: test/built-ins/Iterator/prototype/filter/predicate-args', function (st) {
var g = function g() {
var arr = ['a', 'b', 'c'];
var i = 0;
return {
next: function () {
try {
return {
value: arr[i],
done: i >= arr.length
};
} finally {
i += 1;
}
}
};
};
var assertionCount = 0;
var iter = filter(
g(),
function (value, count) {
if (value === 'a') {
st.equal(count, 0, 'first iteration');
} else if (value === 'b') {
st.equal(count, 1, 'second iteration');
} else if (value === 'c') {
st.equal(count, 2, 'third iteration');
} else {
st.fail('unexpected iteration');
}
assertionCount += 1;
return true;
}
);

st.equal(assertionCount, 0, 'prior to iteration');

testIterator(iter, ['a', 'b', 'c'], st, 'iteration');

st.equal(assertionCount, 3);

st.end();
});
},
index: function () {
test('Iterator.prototype.' + fnName + ': index', function (t) {
Expand Down

0 comments on commit 14aa2d8

Please sign in to comment.