diff --git a/packages/it-filter/src/index.ts b/packages/it-filter/src/index.ts index d5507138..03d7de4f 100644 --- a/packages/it-filter/src/index.ts +++ b/packages/it-filter/src/index.ts @@ -49,7 +49,11 @@ function filter (source: Iterable | AsyncIterable, fn: (val: T) => boo const func = fn as (val: T) => boolean return (function * () { - for (const entry of source) { + if (res === true) { + yield value + } + + for (const entry of peekable) { if (func(entry)) { yield entry } diff --git a/packages/it-filter/test/index.spec.ts b/packages/it-filter/test/index.spec.ts index 762f190c..3c7787b7 100644 --- a/packages/it-filter/test/index.spec.ts +++ b/packages/it-filter/test/index.spec.ts @@ -18,6 +18,13 @@ describe('it-filter', () => { expect(res).to.deep.equal([3, 4]) }) + it('should filter all values less than 2', async () => { + const res = all(filter(values(), val => val < 2)) + + expect(res[Symbol.iterator]).to.be.ok() + expect(res).to.deep.equal([0, 1]) + }) + it('should filter all values greater than 2 with a promise', () => { const res = all(filter(values(), val => val > 2)) diff --git a/packages/it-foreach/src/index.ts b/packages/it-foreach/src/index.ts index 7d8d2ba3..63455f6d 100644 --- a/packages/it-foreach/src/index.ts +++ b/packages/it-foreach/src/index.ts @@ -44,7 +44,9 @@ function forEach (source: Iterable | AsyncIterable, fn: (thing: T) => const func = fn as (val: T) => void return (function * () { - for (const val of source) { + yield value + + for (const val of peekable) { func(val) yield val } diff --git a/packages/it-map/package.json b/packages/it-map/package.json index 33512a00..2b4e7b1d 100644 --- a/packages/it-map/package.json +++ b/packages/it-map/package.json @@ -135,7 +135,8 @@ "release": "aegir release" }, "devDependencies": { - "aegir": "^38.1.7" + "aegir": "^38.1.7", + "it-all": "^3.0.0" }, "dependencies": { "it-peekable": "^3.0.0" diff --git a/packages/it-map/src/index.ts b/packages/it-map/src/index.ts index ff3f6d1f..6d469110 100644 --- a/packages/it-map/src/index.ts +++ b/packages/it-map/src/index.ts @@ -44,7 +44,9 @@ function map (source: AsyncIterable | Iterable, func: (val: I) => O const fn = func as (val: I) => O return (function * () { - for (const val of source) { + yield res as O + + for (const val of peekable) { yield fn(val) } })() diff --git a/packages/it-map/test/index.spec.ts b/packages/it-map/test/index.spec.ts index 902bdb75..be55b0ae 100644 --- a/packages/it-map/test/index.spec.ts +++ b/packages/it-map/test/index.spec.ts @@ -1,5 +1,6 @@ import { expect } from 'aegir/chai' import map from '../src/index.js' +import all from 'it-all' async function * asyncGenerator (): AsyncGenerator { yield 1 @@ -18,44 +19,44 @@ describe('it-map', () => { const gen = map(asyncGenerator(), (val) => val + 1) expect(gen[Symbol.asyncIterator]).to.be.ok() - for await (const result of gen) { - expect(result).to.equal(2) - } + const results = await all(gen) + expect(results).to.have.lengthOf(1) + expect(results).to.have.nested.property('[0]', 2) }) it('should map an async iterator to a promise', async () => { const gen = map(asyncGenerator(), async (val) => val + 1) expect(gen[Symbol.asyncIterator]).to.be.ok() - for await (const result of gen) { - expect(result).to.equal(2) - } + const results = await all(gen) + expect(results).to.have.lengthOf(1) + expect(results).to.have.nested.property('[0]', 2) }) it('should map an iterator', () => { const gen = map(generator(), (val) => val + 1) expect(gen[Symbol.iterator]).to.be.ok() - for (const result of gen) { - expect(result).to.equal(2) - } + const results = all(gen) + expect(results).to.have.lengthOf(1) + expect(results).to.have.nested.property('[0]', 2) }) it('should map an iterator to a promise', async () => { const gen = map(generator(), async (val) => val + 1) expect(gen[Symbol.asyncIterator]).to.be.ok() - for await (const result of gen) { - expect(result).to.equal(2) - } + const results = await all(gen) + expect(results).to.have.lengthOf(1) + expect(results).to.have.nested.property('[0]', 2) }) it('should map a source', async () => { const gen = map(source(), (val) => val + 1) expect(gen[Symbol.asyncIterator]).to.be.ok() - for await (const result of gen) { - expect(result).to.equal(2) - } + const results = await all(gen) + expect(results).to.have.lengthOf(1) + expect(results).to.have.nested.property('[0]', 2) }) })