Tiny 1.7K function pads the cells of a 2D array of strings (table) by column, left/right aligned, optionally formatted, optional headings.
npm install esc-pad-table-arraynpm install esc-pad-table-arrayJavaScript
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' ],
];
`
$3
`javascript
console.log(padTableA(inputA));
`
!image$3
`javascript
console.log(padTableA(inputA, { alignA:[ 'L', 'R' ] }));
console.log(padTableA(inputA, { align:'LR' })); // terse alternative syntax
`
!image$3
npm install esc-colors
`javascript
import colors from 'esc-colors';
``javascript
console.log(padTableA(inputA, { align:'LR', fmtA:[ colors.magenta, colors.blue ] }));
`
!image$3
`javascript
console.log(padTableA(inputA, { headingA:['LAN IP', 'HOSTNAME'], align:'LR', fmtA:[ colors.magenta, colors.blue ] }));
`
!image$3
`javascript
console.log(padTableA(inputA, {
headingA: ['LAN IP', 'HOSTNAME'],
headingChar: null,
align: 'LR',
fmtA: [ colors.magenta, colors.blue ],
}));
`
!image$3
You can get a 1D or 2D array and do whatever you want with it, trivially.
`javascript
const tableAA = padTableA(inputA, { align:'LR', colDelim:null, rowDelim:null, paddingChar:'.' });console.log('tableAA', tableAA);
`
!image`javascript
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));
`
!imageGet the table data and you can flip it any style.
$3
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', '' ]`javascript
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`javascript
const whateverCharYouLike = '.';paddingChar:whateverCharYouLike, trim:false also works with subheadings:If I have ideas for something different, I'll make a new package rather than make breaking changes.