Gathering detailed insights and metrics for csv-writer
Gathering detailed insights and metrics for csv-writer
Gathering detailed insights and metrics for csv-writer
Gathering detailed insights and metrics for csv-writer
Convert objects/arrays into a CSV string or write them into a CSV file
npm install csv-writer
Typescript
Module System
Node Version
NPM Version
98.4
Supply Chain
100
Quality
75.9
Maintenance
100
Vulnerability
100
License
TypeScript (93.91%)
JavaScript (4.74%)
Shell (1.35%)
Total Downloads
62,959,055
Last Day
67,973
Last Week
472,437
Last Month
2,693,354
Last Year
26,762,652
248 Stars
192 Commits
39 Forks
4 Watching
1 Branches
6 Contributors
Minified
Minified + Gzipped
Latest Version
1.6.0
Package Id
csv-writer@1.6.0
Size
23.23 kB
NPM Version
6.11.3
Node Version
10.17.0
Publised On
18 Jan 2020
Cumulative downloads
Total Downloads
Last day
-43.2%
67,973
Compared to previous day
Last week
-31.1%
472,437
Compared to previous week
Last month
-1.3%
2,693,354
Compared to previous month
Last year
75.6%
26,762,652
Compared to previous year
Convert objects/arrays into a CSV string or write them into a file. It respects RFC 4180 for the output CSV format.
The example below shows how you can write records defined as the array of objects into a file.
1const createCsvWriter = require('csv-writer').createObjectCsvWriter; 2const csvWriter = createCsvWriter({ 3 path: 'path/to/file.csv', 4 header: [ 5 {id: 'name', title: 'NAME'}, 6 {id: 'lang', title: 'LANGUAGE'} 7 ] 8}); 9 10const records = [ 11 {name: 'Bob', lang: 'French, English'}, 12 {name: 'Mary', lang: 'English'} 13]; 14 15csvWriter.writeRecords(records) // returns a promise 16 .then(() => { 17 console.log('...Done'); 18 }); 19 20// This will produce a file path/to/file.csv with following contents: 21// 22// NAME,LANGUAGE 23// Bob,"French, English" 24// Mary,English
You can keep writing records into the same file by calling writeRecords
multiple times
(but need to wait for the fulfillment of the promise
of the previous writeRecords
call).
1// In an `async` function
2await csvWriter.writeRecords(records1)
3await csvWriter.writeRecords(records2)
4...
However, if you need to keep writing large data to a certain file, you would want to create
node's transform stream and use CsvStringifier
, which is explained later, inside it
, and pipe the stream into a file write stream.
If you don't want to write a header line, don't give title
to header elements and just give field IDs as a string.
1const createCsvWriter = require('csv-writer').createObjectCsvWriter; 2const csvWriter = createCsvWriter({ 3 path: 'path/to/file.csv', 4 header: ['name', 'lang'] 5});
If each record is defined as an array, use createArrayCsvWriter
to get an csvWriter
.
1const createCsvWriter = require('csv-writer').createArrayCsvWriter; 2const csvWriter = createCsvWriter({ 3 header: ['NAME', 'LANGUAGE'], 4 path: 'path/to/file.csv' 5}); 6 7const records = [ 8 ['Bob', 'French, English'], 9 ['Mary', 'English'] 10]; 11 12csvWriter.writeRecords(records) // returns a promise 13 .then(() => { 14 console.log('...Done'); 15 }); 16 17// This will produce a file path/to/file.csv with following contents: 18// 19// NAME,LANGUAGE 20// Bob,"French, English" 21// Mary,English
If you just want to get a CSV string but don't want to write into a file,
you can use createObjectCsvStringifier
(or createArrayCsvStringifier
)
to get an csvStringifier
.
1const createCsvStringifier = require('csv-writer').createObjectCsvStringifier; 2const csvStringifier = createCsvStringifier({ 3 header: [ 4 {id: 'name', title: 'NAME'}, 5 {id: 'lang', title: 'LANGUAGE'} 6 ] 7}); 8 9const records = [ 10 {name: 'Bob', lang: 'French, English'}, 11 {name: 'Mary', lang: 'English'} 12]; 13 14console.log(csvStringifier.getHeaderString()); 15// => 'NAME,LANGUAGE\n' 16 17console.log(csvStringifier.stringifyRecords(records)); 18// => 'Bob,"French, English"\nMary,English\n'
<Object>
path <string>
Path to a write file
header <Array<{id, title}|string>>
Array of objects (id
and title
properties) or strings (field IDs).
A header line will be written to the file only if given as an array of objects.
fieldDelimiter <string>
(optional)
Default: ,
. Only either comma ,
or semicolon ;
is allowed.
recordDelimiter <string>
(optional)
Default: \n
. Only either LF (\n
) or CRLF (\r\n
) is allowed.
headerIdDelimiter <string>
(optional)
Default: undefined
. Give this value to specify a path to a value in a nested object.
alwaysQuote <boolean>
(optional)
Default: false
. Set it to true
to double-quote all fields regardless of their values.
encoding <string>
(optional)
Default: utf8
.
append <boolean>
(optional)
Default: false
. When true
, it will append CSV records to the specified file.
If the file doesn't exist, it will create one.
NOTE: A header line will not be written to the file if true
is given.
<CsvWriter>
<Object>
path <string>
Path to a write file
header <Array<string>>
(optional)
Array of field titles
fieldDelimiter <string>
(optional)
Default: ,
. Only either comma ,
or semicolon ;
is allowed.
recordDelimiter <string>
(optional)
Default: \n
. Only either LF (\n
) or CRLF (\r\n
) is allowed.
alwaysQuote <boolean>
(optional)
Default: false
. Set it to true
to double-quote all fields regardless of their values.
encoding <string>
(optional)
Default: utf8
.
append <boolean>
(optional)
Default: false
. When true
, it will append CSV records to the specified file.
If the file doesn't exist, it will create one.
NOTE: A header line will not be written to the file if true
is given.
<CsvWriter>
records <Iterator<Object|Array>>
Depending on which function was used to create a csvWriter
(i.e. createObjectCsvWriter
or createArrayCsvWriter
),
records will be either a collection of objects or arrays. As long as the collection is iterable, it doesn't need to be an array.
<Promise>
<Object>
header <Array<{id, title}|string>>
Array of objects (id
and title
properties) or strings (field IDs)
fieldDelimiter <string>
(optional)
Default: ,
. Only either comma ,
or semicolon ;
is allowed.
recordDelimiter <string>
(optional)
Default: \n
. Only either LF (\n
) or CRLF (\r\n
) is allowed.
headerIdDelimiter <string>
(optional)
Default: undefined
. Give this value to specify a path to a value in a nested object.
alwaysQuote <boolean>
(optional)
Default: false
. Set it to true
to double-quote all fields regardless of their values.
<ObjectCsvStringifier>
<string>
<Array<Object>>
<string>
<Object>
header <Array<string>>
(optional)
Array of field titles
fieldDelimiter <string>
(optional)
Default: ,
. Only either comma ,
or semicolon ;
is allowed.
recordDelimiter <string>
(optional)
Default: \n
. Only either LF (\n
) or CRLF (\r\n
) is allowed.
alwaysQuote <boolean>
(optional)
Default: false
. Set it to true
to double-quote all fields regardless of their values.
<ArrayCsvStringifier>
<string>
<Array<Array<string>>>
<string>
Feature requests and bug reports are very welcome: https://github.com/ryu1kn/csv-writer/issues
A couple of requests from me when you raise an issue on GitHub.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
6 existing vulnerabilities detected
Details
Reason
Found 1/12 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More