
Security News
CISA Kills Off RSS Feeds for KEVs and Cyber Alerts
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
csv-builder
Advanced tools
Easily encode complex JSON objects to CSV with CsvBuilder's schema-like API
Easily encode complex JSON objects to CSV with CsvBuilder's schema-like API.
const CsvBuilder = require('csv-builder')
const data = [
{
name: 'Foo Bar',
meta: {
active: true,
roles: [
'user',
'admin'
]
}
}
]
const builder = new CsvBuilder({
headers: ['Firstname', 'Lastname', 'Role 1', 'Role 2', 'Active'],
alias: {
'Role 1': 'meta.roles[0]',
'Role 2': 'meta.roles[1]',
'Active': 'meta.active'
}
})
.virtual('Firstname', user => user.name.split(' ')[0])
.virtual('Lastname', user => user.name.split(' ')[1])
/* Each of the following produces the following CSV contents:
"Firstname","Lastname","Role 1","Role 2","Active"
"Foo","Bar","user","admin","true"
*/
// (1) Create from a Stream of objects (like a database)
getObjectStream()
.pipe(builder.createTransformStream())
.pipe(fs.createWriteStream('output.csv'))
// (2) Create from an existing payload (`data` is an array of objects)
builder.createReadStream(data)
.pipe(fs.createWriteStream('output.csv'))
// (3) Roll your own
let csv = ''
csv += builder.getHeaders()
data.forEach(item => {
csv += builder.getRow(item)
})
fs.writeFileSync('output.csv', csv)
$ npm i -s csv-builder
# or
$ yarn add csv-builder
getHeaders()
and getRow(object)
methods respectively.headers
String|Array Space separated headers, or array of headers (required)delimiter
String The column delimiter. Default ','
terminator
String The row terminator. Default '\n'
quoted
Boolean Quote columns? Default true
alias
Object An object in the format of { "csv header": "object prop" }, object prop
will be aliased to csv header
. Default {}
createReadStream
(payload): Stream.ReadableCreates a readable stream and consumes the payload.
payload
Array<Object> Incoming data.createTransformStream
(): Stream.TransformCreates a transform stream. The stream expects either Objects or JSON.
headers
(headers): thisheaders
String|Array Space separated headers, or array of headersalias
(header, prop): thisSet single or multiple contraints. If header
is an object, it will extend any existing constraints, not replace.
header
String|Object Either object {"header": "property"} Or a string "Header"prop
String|undefined Property to correspond to header, omit if using object.virtual
(prop, fn): thisCreate a virtual property. Virtual properties are treated the same as normal properties. If there is no corresponding header or alias, the virtual will not be present in resulting CSV.
prop
String Virtual property namefn
(item: any) => any Where item
is an element from the incoming data, and the return value is the corresponding value for the virtualized property.getHeaders
(): StringThe headers in CSV format
getRow
(item): StringReturns the CSV formated row for a given item
.
item
Object A n item matching the "schema".constraints
attribute in options (for constructor) is deprecated, use alias
instead.set(prop, value)
method is deprecated, use alias(prop, value)
instead.FAQs
Easily encode complex JSON objects to CSV with CsvBuilder's schema-like API
The npm package csv-builder receives a total of 1,106 weekly downloads. As such, csv-builder popularity was classified as popular.
We found that csv-builder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.