streaming-iterables
Advanced tools
Comparing version 7.0.4 to 7.1.0
@@ -121,2 +121,4 @@ /** | ||
export declare type CurriedTakeLastResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterable<M>; | ||
export declare type CurriedTakeResult = <T, M extends AnyIterable<T>>(curriedIterable: M) => UnwrapAnyIterable<M>; | ||
@@ -196,5 +198,5 @@ | ||
*/ | ||
export declare function flatMap<T, B>(func: (data: T) => FlatMapValue<B>): (iterable: AnyIterable<T>) => AsyncGenerator<B>; | ||
export declare function flatMap<T, B>(func: (data: T) => FlatMapValue<B>): (iterable: AnyIterable<T>) => AsyncGenerator<NonNullable<B>>; | ||
export declare function flatMap<T, B>(func: (data: T) => FlatMapValue<B>, iterable: AnyIterable<T>): AsyncGenerator<B>; | ||
export declare function flatMap<T, B>(func: (data: T) => FlatMapValue<B>, iterable: AnyIterable<T>): AsyncGenerator<NonNullable<B>>; | ||
@@ -467,2 +469,42 @@ /** | ||
/** | ||
* 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. | ||
```ts | ||
import { pipeline, takeLast, collect } from 'streaming-iterables' | ||
import { getPokemon } from 'iterable-pokedex' | ||
const bottomFive = await collect(takeLast(5, getPokemon())) | ||
// last five pokemon | ||
``` | ||
*/ | ||
export declare function takeLast(count: number): CurriedTakeLastResult; | ||
export declare function takeLast<T, M extends AnyIterable<T>>(count: number, iterable: M): UnwrapAnyIterable<M>; | ||
/** | ||
* 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 declare function takeWhile<T, S extends T>(predicate: (data: T) => data is S): <A extends T>(curriedIterable: AnyIterable<A>) => AsyncGenerator<S>; | ||
export declare function takeWhile<T>(predicate: (data: T) => boolean | Promise<boolean>): <A>(curriedIterable: AnyIterable<A>) => AsyncGenerator<A>; | ||
export declare function takeWhile<T, S extends T>(predicate: (data: T) => data is S, iterable: AnyIterable<T>): AsyncGenerator<S>; | ||
export declare function takeWhile<T>(predicate: (data: T) => boolean | Promise<boolean>, iterable: AnyIterable<T>): AsyncGenerator<T>; | ||
/** | ||
* Returns a new iterator that yields the data it consumes, passing the data through to a function. If you provide an async function, the iterator will wait for the promise to resolve before yielding the value. This is useful for logging, or processing information and passing it along. | ||
@@ -469,0 +511,0 @@ */ |
@@ -754,2 +754,51 @@ (function (global, factory) { | ||
async function* _takeLast(count, iterable) { | ||
const buffer = []; | ||
for await (const res of iterable) { | ||
buffer.push(res); | ||
if (buffer.length > count) { | ||
buffer.shift(); | ||
} | ||
} | ||
while (buffer.length) { | ||
yield await buffer.shift(); | ||
} | ||
} | ||
function* _syncTakeLast(count, iterable) { | ||
const buffer = []; | ||
for (const res of iterable) { | ||
buffer.push(res); | ||
if (buffer.length > count) { | ||
buffer.shift(); | ||
} | ||
} | ||
while (buffer.length) { | ||
yield buffer.shift(); | ||
} | ||
} | ||
function takeLast(count, iterable) { | ||
if (iterable === undefined) { | ||
return curriedIterable => takeLast(count, curriedIterable); | ||
} | ||
if (iterable[Symbol.asyncIterator]) { | ||
return _takeLast(count, iterable); | ||
} | ||
return _syncTakeLast(count, iterable); | ||
} | ||
async function* _takeWhile(predicate, iterable) { | ||
for await (const data of iterable) { | ||
if (!await predicate(data)) { | ||
return; | ||
} | ||
yield data; | ||
} | ||
} | ||
function takeWhile(predicate, iterable) { | ||
if (iterable === undefined) { | ||
return (curriedIterable) => _takeWhile(predicate, curriedIterable); | ||
} | ||
return _takeWhile(predicate, iterable); | ||
} | ||
async function* _asyncTap(func, iterable) { | ||
@@ -1042,2 +1091,4 @@ for await (const val of iterable) { | ||
exports.take = take; | ||
exports.takeLast = takeLast; | ||
exports.takeWhile = takeWhile; | ||
exports.tap = tap; | ||
@@ -1044,0 +1095,0 @@ exports.throttle = throttle; |
{ | ||
"name": "streaming-iterables", | ||
"version": "7.0.4", | ||
"version": "7.1.0", | ||
"description": "A collection of utilities for async iterables. Designed to replace your streams.", | ||
@@ -43,24 +43,24 @@ "main": "./dist/index.js", | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.23.0", | ||
"@types/chai": "4.3.1", | ||
"@microsoft/api-extractor": "7.29.5", | ||
"@types/chai": "4.3.3", | ||
"@types/mocha": "9.1.1", | ||
"@types/node": "17.0.30", | ||
"@types/sinon": "10.0.11", | ||
"@typescript-eslint/eslint-plugin": "5.21.0", | ||
"@typescript-eslint/parser": "5.21.0", | ||
"@types/sinon": "10.0.13", | ||
"@typescript-eslint/eslint-plugin": "5.34.0", | ||
"@typescript-eslint/parser": "5.34.0", | ||
"benchmark": "2.1.4", | ||
"bluestream": "10.3.3", | ||
"c8": "7.11.2", | ||
"c8": "7.12.0", | ||
"chai": "4.3.6", | ||
"eslint": "8.14.0", | ||
"eslint": "8.22.0", | ||
"eslint-config-airbnb-base": "15.0.0", | ||
"eslint-plugin-import": "2.26.0", | ||
"mocha": "9.2.2", | ||
"prettier": "2.6.2", | ||
"rollup": "2.71.1", | ||
"sinon": "13.0.2", | ||
"mocha": "10.0.0", | ||
"prettier": "2.7.1", | ||
"rollup": "2.78.1", | ||
"sinon": "14.0.0", | ||
"through2-concurrent": "2.0.0", | ||
"ts-node": "^10.7.0", | ||
"tslib": "^2.4.0", | ||
"typescript": "4.6.4" | ||
"ts-node": "10.9.1", | ||
"tslib": "2.4.0", | ||
"typescript": "4.7.4" | ||
}, | ||
@@ -67,0 +67,0 @@ "engines": { |
@@ -66,2 +66,4 @@ # streaming-iterables πββοΈ | ||
- [`take()`](#take) | ||
- [`takeLast()`](#takelast) | ||
- [`takeWhile()`](#takewhile) | ||
- [`tap()`](#tap) | ||
@@ -516,2 +518,41 @@ - [`throttle()`](#throttle) | ||
### takeLast | ||
```ts | ||
function takeLast<T>(count: number, iterable: AsyncIterable<T>): AsyncIterableIterator<T> | ||
function takeLast<T>(count: number, iterable: Iterable<T>): IterableIterator<T> | ||
``` | ||
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. | ||
```ts | ||
import { pipeline, takeLast, collect } from 'streaming-iterables' | ||
import { getPokemon } from 'iterable-pokedex' | ||
const bottomFive = await collect(takeLast(5, getPokemon())) | ||
// last five pokemon | ||
``` | ||
### takeWhile | ||
```ts | ||
function takeWhile<T, S extends T>(predicate: (data: T) => data is S, iterable: AnyIterable<T>): AsyncGenerator<S>; | ||
``` | ||
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 | ||
``` | ||
### tap | ||
@@ -518,0 +559,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
122071
2586
691
0