From 14aa2d8b75fd16378c2be183fd5b008712547ed4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 17 Mar 2023 11:32:43 -0700 Subject: [PATCH] [Fix] `filter`: properly increment the counter --- Iterator.prototype.filter/implementation.js | 2 +- test/Iterator.prototype.filter.js | 44 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Iterator.prototype.filter/implementation.js b/Iterator.prototype.filter/implementation.js index d5ce5fe..31ce962 100644 --- a/Iterator.prototype.filter/implementation.js +++ b/Iterator.prototype.filter/implementation.js @@ -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 diff --git a/test/Iterator.prototype.filter.js b/test/Iterator.prototype.filter.js index 1efcc80..444e096 100644 --- a/test/Iterator.prototype.filter.js +++ b/test/Iterator.prototype.filter.js @@ -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) {