In NodeJs programs, printing structured arrays to the console can be annoying. tablify fulfills your greatest desires.
It can generate a pretty table out of
- an array of arrays
- an array of dictionaries; this is perhaps the most common thanks to (no)SQL
- a single dictionary, with each key/value pair getting a nice row
- data with or without headers
For example, here's how tablify handles an array of arrays:
tablify=require('tablify').tablifydata=[[1,2,3]["cat","dog",Math.PI]]console.logtablifydataOutput:
--------------------------------- | 1 | 2 | 3 | | cat | dog | 3.141592653589793 | --------------------------------- > npm install tablify If your structure has a header row, pass the optional "has_header" param:
data=[["name","age"]["Chris",10]["Max",8]]console.logtablifydata,{has_header: true}Output:
--------------- | name | age | --------------- | Chris | 10 | | Max | 8 | --------------- Even with inconsistent keys, you can print an array of dictionaries. Column headers are calculated automatically using the union of all keys.
data=[{name: "Chris",age: 16,gender: "M"}{name: "Max",age: 12,gender: "M"}{name: "Sam",gender: "F",colors: ["Orange","Blue"]}]console.logtablifydataOutput:
------------------------------------------------- | # | age | colors | gender | name | ------------------------------------------------- | 0 | 16 | | M | Chris | | 1 | 12 | | M | Max | | 2 | | ["Orange","Blue"] | F | Sam | ------------------------------------------------- console.log tablify data,{keys: ["age","name"]} Output:
-------------------- | # | age | name | -------------------- | 0 | 16 | Chris | | 1 | 12 | Max | | 2 | | Sam | -------------------- If tablify is passed an object that's not an array, it will pivot to show keys in one column and values in another.
console.log tablify{"name": "Chris", "age": 25, "obj": [1,2,3,{"foo":"bar"}]} Output:
-------------------------------- | age | 25 | | name | Chris | | obj | [1,2,3,{"foo":"bar"}] | -------------------------------- Any subset of these can be passed as a second parameter to tablify, in a dictionary.
show_indexinclude a column showing the row number of each row. The default isfalseunless tablify is passed an array of dictionaries, in which case the default istruehas_headerinclude the first row as a header; this defaults tofalseunless passed an array of dicts, in which case the keys are used as a first row and this defaults totrue; if passing a single dictionary, this is ignoredkeyswhich columns to use, when tablifying an array of dictionaries; by default all keys are used in alphabetical orderrow_startdefault = '| 'row_enddefault = ' |'spacerdefault = ' | 'row_sep_chardefault = '-'