1 | ## _readline_
|
2 | > Read a file line by line.
|
3 |
|
4 | ## Install
|
5 |
|
6 | ## Important. In node 10 there is a core module named readline. Please use linebyline instead, it is the same module just renamed:
|
7 | [Npm linebyline](https://www.npmjs.com/package/linebyline)
|
8 |
|
9 | ```sh
|
10 | npm install linebyline
|
11 | ```
|
12 |
|
13 | ## Test
|
14 | ```sh
|
15 | npm install .
|
16 | npm test
|
17 |
|
18 | ```
|
19 |
|
20 |
|
21 | ## What's this?
|
22 |
|
23 | Simple streaming readline module for NodeJS. Reads a file and buffers new lines emitting a _line_ event for each line.
|
24 |
|
25 | ## Usage
|
26 | ### Simple
|
27 | ```js
|
28 | var readline = require('linebyline'),
|
29 | rl = readline('./somefile.txt');
|
30 | rl.on('line', function(line, lineCount, byteCount) {
|
31 | // do something with the line of text
|
32 | })
|
33 | .on('error', function(e) {
|
34 | // something went wrong
|
35 | });
|
36 | ```
|
37 |
|
38 | ### ASCII file decoding
|
39 | As the underlying `fs.createReadStream` doesn't care about the specific ASCII encoding of the file, an alternative way to decode the file is by telling the `readline` library to retain buffer and then decoding it using a converter (e.g. [`iconv-lite`](https://www.npmjs.com/package/iconv-lite)).
|
40 | ```js
|
41 | var readline = require('linebyline'),
|
42 | rl = readline('./file-in-win1251.txt', {
|
43 | retainBuffer: true //tell readline to retain buffer
|
44 | });
|
45 | rl.on("line", function (data,linecount){
|
46 | var line = iconv.decode(data, 'win1251');
|
47 | // do something with the line of converted text
|
48 | });
|
49 | ```
|
50 | ##API
|
51 | ## readLine(readingObject[, options])
|
52 | ### Params:
|
53 |
|
54 | * `readingObject` - file path or stream object
|
55 | * `options` can include:
|
56 | * `maxLineLength` - override the default 4K buffer size (lines longer than this will not be read)
|
57 | * `retainBuffer` - avoid converting to String prior to emitting 'line' event; will pass raw buffer with encoded data to the callback
|
58 |
|
59 | ### Return:
|
60 |
|
61 | * **EventEmitter**
|
62 |
|
63 |
|
64 | ## License
|
65 |
|
66 | BSD © [Craig Brookes](http://craigbrookes.com/)
|