esc-pad-table-array
TypeScript icon, indicating that this package has built-in type declarations

0.2.13 • Public • Published

esc-pad-table-array

Tiny 1.7K function pads the cells of a 2D array of strings (table) by column, left/right aligned, optionally formatted, optional headings.

Install

npm install esc-pad-table-array

Example import and data

import padTableA from 'esc-pad-table-array';
const inputA = [
  [ '192.168.1.4', 'apple.example.org' ],
  [ '192.168.1.91', 'fig.example.org', 'fig.otherdomain.co.za' ],
  [ '192.168.1.123', 'watermelon.example.org', 'watermelon.otherdomain.co.za' ],
  [ '192.168.1.201', 'plum.example.org', 'plum.otherdomain.co.za' ],
];

Simplest example

console.log(padTableA(inputA));

image

Example with alignment

console.log(padTableA(inputA, { alignA:[ 'L', 'R' ] }));
console.log(padTableA(inputA, { align:'LR' }));          // terse alternative syntax

image

Example with alignment and formatting

npm install esc-colors

import colors from 'esc-colors';
console.log(padTableA(inputA, { align:'LR', fmtA:[ colors.magenta, colors.blue ] }));

image

Example with heading, alignment and formatting

console.log(padTableA(inputA, { headingA:['LAN IP', 'HOSTNAME'], align:'LR', fmtA:[ colors.magenta, colors.blue ] }));

image

Example with heading (no underline), alignment and formatting

console.log(padTableA(inputA, {
  headingA: ['LAN IP', 'HOSTNAME'],
  headingChar: null,
  align: 'LR',
  fmtA: [ colors.magenta, colors.blue ],
}));

image

Example getting 2D array

You can get a 1D or 2D array and do whatever you want with it, trivially.

const tableAA = padTableA(inputA, { align:'LR', colDelim:null, rowDelim:null, paddingChar:'.' });

console.log('tableAA', tableAA);

image

const rowsA = tableAA.map( colsA => '|..' + colsA.join('...') + '..|' );

const width = rowsA[0].length;

console.log('='.repeat(width));

rowsA.forEach( rowS => console.log(rowS) );

console.log('='.repeat(width));

image

Get the table data and you can flip it any style.

Example subheadings in your table

Rows with a single column are treated as subheadings, so they 'merge' the adjacent 'cells' and don't affect your other column widths.

If you have a row with a single data column and want it treated as a data row simply add another empty column next to it like [ 'MYDATA', '' ]

const inputA = [
  [ '# these are my fruity computers, make a nice smoothie' ],
  [ '192.168.1.4', 'papaya.example.org' ],
  [ '192.168.1.91', 'fig.example.org', 'fig.otherdomain.co.za' ],
  [ '192.168.1.123', 'watermelon.example.org', 'watermelon.otherdomain.co.za' ],
  [ '# cooked computers, serve hot' ],
  [ '192.168.1.201', 'pizza.example.org', 'pizza.otherdomain.co.za' ],
  [ '192.168.1.211', 'lasagna.example.org', 'lasagna.otherdomain.co.za' ],
  [ '# drinks' ],
  [ '192.168.1.214', 'mate.example.org', 'mate.otherdomain.co.za' ],
];

console.log(padTableA(inputA, {
  headingA: ['LAN IP', 'HOSTNAME'],
  align: 'LR',
  indent: 2,
  fmtA: [ colors.magenta, colors.blue ],
  fmtSubF: colors.yellow,
}));

image

const whateverCharYouLike = '.';`
// `paddingChar:whateverCharYouLike, trim:false` also works with subheadings:

image

Project goals

  • No dependencies
  • No nonsense
  • Reliable, no radical changes will ever happen.
  • Small, simple, easy to audit, yourself

If I have ideas for something different, I'll make a new package rather than make breaking changes.

Inspired by

I looked what else was available and it was massively bloated and over-complicated.

Notes

Have fun!

Say thanks

Star the repo https://github.com/softwarecreations/esc-pad-table-array

PR's or issues

Welcome

License

MIT

Package Sidebar

Install

npm i esc-pad-table-array

Weekly Downloads

2

Version

0.2.13

License

MIT

Unpacked Size

11.7 kB

Total Files

5

Last publish

Collaborators

  • softwarecreations