Skip to content

Commit 57a2f1e

Browse files
authored
docs: add docs for takeWhile and takeLast (#287)
1 parent aca5ccd commit 57a2f1e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Diff for: README.md

+41
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Since this works with async iterators it requires node 10 or higher.
6363
- [`pipeline()`](#pipeline)
6464
- [`reduce()`](#reduce)
6565
- [`take()`](#take)
66+
- [`takeLast()`](#takelast)
67+
- [`takeWhile()`](#takewhile)
6668
- [`tap()`](#tap)
6769
- [`throttle()`](#throttle)
6870
- [`time()`](#time)
@@ -512,6 +514,45 @@ const topFive = await collect(take(5, getPokemon()))
512514
// first five pokemon
513515
```
514516

517+
### takeLast
518+
519+
```ts
520+
function takeLast<T>(count: number, iterable: AsyncIterable<T>): AsyncIterableIterator<T>
521+
function takeLast<T>(count: number, iterable: Iterable<T>): IterableIterator<T>
522+
```
523+
524+
Returns a new iterator that reads a specific number of items from the end of `iterable` once it has completed. When used with generators it advances the generator, when used with arrays it gets a new iterator and starts from the beginning.
525+
526+
```ts
527+
import { pipeline, takeLast, collect } from 'streaming-iterables'
528+
import { getPokemon } from 'iterable-pokedex'
529+
530+
const bottomFive = await collect(takeLast(5, getPokemon()))
531+
// last five pokemon
532+
```
533+
534+
### takeWhile
535+
536+
```ts
537+
function takeWhile<T, S extends T>(predicate: (data: T) => data is S, iterable: AnyIterable<T>): AsyncGenerator<S>;
538+
```
539+
540+
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.
541+
542+
```ts
543+
import { takeWhile } from 'streaming-iterables'
544+
import { getPokemon } from 'iterable-pokedex'
545+
546+
const firstSlowOnes = takeWhile(pokemon => pokemon.baseStats.speed < 100)
547+
548+
for await (const pokemon of firstSlowOnes(getPokemon())) {
549+
console.log(pokemon)
550+
}
551+
// Abomasnow
552+
// Abra
553+
// Absol
554+
```
555+
515556
### tap
516557

517558
```ts

0 commit comments

Comments
 (0)