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.
- Host: GitHub
- URL: https://github.com/parro-it/ai-filter
- Owner: parro-it
- License: mit
- Created: 2017-11-17T20:18:01.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-29T16:02:13.000Z (about 8 years ago)
- Last Synced: 2024-10-12T16:58:30.005Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 514 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# ai-filter
[](http://travis-ci.org/parro-it/ai-filter)
[](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