From 4966f47472165f382022981f9c2d05b74eb56c27 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 31 Mar 2023 15:03:12 +0100 Subject: [PATCH 1/4] fix: yield first mapped/filtered values Fixes regression introduced in last major release. --- packages/it-filter/src/index.ts | 4 ++++ packages/it-filter/test/index.spec.ts | 7 ++++++ packages/it-map/package.json | 3 ++- packages/it-map/src/index.ts | 2 ++ packages/it-map/test/index.spec.ts | 31 ++++++++++++++------------- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/packages/it-filter/src/index.ts b/packages/it-filter/src/index.ts index d5507138..49f5d26e 100644 --- a/packages/it-filter/src/index.ts +++ b/packages/it-filter/src/index.ts @@ -49,6 +49,10 @@ function filter (source: Iterable | AsyncIterable, fn: (val: T) => boo const func = fn as (val: T) => boolean return (function * () { + if (res === true) { + yield value + } + for (const entry of source) { 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-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..ced39741 100644 --- a/packages/it-map/src/index.ts +++ b/packages/it-map/src/index.ts @@ -44,6 +44,8 @@ function map (source: AsyncIterable | Iterable, func: (val: I) => O const fn = func as (val: I) => O return (function * () { + yield res as O + for (const val of source) { 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) }) }) From 830abfb3def03ceadb9809f41b3c3419e429a893 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 31 Mar 2023 15:12:54 +0100 Subject: [PATCH 2/4] fix: yield from peekable --- packages/it-filter/src/index.ts | 2 +- packages/it-foreach/src/index.ts | 2 +- packages/it-map/src/index.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/it-filter/src/index.ts b/packages/it-filter/src/index.ts index 49f5d26e..03d7de4f 100644 --- a/packages/it-filter/src/index.ts +++ b/packages/it-filter/src/index.ts @@ -53,7 +53,7 @@ function filter (source: Iterable | AsyncIterable, fn: (val: T) => boo yield value } - for (const entry of source) { + for (const entry of peekable) { if (func(entry)) { yield entry } diff --git a/packages/it-foreach/src/index.ts b/packages/it-foreach/src/index.ts index 7d8d2ba3..28b06f5b 100644 --- a/packages/it-foreach/src/index.ts +++ b/packages/it-foreach/src/index.ts @@ -44,7 +44,7 @@ function forEach (source: Iterable | AsyncIterable, fn: (thing: T) => const func = fn as (val: T) => void return (function * () { - for (const val of source) { + for (const val of peekable) { func(val) yield val } diff --git a/packages/it-map/src/index.ts b/packages/it-map/src/index.ts index ced39741..6d469110 100644 --- a/packages/it-map/src/index.ts +++ b/packages/it-map/src/index.ts @@ -46,7 +46,7 @@ function map (source: AsyncIterable | Iterable, func: (val: I) => O return (function * () { yield res as O - for (const val of source) { + for (const val of peekable) { yield fn(val) } })() From 55f178c90a2883ae9dcfb4cf65ee53e1887f5d1c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 31 Mar 2023 15:20:00 +0100 Subject: [PATCH 3/4] fix: yield first value --- packages/it-foreach/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/it-foreach/src/index.ts b/packages/it-foreach/src/index.ts index 28b06f5b..e7e467ba 100644 --- a/packages/it-foreach/src/index.ts +++ b/packages/it-foreach/src/index.ts @@ -44,6 +44,8 @@ function forEach (source: Iterable | AsyncIterable, fn: (thing: T) => const func = fn as (val: T) => void return (function * () { + yield value as T + for (const val of peekable) { func(val) yield val From 7fb0deb029bcb3ecc808e4db8364a8f3e70f180b Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 31 Mar 2023 15:24:40 +0100 Subject: [PATCH 4/4] chore: linting --- packages/it-foreach/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/it-foreach/src/index.ts b/packages/it-foreach/src/index.ts index e7e467ba..63455f6d 100644 --- a/packages/it-foreach/src/index.ts +++ b/packages/it-foreach/src/index.ts @@ -44,7 +44,7 @@ function forEach (source: Iterable | AsyncIterable, fn: (thing: T) => const func = fn as (val: T) => void return (function * () { - yield value as T + yield value for (const val of peekable) { func(val)