Read file, stream, string, buffer line by line without putting them all in memory. It supports cool features like `custom line separator`, `various encodings`, `reverse mode`, `iterable protocol`
npm install nexlineRead file, stream, string, buffer line by line without putting them all in memory.\
It supports cool features below.
* Lightweight
* Iterable protocol
* Handle large file with small memory footprint
* Support various input types (file, stream, string, buffer)
* Support various encodings
* Support reverse mode
* Support custom, multiple line separators
* Support multiple inputs
pause() method does not work immediately.\pause() works immediately, it is still inconvenient.\pause() and resume() at every line.\lineSeparator default value is changed from ['\n', '\r\n'] to '\n' for better performancelineSeparator: ['\n', '\r\n']
npm install nexline
`
How to use
$3
`js
const nexline = require('nexline');async function main () {
const nl = nexline(...);
while(true) {
const line = await nl.next();
console.log(line);
if (line === null) break; // If all data is read, returns null
}
}
`$3
`js
const nexline = require('nexline');async function main () {
const nl = nexline(...);
// nexline is iterable
for await (const line of nl) console.log(line);
}
`$3
* Don't forget to close file descriptor after finish
* You can use autoCloseFile: true to close file descriptor automatically
`js
const nl = nexline({
input: fs.openSync(path_to_file, 'r'),
// autoCloseFile: true,
});
`$3
`js
const nl = nexline({
input: fs.createReadStream(path_to_file),
});
`$3
`js
const nl = nexline({
input: 'foo\nbar\nbaz',
});
`$3
`js
const nl = nexline({
input: Buffer.from('foo\nbar\nbaz'),
});
`$3
`js
const nl = nexline({
input: [
fs.openSync(path_to_file1, 'r'),
fs.createReadStream(path_to_file2),
'foo\nbar\nbaz',
Buffer.from('foo\nbar\nbaz'),
],
});
`$3
See encodings supported by iconv-lite
`js
const nl = nexline({
input: fs.openSync(path_to_file, 'r'),
encoding: 'cp949',
});
`$3
`js
const nl = nexline({
input: 'foo;bar;baz',
lineSeparator: ';',
});
`$3
`js
const nl = nexline({
input: 'foo\r\nbar\nbaz',
lineSeparator: ['\n', '\r\n'], // You can handle both LF and CRLF like this.
});
`$3
`js
const nl = nexline({
input: fs.openSync(path_to_file, 'r'), // NOTE: You cannot use stream in reverse mode.
reverse: true,
});
`Methods
| Name | Description |
| ------------- | --------------- |
| next() | async, It returns line until all data is read, after then returns null` |