Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional callback to ColumnOptions to allow custom formatting at the column level #322

Open
RonnBlack opened this issue Feb 9, 2022 · 1 comment

Comments

@RonnBlack
Copy link

Summary

Add an optional callback function to the ColumnOptions object that can be used to perform custom formatting or additional processing of a specific columns.

Motivation

Of of the box csv-stringify only supports formatting by data type. This does not allow different formats for different types of columns. For example a column that contain dates as opposed to a column that is a date-time. The same situation occurs when you have a output that requires both numbers and currencies.

Alternative

I am currently constructing an array of functions that are applied in a ForEach() to the rows of the original input.

stringify(
          rows.map(row => {
            this.formatters?.forEach( fmt => fmt(row) );
            return row;
          }),
          {
            header: addHeader,
            columns: this.columns
          }
)

A working Sample is here:
https://stackblitz.com/edit/typescript-column-format-ks3cs3?file=report-utils/csv-utils.ts

Draft

stringify(
      testData,
      {
        header: true,
        columns: [
          {key:'strVal', header: 'String'},
          {key:'dVal', header: 'Date', (value) => { return value.LocaleDateString(); }},
          {key:'dtVal', header: 'Date Time', (value) => { return `${value.toLocaleDateString()} ${value.toLocaleTimeString()}` }},
          {key:'currVal', header: 'Currency', (value) => { return value.toFixed(2); }}
        ]
)

Additional context

Add any other context or screenshots about the feature request here.

@wdavidw
Copy link
Member

wdavidw commented Feb 10, 2022

It seems reasonable, I'll start looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants