esc-pad-table-array
v0.2.0
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
84
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.
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