esc-pad-table-array
v0.2.13
Published
Tiny 1.7K function pads the cells of a 2D array of strings (table) by column, left/right aligned, optionally formatted, optional headings.
Downloads
744
Maintainers
Readme
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));
Example with alignment
console.log(padTableA(inputA, { alignA:[ 'L', 'R' ] }));
console.log(padTableA(inputA, { align:'LR' })); // terse alternative syntax
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 ] }));
Example with heading, alignment and formatting
console.log(padTableA(inputA, { headingA:['LAN IP', 'HOSTNAME'], align:'LR', fmtA:[ colors.magenta, colors.blue ] }));
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 ],
}));
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);
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));
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,
}));
const whateverCharYouLike = '.';`
// `paddingChar:whateverCharYouLike, trim:false` also works with subheadings:
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