https://github.com/morganconrad/f-readline
Thin layer over node's readline module to provide functional support: map, reduce, filter, forEach()
https://github.com/morganconrad/f-readline
async filter foreach functional-programming map node-readline readline reduce
Last synced: 4 months ago
JSON representation
Thin layer over node's readline module to provide functional support: map, reduce, filter, forEach()
- Host: GitHub
- URL: https://github.com/morganconrad/f-readline
- Owner: MorganConrad
- License: mit
- Created: 2020-01-11T17:58:20.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:25:33.000Z (almost 3 years ago)
- Last Synced: 2025-06-09T10:06:56.645Z (5 months ago)
- Topics: async, filter, foreach, functional-programming, map, node-readline, readline, reduce
- Language: JavaScript
- Size: 144 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](http://travis-ci.org/MorganConrad/f-readline)
[](https://github.com/MorganConrad/f-readline)
[](https://www.npmjs.org/package/f-readline)
[](https://snyk.io/test/github/morganconrad/f-readline)
[](https://coveralls.io/github/MorganConrad/f-readline)
# f-readline
For a long time, Node had no "easy" way to read a stream line by line. Until v11.4, when [readline](https://nodejs.org/api/readline.html) added [support for async iteration](https://github.com/nodejs/node/pull/23916).
This module is a thin layer over readline to provide functional programming constructs: `filter(), forEach(), map(), reduce()`. It also provides a convenient `getAllLines()`.
## Basic API
**Note:** Other than the constructor, all methods are **async**.
### constructor(readable, interfaceOptions) constructor
- readable is the stream to be read
- interfaceOptions (optional, default = {}) will be passed to [readline.createInterface(interfaceOptions)](https://nodejs.org/api/readline.html#readline_readline_createinterface_options)
- crlfDelay defaults to 999999
- input is set to the `readable` argument
### async getAllLines()
Convenience method to just provide an array of all the lines. Obviously it must all fit in memory!
## Functional API
The "functional" methods below accept a user function (or "predicate") **fn** as their first argument. This method is usually called with three arguments:
- the line
- the line count (starting at 0)
- the instance of f-readline. Generally useless but see notes at end
### async filter(fn)
Returns an array of all lines passing the predicate `fn(line, index, this)`
### async forEach(fn)
Calls `fn(line, index, this)` for each line.
### async map(fn)
Returns an array obtained by calling `fn(line, index, this)` for each line
### async reduce(fn, acc)
Reduces using `fn(acc, line, index, this)`
### Notes, Todos, and Caveats
#### Since readline is clever on memory (?), this may save on memory
- if you are just counting lines or characters
- if you are filtering just a small subset of the input
#### What good is the 3rd argument to `fn()`?
- The interfaceOptions are available in `.interfaceOptions`
- The created interface is available in `.rl`
- If you want to pass other client specific info to **fn**, just add it to the FReadLine instance, _e.g._
```js
let frl = new FReadLine(readable, interfaceOptions);
frl.clientData = { your data here };
// then, during the call to fn(), you could access those
fn(line, index, frl) {
do something with frl.clientData
}
```
#### This module has nothing to do with prompting the user, pausing the input, etc. Just reading a stream line by line.
### Alternatives
All of these do their own twiddly buffering and eol parsing, instead of relying on a "robust" built-in library.
#### [file-readline](https://www.npmjs.com/package/file-readline)
- non-functional
- only reads a **file**, not any stream
#### [n-readlines](https://www.npmjs.com/package/n-readlines)
- non-functional
- synchronous
#### [readlines-ng](https://www.npmjs.com/package/readlines-ng)
- non-functional
- looks pretty good otherwise and claims to be fast.