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

line-transform-machines

v1.0.0

Published

Processes text input stream/file line by line. Takes care of I/O & Errors. Great for CLI apps.

Downloads

3

Readme

line-transform-machines

Processes text input stream/file line by line. Takes care of I/O & Errors. Great for CLI apps.

  • LineMachine/AsyncLineMachine processes input lines by calling a (sync/async) callback on them.

  • RxjsLineMachine processes input lines by subscribing to the customized Observable.

Installation

$ npm install line-transform-machines

Example

From the input file input.txt, print lines, in uppercase, with its line number at the beginning of each line. Do it only for non-empty lines.
So that:

Hello,

world!

Becomes:

1: HELLO,
3: WORLD!

Solution 1: use simple callback

import {createLineMachine} from 'line-transform-machines';
import {stdout} from 'node:process';

const toUpperIgnoreEmptyLinesNumbered = (s: string, lineNum: number) => {
  if (s.trim().length === 0) return null; // returning null removes that line from output
  return `${lineNum}: ${s.toUpperCase()}`;

const lineMachine = createLineMachine(toUpperIgnoreEmptyLinesNumbered);

const runner = async () => {
  try {
    await lineMachine('./examples/input.txt', stdout);
  } catch (err) {
    console.error(err);
  }
};
runner();

Solution 2: use RxJS

import {createLineMachine} from 'line-transform-machines';
import {Observable, map, filter} from 'rxjs';
import {stdout} from 'node:process';

const toUpperIgnoreEmptyLinesNumbered = (
  obs: Observable<{value: string; lineNumber: number}>
): Observable<string> => {
  return obs.pipe(
    filter(x => x.value.trim().length > 0),
    map(x => `${x.lineNumber}: ${x.value.toLocaleUpperCase()}`)
  );
};

const lineMachine = createRxjsLineMachine(toUpperIgnoreEmptyLinesNumbered);

// ...the same code as in solution 1
const runner = async () => {
  try {
    await lineMachine('./examples/input.txt', stdout);
  } catch (err) {
    console.error(err);
  }
};
runner();

Error handling

import {createLineMachine} from 'line-transform-machines';

// our callback that can throw error
const normalizeNumbers = (s: string) => {
  const num = parseInt(s);
  if (isNaN(num)) throw new Error(`Not a number: ${s}`);
  return num.toString();
};
const lineMachine = createLineMachine(normalizeNumbers);

const runner = async () => {
  try {
    await lineMachine('./examples/nums.txt', './examples/normalized.txt');
  } catch (err) {
    console.error(err);
  }
};
runner();

Input (./examples/nums.txt):

45
62
 12
  6
hello
5
3

Console output:

Error: [./examples/nums.txt:5]
Not a number: hello
    at Object.normalizeNumbers ...

See that input file name & line is present in the error message automatically;)

If output is a file, it contains computed values before error has been thrown:

Output file (./examples/normalized.txt):

45
62
12
6