Formatters allow you to control how different data types (like numbers or strings) are represented in the resulting CSV. They are configured by passing a formatters object within the options passed to a parser. The keys in the formatters object correspond to the data types (e.g., number, string), and the values are the formatter functions. You can use built-in formatters from @json2csv/formatters or provide your own custom formatter functions.
import { Parser } from '@json2csv/plainjs';
import { number as numberFormatter } from '@json2csv/formatters';
const opts = {
formatters: {
number: numberFormatter({ decimals: 3, separator: ',' }),
string: (val) => val.toUpperCase() // Example of a custom formatter
}
};
const parser = new Parser(opts);
const csv = parser.parse(myData);