CSV Stringify

node v10.24.1
version: 2.0.0
endpointsharetweet
Simple, synchronous CSV conversion of a data set, using CSV Stringify https://csv.js.org/stringify/. CSV parsing libraries are great for handling deceiptively awkward issues, such as commas internal to a field, which can affect numeric types that are formatted!
const csvStringifySync = require("csv-stringify/lib/sync") const numeral = require("numeral") const data = [ { "Strings": "A description, with a comma.", "Numbers": numeral(1234.34).format("0,0.00"), "Booleans": false, "Other": "omitted" } ] const opts = { cast: { "boolean": val => val.toString() }, columns: [ "Strings", "Numbers", "Booleans" ], header: true } const result = csvStringifySync(data, opts) result
Note the "Other" column is omitted, as it is not specified in the column array of the options.
const csvParseSync = require("csv-parse/lib/sync") const records = csvParseSync(result) records[0].indexOf("Other") < 0
You can consume this output from the endpoint.
module.exports.endpoint = (_, res) => res.end(result)
Loading…

no comments

    sign in to comment