An open API service indexing awesome lists of open source software.

https://github.com/parro-it/ai-filter

Filter over async iterables.
https://github.com/parro-it/ai-filter

Last synced: 10 months ago
JSON representation

Filter over async iterables.

Awesome Lists containing this project

README

          

# ai-filter

[![Travis Build Status](https://img.shields.io/travis/parro-it/ai-filter/master.svg)](http://travis-ci.org/parro-it/ai-filter)
[![NPM downloads](https://img.shields.io/npm/dt/ai-filter.svg)](https://npmjs.org/package/ai-filter)

> Filter over async iterables.

This method creates a new async iterable with all elements that pass the test
implemented by the provided function.

## Async iterable fun

**This module is part of
[Async iterable fun](https://github.com/parro-it/ai-fun), a complete toolset of
modules to work with async iterables.**

## Usage

This example read a text file, filtering only chunks containing a new line:

```js
import filter from 'ai-filter';
import { createReadStream } from "fs";

const stream = createReadStream(`file.txt`, "utf8");
const result = filter(chunk => chunk.contains('\n'), fromStream(stream))
for await (const chunk of result) {
console.log(chunk);
}
```

## API

### index

The filter() method creates a new async iterable with all elements that pass the
test implemented by the provided function.

**Parameters**

* `predicate`
**[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)**
is a predicate, to test each element of the async iterable. Return true to
keep the element, false otherwise, taking three arguments:

```
element: The current element being processed in the async iterable.
index: The index of the current element being processed in the async iterable.
iterable: The async iterable filter was called upon.
```

* `data` **AsyncIterable** The source async iterable to filter.

Returns **AsyncIterable** A new async iterable with the elements that pass the
test.

## Install

With [npm](https://npmjs.org/) installed, run

```bash
npm install --save ai-filter
```

## See Also

* [`noffle/common-readme`](https://github.com/noffle/common-readme)
* [`parro-it/ai-fun`](https://github.com/parro-it/ai-fun)

## License

MIT