-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: return iterators from synchronous sources (#58)
Applies the same changes from #55 to it-filter BREAKING CHANGE: if you pass a synchronous iterator and a synchronous filter function it will return a synchronous generator in response
- Loading branch information
1 parent
bf14176
commit cdd84b4
Showing
4 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,60 @@ | ||
import peek from 'it-peekable' | ||
|
||
function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> { | ||
return thing[Symbol.asyncIterator] != null | ||
} | ||
|
||
/** | ||
* Filters the passed (async) iterable by using the filter function | ||
*/ | ||
export default async function * filter <T> (source: AsyncIterable<T> | Iterable<T>, fn: (val: T) => boolean | Promise<boolean>): AsyncGenerator<T, void, undefined> { | ||
for await (const entry of source) { | ||
if (await fn(entry)) { | ||
yield entry | ||
} | ||
function filter <T> (source: Iterable<T>, fn: (val: T) => Promise<boolean>): AsyncGenerator<T, void, undefined> | ||
function filter <T> (source: Iterable<T>, fn: (val: T) => boolean): Generator<T, void, undefined> | ||
function filter <T> (source: Iterable<T> | AsyncIterable<T>, fn: (val: T) => boolean | Promise<boolean>): AsyncGenerator<T, void, undefined> | ||
function filter <T> (source: Iterable<T> | AsyncIterable<T>, fn: (val: T) => boolean | Promise<boolean>): Generator<T, void, undefined> | AsyncGenerator<T, void, undefined> { | ||
if (isAsyncIterable(source)) { | ||
return (async function * () { | ||
for await (const entry of source) { | ||
if (await fn(entry)) { | ||
yield entry | ||
} | ||
} | ||
})() | ||
} | ||
|
||
// if mapping function returns a promise we have to return an async generator | ||
const peekable = peek(source) | ||
const { value, done } = peekable.next() | ||
|
||
if (done === true) { | ||
return (function * () {}()) | ||
} | ||
|
||
const res = fn(value) | ||
|
||
// @ts-expect-error .then is not present on O | ||
if (typeof res.then === 'function') { | ||
return (async function * () { | ||
if (await res) { | ||
yield value | ||
} | ||
|
||
for await (const entry of peekable) { | ||
if (await fn(entry)) { | ||
yield entry | ||
} | ||
} | ||
})() | ||
} | ||
|
||
const func = fn as (val: T) => boolean | ||
|
||
return (function * () { | ||
for (const entry of source) { | ||
if (func(entry)) { | ||
yield entry | ||
} | ||
} | ||
})() | ||
} | ||
|
||
export default filter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters