All Projects → ryu1kn → Csv Writer

ryu1kn / Csv Writer

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

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Csv Writer

Jsonexport
{} → 📄 it's easy to convert JSON to CSV
Stars: ✭ 208 (+48.57%)
Mutual labels:  csv, npm-package
Create React Component Folder
Creates react component folder structure
Stars: ✭ 139 (-0.71%)
Mutual labels:  npm-package
Anytojson
Converts any data repository to JSON(or atleast strives to!). Currently converts flat-file JSON, flat-file CSV, REST JSON, REST CSV and Databases(via ODBC) to JSON 🔥.
Stars: ✭ 130 (-7.14%)
Mutual labels:  csv
Ag Psd
Javascript library for reading and writing PSD files
Stars: ✭ 135 (-3.57%)
Mutual labels:  npm-package
Ink Link
Link component for Ink
Stars: ✭ 133 (-5%)
Mutual labels:  npm-package
Elasticsearch Dataformat
Excel/CSV/BulkJSON downloads on Elasticsearch.
Stars: ✭ 135 (-3.57%)
Mutual labels:  csv
Replace In Files Cli
Replace matching strings and regexes in files
Stars: ✭ 129 (-7.86%)
Mutual labels:  npm-package
Texme
Self-rendering Markdown + LaTeX documents
Stars: ✭ 1,970 (+1307.14%)
Mutual labels:  npm-package
Defer.js
🥇 A super small, super efficient library that helps you lazy load everything like images, video, audio, iframe as well as stylesheets, and JavaScript.
Stars: ✭ 138 (-1.43%)
Mutual labels:  npm-package
Ngx Config
Configuration utility for Angular
Stars: ✭ 135 (-3.57%)
Mutual labels:  npm-package
Ohshitgit
⁉️Oh shit! A cli tool to help you unfuck your git mistakes
Stars: ✭ 135 (-3.57%)
Mutual labels:  npm-package
Node Csvtojson
Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.
Stars: ✭ 1,760 (+1157.14%)
Mutual labels:  csv
To Milliseconds
Convert an object of time properties to milliseconds: `{seconds: 2}` → `2000`
Stars: ✭ 136 (-2.86%)
Mutual labels:  npm-package
Dataclass Csv
Map CSV to Data Classes
Stars: ✭ 133 (-5%)
Mutual labels:  csv
Criterion
Microbenchmarking for Modern C++
Stars: ✭ 140 (+0%)
Mutual labels:  csv
Etl.net
Mass processing data with a complete ETL for .net developers
Stars: ✭ 129 (-7.86%)
Mutual labels:  csv
Dumpling
Dumpling is a fast, easy-to-use tool written by Go for dumping data from the database(MySQL, TiDB...) to local/cloud(S3, GCP...) in multifarious formats(SQL, CSV...).
Stars: ✭ 134 (-4.29%)
Mutual labels:  csv
Handlebars Webpack Plugin
Renders your html-template at build time
Stars: ✭ 135 (-3.57%)
Mutual labels:  npm-package
Alfred Vscode
Alfred 3 workflow that allows you to browse and open Visual Studio Code projects or simply open specified folders/files
Stars: ✭ 141 (+0.71%)
Mutual labels:  npm-package
Mdme
Self-rendering Markdown content
Stars: ✭ 140 (+0%)
Mutual labels:  npm-package

Build Status Coverage Status Code Climate

CSV Writer

Convert objects/arrays into a CSV string or write them into a file. It respects RFC 4180 for the output CSV format.

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).

// In an `async` function
await csvWriter.writeRecords(records1)
await csvWriter.writeRecords(records2)
...

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.

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'
});

const 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). 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.

Returns:
  • <CsvWriter>

createArrayCsvWriter(params)

Parameters:
  • params <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.

Returns:
  • <CsvWriter>

CsvWriter#writeRecords(records)

Parameters:
  • 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.

Returns:
  • <Promise>

createObjectCsvStringifier(params)

Parameters:
  • params <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.

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

    • 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.

Returns:
  • <ArrayCsvStringifier>

ArrayCsvStringifier#getHeaderString()

Returns:
  • <string>

ArrayCsvStringifier#stringifyRecords(records)

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

Request Features or Report Bugs

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.

  • Requesting a feature: Please try to provide the context of why you want the feature. Such as, in what situation the feature could help you and how, or how the lack of the feature is causing an inconvenience to you. I can't start thinking of introducing it until I understand how it helps you 🙂
  • Reporting a bug: If you could provide a runnable code snippet that reproduces the bug, it would be very helpful!

Development

Prerequisite

  • Node version 8 or above
  • Docker
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].