gajus / table

Formats data into a string table.
Other
894 stars 77 forks source link

Allow access to header.content #205

Open PenguRinPrivate opened 2 years ago

PenguRinPrivate commented 2 years ago
export declare type HeaderUserConfig = Omit<ColumnUserConfig, 'verticalAlignment' | 'width'> & {
    readonly content: string;
};

Right now it's on readonly mode if you assign the TableUserConfig to your config file, however, this prevents reusability if you want to change the content only for another table. Right now I have to either @ts-ignore the config file input if I want to change the content without assigning a type to it or I have to create a completely new config file.

nam-hle commented 2 years ago

Hi @PenguRin, how about reuse the table body content somehow? Btw, a sample code would help so much. Thanks.

PenguRinPrivate commented 2 years ago

Hey @nam-hle, reusing the content is not an option as they have different use cases but the same table layout. All that'd change would literally be the content in like 5 cases. To make the use case more clear with an example:

config.header.content = 'xyz'
console.log(table(commandTable, config));

This is the desired outcome without resolving to @ts-ignore or creating multiple configuration files.

Thank you!

nam-hle commented 2 years ago

Hey @PenguRin, thanks for clarification. What if we have a function like this:

function createTable(title: string, data: string[][]) {
  const config: TableUserConfig = {
    columnDefault: {
      width: 10,
    },
    header: {
      alignment: 'center',
      content: title,
    },
  }

  return table(data, config)
}
PenguRinPrivate commented 2 years ago

Of course that works but as aforementioned, I basically don't see the point of factory functions or any hacky workarounds when we could simply access it, there should be no harm, right? Unless I am missing something fundamental here that justifies the readonly property.