世祺 0423d5801c Initial commit (by Create-Cloudflare CLI) | 1 year ago | |
---|---|---|
.. | ||
build | 1 year ago | |
.babelrc | 1 year ago | |
.eslintrc | 1 year ago | |
.travis.yml | 1 year ago | |
CHANGELOG.md | 1 year ago | |
LICENSE | 1 year ago | |
README.md | 1 year ago | |
as-table.d.ts | 1 year ago | |
as-table.js | 1 year ago | |
package.json | 1 year ago | |
test.js | 1 year ago |
A simple function that print objects and arrays as ASCII tables. Supports ANSI styling and weird 💩 Unicode emoji symbols (they won't break the layout), thanks to printable-characters
.
npm install as-table
asTable = require ('as-table')
asTable ([ { foo: true, string: 'abcde', num: 42 },
{ foo: false, string: 'qwertyuiop', num: 43 },
{ string: null, num: 44 } ])
foo string num
----------------------
true abcde 42
false qwertyuiop 43
null 44
asTable ([['qwe', '123456789', 'zxcvbnm'],
['qwerty', '12', 'zxcvb'],
['qwertyiop', '1234567', 'z']])
qwe 123456789 zxcvbnm
qwerty 12 zxcvb
qwertyiop 1234567 z
asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (data)
qwe | 1234… | zxc…
qwer… | 12 | zxc…
qwer… | 1234… | z
asTable.configure ({ right: true }) (data)
foo bar baz
-----------------------------
qwe 123456789 zxcvbnm
qwerty 12 zxcvb
qwertyiop 1234567 z
asTable.configure ({ print: x => (typeof x === 'boolean') ? (x ? 'yes' : 'no') : String (x) }) (data)
foo string num
--------------------
yes abcde 42
no qwertyuiop 43
null 44
The callback also receives a field name (in case of objects) or a column index (in case of arrays):
asTable = require ('as-table').configure ({
print (x, k) {
if (k === 'timestamp') return new Date (x).toGMTString()
return String (x)
}
})
asTable ([ { name: 'A', timestamp: 1561202591572 },
{ name: 'B', timestamp: 1558524240034 } ])
asTable = require ('as-table').configure ({ maxTotalWidth: 25, delimiter: ' | ' })
asTable (data)
With string coloring by ansicolor
(just for the demo purposes, any library will fit):
asTable = require ('as-table').configure ({ title: x => x.bright, delimiter: ' | '.dim.cyan, dash: '-'.bright.cyan })
console.log (
asTable ([ { foo: true, string: 'abcde', num: 42 },
{ foo: false, string: 'qwertyuiop'.bgMagenta.green.bright, num: 43 } ])