diff --git a/Lazy/zipWithIndexL.js b/Lazy/zipWithIndexL.js index b4f8281..7e0c8ec 100644 --- a/Lazy/zipWithIndexL.js +++ b/Lazy/zipWithIndexL.js @@ -1,6 +1,10 @@ +import go1 from "../Strict/go1.js"; import toIter from "../Strict/toIter.js"; export default function* zipWithIndexL(iter) { let i = -1; - for (const a of toIter(iter)) yield [++i, a]; + for (const a of toIter(iter)) { + const _i = ++i; + yield go1(a, (a) => [_i, a]); + } } diff --git a/test/spec.js b/test/spec.js index 6a02769..2dd321b 100644 --- a/test/spec.js +++ b/test/spec.js @@ -2750,4 +2750,26 @@ const { expect } = chai; expect(await last([1, 2, Promise.resolve(3)])).to.eql(3); }); }); + + describe(`L.zipWIthIndex`, function () { + it(`Yield each element with index`, function () { + // given + const items = ["a", "b", "c", "d", "e"]; + + // when + const result = L.zipWithIndex(items); + + // then + let cur; + for (let i = 0; i < items.length; i++) { + const item = items[i]; + cur = result.next(); + expect(cur.done).false; + expect(i).to.equal(cur.value[0]); + expect(item).to.equal(cur.value[1]); + } + cur = result.next(); + expect(cur.done).true; + }); + }); })();