larswaechter / voici.js

A Node.js library for pretty printing your data on the terminal🎨
https://voici.larswaechter.dev
MIT License
337 stars 4 forks source link

Add "fillEmpty" config option #14

Closed larswaechter closed 1 year ago

larswaechter commented 1 year ago

An fillEmpty option might be useful for dynamically filling empty values.

An example

const data = [
  { firstname: 'Homer', lastname: 'Simpson', age: 39 },
  { firstname: 'Marge', lastname: '', age: 36 },
  { firstname: 'Bart', lastname: 'Simpson', age: 10 },
  { firstname: 'Lisa', lastname: undefined, age: 8 },
  { firstname: 'Maggie', lastname: null, age: 1 }
];

const table = new Table(data, {
  body: {
    fillEmpty: {
      lastname: (row) => 'Simpson'
    }
  }
});
table.print();

Output:

  firstname    lastname    age  
================================
  Homer        Simpson     39   
  Marge        Simpson     36   
  Bart         Simpson     10   
  Lisa         Simpson     8    
  Maggie       Simpson     1    

The method getDataCell should provide the filled value as well:

console.log(table.getDataCell(1, 'lastname')); // Simpson