Coder Social home page Coder Social logo

parse-columns's Introduction

parse-columns

Parse text columns, like the output of Unix commands

Install

npm install parse-columns

Usage

$ df -kP
Filesystem 1024-blocks      Used Available Capacity  Mounted on
/dev/disk1   487350400 467871060  19223340    97%    /
devfs              185       185         0   100%    /dev
map -hosts           0         0         0   100%    /net
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
import parseColumns from 'parse-columns';

const execFileP = promisify(childProcess.execFile);

const {stdout} = await execFileP('df', ['-kP']);

console.log(parseColumns(stdout, {
	transform: (item, header, columnIndex) => {
		// Coerce elements in column index 1 to 3 to a number
		if (columnIndex >= 1 && columnIndex <= 3) {
			return Number(item);
		}

		return item;
	}
}));
/*
[
	{
		Filesystem: '/dev/disk1',
		'1024-blocks': 487350400,
		Used: 467528020,
		Available: 19566380,
		Capacity: '96%',
		'Mounted on': '/'
	},

]
*/

API

parseColumns(textColumns, options?)

textColumns

Type: string

The text columns to parse.

options

Type: object

separator

Type: string Default: ' '

Separator to split columns on.

headers

Type: string[]

Headers to use instead of the existing ones.

transform

Type: Function

Transform elements.

Useful for being able to cleanup or change the type of elements.

The supplied function gets the following arguments and is expected to return the element:

  • element (string)
  • header (string)
  • columnIndex (number)
  • rowIndex (number)

Related

parse-columns's People

Contributors

bendingbender avatar dbkaplun avatar eush77 avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

parse-columns's Issues

Separators in values

Input:

$ ps -p 29920 -o cmd,pid,start
CMD                           PID  STARTED
emacs -nw .                 29920   Oct 16

Output:

[ { CMD: 'emacs', '': '.', PID: '29920', STARTED: 'Oct 16' } ]

Expected output:

[ { CMD: 'emacs -nw .', PID: '29920', STARTED: 'Oct 16' } ]

Apparently the algorithm is not working in this case (column split is wrong, and also -nw is missing), I wonder if there is a simple fix.


One idea is to split not based on vertically aligned separators (as it may be just a coincidence), but instead on header positions. Say, for the example above we could take an indexOf for each header:

header index
CMD 0
PID 30
STARTED 35

And then for each line we could adjust these positions like this:

while (!reSeparator.test(line[pos - 1]) {
  pos -= 1;
}

And then just slice that row and trim the values.

This approach, if implemented, would parse the example above and pass the tests (I think), but it would fail in other cases where header is right-aligned, and the value contains separators, such as:

        CMD                   PID  STARTED
emacs -nw .                 29920   Oct 16

However, I didn't manage to come up with an example using ps(1). Maybe you know tools that output data in such format (apart from Node apps using text-table and right align — but these hopefully provide an API and you wouldn't parse their output anyway).

Another trap is headers containing separators (such as Mounted on from the readme), but we could use the existing algorithm to detect such headers (and this is in fact all we can do).

Manage overflow

Overflow of a value in a cell is not managed
When a value length is larger than the header, the row is offset.
then parsing is messy.

NAME          PID    
short_name    1234
long_very_very_long_name  1235
short_name2   1236

Thanks

Typings for typescript

What do you think about adding some typings for typescript developers?

(They are type annotations, which provide better type checking if you use typescript)

I've made a fork of your repo https://github.com/int0h/parse-columns and can create a pull request to yours if you are interested.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.