npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

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));

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