file-reader-reverse
v1.0.2
Published
Node.JS library to read files line-by-line from bottom (backwards) This library will give you the last line first.
Downloads
68
Maintainers
Readme
File Reader Reverse (TS)
Node.JS library to read files line-by-line from bottom (backwards) This library will give you the last line first.
Usage
In non-async context
import { ReverseFileReader } from 'file-reader-reverse'
const reader = new ReverseFileReader('filepath')
const generator = reader.getLatestEntires() // Returns an async generator
generator.next().then(line => { /* line 1 */ })
generator.next().then(line => { /* line 2 */ })
...
In async context
import { ReverseFileReader } from 'file-reader-reverse'
async function read() {
const reader = new ReverseFileReader('filepath')
for (await const line of reader.getLatestEntires()) {
console.log(line)
...
}
}
You may also pass an offset and limit to getLatestEntires(offset?: number, limit?: number)
const reader = new ReverseFileReader('filepath')
// Skip last 5 lines and read till start of file
const generator = reader.getLatestEntires(5, Infinity)
// Skip last 12 files and read next 16 lines
// (till 28th line from bottom of file)
const generator = reader.getLatestEntires(12, 16)
// Read from end to start of file
const generator = reader.getLatestEntires(0, Infinity)