larswaechter / voici.js

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

Improve type system #4

Closed larswaechter closed 1 year ago

larswaechter commented 1 year ago

Improved the type system as mentioned in #2. Moreover there are some breaking API changes:

The GitBook documentation must be updated.

An example:

import { AccumulationFunction, Table } from '.';

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

interface IDynamicAttributes {
  fullname: string;
}

const table = new Table<(typeof data)[number], IDynamicAttributes>(data, {
  header: {
    dynamic: {
      fullname(row) {
        return row.firstname + ' ' + row.lastname;
      }
    }
  },
  body: {
    accumulation: {
      columns: {
        age: AccumulationFunction.SUM
      }
    }
  }
});
table.print();