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

node-column-parser

v1.0.1

Published

Intended to provide better shell output to Javascript object parsing, turning a whitespace-delimited table (such as ps aux) returns, into an array of Javascript objects, each object keyed on column headers, using row values.

Downloads

1,057

Readme

node-column-parser

Intended to provide better shell output to Javascript object parsing, turning a whitespace-delimited table (such as ps aux) returns, into an array of Javascript objects, each object keyed on column headers, using row values.

Credits

https://github.com/namshi/node-shell-parser/ for the idea and outline for this README (but not for the implementation). I tried it first and found it buggy so decided to make a better one from scratch.

Install

You can install this library through NPM:

npm install node-column-parser

Definition:

  columnParser(shellOutput, options);
  • shellOutput: the string resulting from running your command
  • options: Currently an optional empty object which will contain a headers object on return

Usage

Execute a process, get its output and then simply feed it to the parser:


var columnParser=require("node-column-parser");

var util=require('util');

const exec = require('child_process').exec;
exec('ps u', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  var options={};
  var rows=columnParser(stdout, options);
  console.log("rows.length="+rows.length+"\n"+util.inspect(rows));
  console.log("HEADERS:\n"+util.inspect(options.headers));
  console.log(`stderr: ${stderr}`);
});

Execution results:

WarrenMac:node-column-parser warren.downs$ node pstest.js
[ { USER: 'warren.downs',
    PID: '84777',
    '%CPU': '0.0',
    '%MEM': '0.2',
    VSZ: '057464',
    RSS: '9848',
    TT: 's000',
    STAT: 'R+',
    STARTED: '4:49PM',
    TIME: '0:00.08',
    COMMAND: 'node pstest.js' },
  { USER: 'warren.downs',
    PID: '84320',
    '%CPU': '0.0',
    '%MEM': '0.0',
    VSZ: '463084',
    RSS: '1592',
    TT: 's001',
    STAT: 'S+',
    STARTED: '4:05PM',
    TIME: '0:00.01',
    COMMAND: '-bash' },
  { USER: 'warren.downs',
    PID: '14166',
    '%CPU': '0.0',
    '%MEM': '0.0',
    VSZ: '463084',
    RSS: '980',
    TT: 's000',
    STAT: 'S',
    STARTED: '12Jul16',
    TIME: '0:01.02',
    COMMAND: '-bash' } ]
HEADERS:
[ { header: 'USER', start: 0, end: 3, ltr: true },
  { header: 'PID', start: 13, end: 17, ltr: false },
  { header: '%CPU', start: 20, end: 23, ltr: false },
  { header: '%MEM', start: 25, end: 28, ltr: false },
  { header: 'VSZ', start: 32, end: 37, ltr: false },
  { header: 'RSS', start: 41, end: 44, ltr: false },
  { header: 'TT', start: 46, end: 49, ltr: false },
  { header: 'STAT', start: 52, end: 55, ltr: true },
  { header: 'STARTED', start: 57, end: 63, ltr: false },
  { header: 'TIME', start: 67, end: 73, ltr: false },
  { header: 'COMMAND', start: 75, end: 80, ltr: true } ]
stderr: 

Command-lines containing spaces

Changing e.g. "ps u" to "ps aux" turns e.g.

USER              PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
warren.downs      432   0.4  2.2  2895776 181368   ??  S    11Jul16  43:30.62 /Applications/Google Drive.app/Contents/MacOS/Google Drive

into

  { USER: 'warren.downs',
    PID: '432',
    '%CPU': '0.4',
    '%MEM': '2.2',
    VSZ: '2895776',
    RSS: '181368',
    TT: '??',
    STAT: 'S',
    STARTED: '11Jul16',
    TIME: '43:30.62',
    COMMAND: '/Applications/Google Drive.app/Contents/MacOS/Google Drive' },

As long as the headers line up in a tabular manner, or the space-containing command line is the last column in the table, parsing should work fine.

How it works

Parsing basically replicates how a human would visually scan a whitespace-delimited table. The column headers give a starting point for the table columns, and scanning the entire table tells us whether a column is right or left aligned.

If the rightmost character in a row below a column header area is always non-blank, as well as the leftmost character, and the one to it's left, then the column is definitely right-aligned (see e.g. VSZ, RSS, and TIME, above).

If there's not enough data to tell, then it doesn't matter (see PID, TT). In the above example, %CPU and %MEM are technically right-aligned but this algorithm doesn't care unless the values expand to the left beyond the header's starting column.

If a column is right-aligned expanding to the left, the actual starting point of the left-most data of a column in any row, is used for all the data rows.