Skip to content

Commit

Permalink
fix: yield first mapped/filtered values (#60)
Browse files Browse the repository at this point in the history
Fixes regression introduced in last major release.
  • Loading branch information
achingbrain authored Mar 31, 2023
1 parent d754628 commit 778a991
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
6 changes: 5 additions & 1 deletion packages/it-filter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ function filter <T> (source: Iterable<T> | AsyncIterable<T>, 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
}
Expand Down
7 changes: 7 additions & 0 deletions packages/it-filter/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 3 additions & 1 deletion packages/it-foreach/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ function forEach <T> (source: Iterable<T> | AsyncIterable<T>, 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
}
Expand Down
3 changes: 2 additions & 1 deletion packages/it-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion packages/it-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ function map <I, O> (source: AsyncIterable<I> | Iterable<I>, 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)
}
})()
Expand Down
31 changes: 16 additions & 15 deletions packages/it-map/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'aegir/chai'
import map from '../src/index.js'
import all from 'it-all'

async function * asyncGenerator (): AsyncGenerator<number> {
yield 1
Expand All @@ -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)
})
})

0 comments on commit 778a991

Please sign in to comment.