Skip to content

Repository files navigation

LineReader

A simple LineReader class, similar to Java's readline() method, written in Typescript (for Node) to read files line-by-line asynchronously and quickly.

Install

# using npm
npm install @dwesley/linereader

Usage

Import

import{LineReader}from"@dwesley/linereader"

Example

import{PathLike}from"node:fs"import{EOL}from"node:os"import{LineReader}from"@dwesley/linereader"asyncfunctionmain(){/** could be any valid path to a readable file */constPATH: PathLike="/dev/stdin"/** the variable value below can be any valid encoding available in the `BufferEncoding` type */constENCODING: BufferEncoding="utf8"/** `LineReader` instance from the file specified in `PATH` variable */constlineReader=LineReader.create(PATH,ENCODING)constoutput=[]while(lineReader.hasNextLine()){constline=awaitlineReader.nextLine()output.push(line)}console.log(output.join(EOL))}main()

API

LineReader.create(path: fs.PathLike, encoding: BufferEncoding = "utf8"): LineReaderInstance

The options you can pass are:

NameTypeDefaultDescription
pathstring | Buffer | URLnoneThe path or location of your file (required)
encoding'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'latin1' | 'binary' | 'hex''utf8'Character encoding to use on read() operation

Instance Methods

The methods of LineReader instance you can access are:

NameReturnTypeDescription
hasNextLinebooleanA function that returns true if there is another line to read, or false if close() has already been called
nextLinePromiseAn async function that reads the next line from the file and returns it.
closevoidA function that closes the LineReader and stops reading lines from the file.

LineReaderInstance.nextLine<Output>(fn?: (value: string) => Output): Promise<Output | string | undefined>

Asynchronously read next single line of current file stream.

Example:

constlineReader=LineReader.create("./file.txt","utf-8")constoutput=[]while(lineReader.hasNextLine()){constline=awaitlineReader.nextLine()output.push(line)}console.log(output.join(" | "))

./file.txt

1111
2222
3333
4444
5555
7777

Output:

1111 | 2222 | 3333 | 4444 | 5555 | | 7777

Example with helpers:

/** Helper function that should format the current line */consthelper=(line: string=""): number[]=>{returnline.split(",",1e5).map((value)=>Number.parseInt(value,10))}constoutput=[]while(lineReader.hasNextLine()){/** * Extended nextLine method for lineReader instance * This nextLine method uses `helper`function as proxy for return the line content * Its return type is inherited from helper's return type */constnumbers=awaitlineReader.nextLine(helper)output.push(numbers.join("-"))}console.log(output.join(EOL))

./using-helper.txt

1,2,3
4
5,,7

Output:

1-2-3
4
5-NaN-7

LineReaderInstance.close(): void

Manually closes the LineReader by calling the close method. This method will be called automatically on the last nextLine operation.

Example:

constlineReader=LineReader.create("./file.txt")for(leti=0;i<2;i++){constline=awaitlineReader.nextLine()elseconsole.log(line)}if(lineReader.hasNextLine())lineReader.close()console.log(awaitlineReader.nextLine())// undefined

./file.txt

1
2
3
4

Output:

1
2
undefined

Contribute

This is open for you to join if you can add value or benefit from this!

License

Feel free to use this library under the conditions of the MIT license.

About

A simple LineReader class, similar to Java's readline() method, written in Typescript (for Node) to read files line-by-line asynchronously and quickly.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages