npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dwesley/linereader

v0.1.0

Published

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

Downloads

10

Readme

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"

async function main() {
  /** could be any valid path to a readable file */
  const PATH: PathLike = "/dev/stdin"

  /** the variable value below can be any valid encoding available in the `BufferEncoding` type */
  const ENCODING: BufferEncoding = "utf8"

  /** `LineReader` instance from the file specified in `PATH` variable  */
  const lineReader = LineReader.create(PATH, ENCODING)

  const output = []
  while (lineReader.hasNextLine()) {
    const line = await lineReader.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:

| Name | Type | Default | Description | | -------- | ------------------------------------------------------------------------------------------------------------- | -------- | ------------------------------------------------ | | path | string \| Buffer \| URL | none | The 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:

| Name | ReturnType | Description | | ----------- | ---------- | ---------------------------------------------------------------------------------------------------------------- | | hasNextLine | boolean | A function that returns true if there is another line to read, or false if close() has already been called | | nextLine | Promise | An async function that reads the next line from the file and returns it. | | close | void | A 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:

const lineReader = LineReader.create("./file.txt", "utf-8")
const output = []
while (lineReader.hasNextLine()) {
  const line = await lineReader.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 */
const helper = (line: string = ""): number[] => {
  return line.split(",", 1e5).map((value) => Number.parseInt(value, 10))
}

const output = []
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
   */
  const numbers = await lineReader.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:

const lineReader = LineReader.create("./file.txt")

for (let i = 0; i < 2; i++) {
  const line = await lineReader.nextLine()
  else console.log(line)
}

if (lineReader.hasNextLine())
  lineReader.close()

console.log(await lineReader.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.