adaltas / adaltas/node-csv

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

Open
#322 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
4.3k
Forks
299
Avg merge
16h 19m
Merged PRs (30d)
1

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the ColumnOptions definition and the stringify entry point used by the columns option. Review the StackBlitz example and existing column-formatting behavior, then determine the callback API and add coverage showing per-column formatting for dates and currencies.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.