Socket
Socket
Sign inDemoInstall

csv-writer

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file


Version published
Weekly downloads
455K
decreased by-2.57%
Maintainers
1
Install size
25.3 kB
Created
Weekly downloads
 

Changelog

Source

[0.0.2] - 2016-10-15

Fixed

  • Fixed the bug that field values were not quoted when they have newline characters

Readme

Source

CSV Writer

Convert objects/arrays into a CSV string or write them into a file.

Prerequisite

  • Node version 4 or above

Usage

The example below shows how you can write records defined as the array of objects into a file.

const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
    path: 'path/to/file.csv',
    header: [
        {id: 'name', title: 'NAME'},
        {id: 'lang', title: 'LANGUAGE'},
    ]
});

const records = [
    {name: 'Bob',  lang: 'French, English'},
    {name: 'Mary', lang: 'English'}
];

csvWriter.writeRecords(records)       // returns a promise
    .then(() => {
        console.log('...Done');
    });

// This will produce a file path/to/file.csv with following contents:
//
//   NAME,LANGUAGE
//   Bob,"French, English"
//   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)

Promise.resolve()
    .then(() => csvWriter.writeRecords(records1))
    .then(() => csvWriter.writeRecords(records2))
    ...

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.

const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
    path: 'path/to/file.csv',
    header: ['name', 'lang']
});

If each record is defined as an array, use createArrayCsvWriter to get an csvWriter.

const createCsvWriter = require('csv-writer').createArrayCsvWriter;
const csvWriter = createCsvWriter({
    header: ['NAME', 'LANGUAGE'],
    path: 'path/to/file.csv'
});

var records = [
    ['Bob',  'French, English'],
    ['Mary', 'English']
];

csvWriter.writeRecords(records)       // returns a promise
    .then(() => {
        console.log('...Done');
    });

// This will produce a file path/to/file.csv with following contents:
//
//   NAME,LANGUAGE
//   Bob,"French, English"
//   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.

const createCsvStringifier = require('csv-writer').createObjectCsvStringifier;
const csvStringifier = createCsvStringifier({
    header: [
        {id: 'name', title: 'NAME'},
        {id: 'lang', title: 'LANGUAGE'},
    ]
});

const records = [
    {name: 'Bob',  lang: 'French, English'},
    {name: 'Mary', lang: 'English'}
];

console.log(csvStringifier.getHeaderString());
// => 'NAME,LANGUAGE\n'

console.log(csvStringifier.stringifyRecords(records));
// => 'Bob,"French, English"\nMary,English\n'

API

createObjectCsvWriter(params)

Parameters:
  • params <Object>
    • path <string>

      Path to a write file

    • header <Array<{id, title}|string>>

      Array of objects (id and title properties) or strings (field ids)

    • encoding <string> (optional)

Returns:
  • <CsvWriter>

createArrayCsvWriter(params)

Parameters:
  • params <Object>
    • path <string>

      Path to a write file

    • header <Array<string>> (optional)

      Array of field titles

    • encoding <string> (optional)

Returns:
  • <CsvWriter>

CsvWriter#writeRecords(records)

Parameters:
  • records <Array<Object|Array>>

    Depending on which function was used to create a csvWriter (i.e. createObjectCsvWriter or createArrayCsvWriter), records will be either an array of objects or arrays

Returns:
  • <Promise>

createObjectCsvStringifier(params)

Parameters:
  • params <Object>
    • header <Array<{id, title}|string>>

      Array of objects (id and title properties) or strings (field ids)

Returns:
  • <ObjectCsvStringifier>

ObjectCsvStringifier#getHeaderString()

Returns:
  • <string>

ObjectCsvStringifier#stringifyRecords(records)

Parameters:
  • records <Array<Object>>
Returns:
  • <string>

createArrayCsvStringifier(params)

Parameters:
  • params <Object>
    • header <Array<string>> (optional)

      Array of field titles

Returns:
  • <ArrayCsvStringifier>

ArrayCsvStringifier#getHeaderString()

Returns:
  • <string>

ArrayCsvStringifier#stringifyRecords(records)

Parameters:
  • records <Array<Array<string>>>
Returns:
  • <string>

Keywords

FAQs

Last updated on 15 Oct 2016

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc