forked from reconbot/streaming-iterables
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented takeWhile re reconbot#275
- Loading branch information
1 parent
9c7eb28
commit 9bf8baf
Showing
3 changed files
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { assert } from 'chai' | ||
import { takeWhile } from './' | ||
import { asyncFromArray } from './util-test' | ||
|
||
describe('takeWhile', () => { | ||
const isTruthy = (i : any) => !!i | ||
const asyncIsTruthy = async (i : any) => !!i | ||
|
||
it('takes iterators while truthy', async () => { | ||
const numbers: number[] = [] | ||
for await (const num of takeWhile(isTruthy, [1, 2, null, undefined, 3])) { | ||
numbers.push(num as number) | ||
} | ||
assert.deepEqual(numbers, [1, 2]) | ||
}) | ||
it('takes async iterators while truthy', async () => { | ||
const numbers: number[] = [] | ||
for await (const num of takeWhile(isTruthy, asyncFromArray([1, 2, null, undefined, 3]))) { | ||
numbers.push(num as number) | ||
} | ||
assert.deepEqual(numbers, [1, 2]) | ||
}) | ||
it('async filters iterators', async () => { | ||
const numbers: number[] = [] | ||
for await (const num of takeWhile(asyncIsTruthy, [1, 2, null, undefined, 3])) { | ||
numbers.push(num as number) | ||
} | ||
assert.deepEqual(numbers, [1, 2]) | ||
}) | ||
|
||
it('is curryable', async () => { | ||
const numbers: number[] = [] | ||
const values = asyncFromArray([1, 2, null, 3]) | ||
const untilFalsy = takeWhile(isTruthy) | ||
for await (const num of untilFalsy(values)) { | ||
numbers.push(num as number) | ||
} | ||
assert.deepEqual(numbers, [1, 2]) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { AnyIterable } from './types' | ||
|
||
async function* _takeWhile<T>(predicate: (data: T) => boolean | Promise<boolean>, iterable: AnyIterable<T>) { | ||
for await (const data of iterable) { | ||
if (!await predicate(data)) { | ||
return | ||
} | ||
yield data | ||
} | ||
} | ||
|
||
/** | ||
* Takes a `predicate` and a `iterable`, and returns a new async iterator of the same type containing the members of the given iterable until the `predicate` returns false. | ||
```ts | ||
import { takeWhile } from 'streaming-iterables' | ||
import { getPokemon } from 'iterable-pokedex' | ||
const firstSlowOnes = takeWhile(pokemon => pokemon.baseStats.speed < 100) | ||
for await (const pokemon of firstSlowOnes(getPokemon())) { | ||
console.log(pokemon) | ||
} | ||
// Abomasnow | ||
// Abra | ||
// Absol | ||
``` | ||
*/ | ||
export function takeWhile<T, S extends T>( | ||
predicate: (data: T) => data is S | ||
): <A extends T>(curriedIterable: AnyIterable<A>) => AsyncGenerator<S> | ||
export function takeWhile<T>( | ||
predicate: (data: T) => boolean | Promise<boolean> | ||
): <A>(curriedIterable: AnyIterable<A>) => AsyncGenerator<A> | ||
export function takeWhile<T, S extends T>(predicate: (data: T) => data is S, iterable: AnyIterable<T>): AsyncGenerator<S> | ||
export function takeWhile<T>( | ||
predicate: (data: T) => boolean | Promise<boolean>, | ||
iterable: AnyIterable<T> | ||
): AsyncGenerator<T> | ||
export function takeWhile<T>(predicate: (data: T) => boolean | Promise<boolean>, iterable?: AnyIterable<T>) { | ||
if (iterable === undefined) { | ||
return (curriedIterable: AnyIterable<any>) => _takeWhile(predicate, curriedIterable) | ||
} | ||
return _takeWhile(predicate, iterable) | ||
} |