Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@algonomia/convert-array-to-csv

aichbauer28MIT1.0.0

Convert an array to a csv formatted string

array, csv, list, convert, comma-separated, values, convert-array-to-csv

readme

convert-array-to-csv

npm Travis branch Codecov branch

Convert an array to a csv formatted string

Table of Contents

Why?

I needed a simple way to download the data from a table component in a csv format.

Installation

$ npm i convert-array-to-csv -S

or

$ yarn add convert-array-to-csv

Functions

Take a look into the usage section for a detailed example.

convertArrayToCSV

Note: you can also use the default export.

This function converts an array of objects, or an array of arrays into an csv formatted string.

Syntax

Returns a new string.

const csv = convertArrayToCSV(data, options);
Parameters
  • data: an array of arrays or an array of objects
  • options: a object
    • holds two keys: header and separator
    • header: and array with the name of the columns, default: undefined
    • separator: the character which is the separator in your csv formatted string, default: ','

Usage

An example how to use it.

const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');

const header = ['number', 'first', 'last', 'handle'];
const dataArrays = [
  [1, 'Mark', 'Otto', '@mdo'],
  [2, 'Jacob', 'Thornton', '@fat'],
  [3, 'Larry', 'the Bird', '@twitter'],
];
const dataObjects = [
  {
    number: 1,
    first: 'Mark',
    last: 'Otto',
    handle: '@mdo',
  },
  {
    number: 2,
    first: 'Jacob',
    last: 'Thornton',
    handle: '@fat',
  },
  {
    number: 3,
    first: 'Larry',
    last: 'the Bird',
    handle: '@twitter',
  },
];

/*
  const csvFromArrayOfObjects  = 'number,first,last,handle\n1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,the Bird,@twitter\n';
*/
const csvFromArrayOfObjects = convertArrayToCSV(dataObjects);

/*
  const csvFromArrayOfArrays  = 'number;first;last;handle\n1;Mark;Otto;@mdo\n2;Jacob;Thornton;@fat\n3;Larry;the Bird;@twitter\n';
*/
const csvFromArrayOfArrays = convertArrayToCSV(dataArrays, {
  header,
  separator: ';'
});

License

MIT © Lukas Aichbauer

changelog

2.0.0 - May, 17 2020

  • f5ebcde Fix: consider separator in checks and use double quotes correctly (closes #22) (Vano Devium)
  • fb85b50 CI: changed to 12x, 14x versions of Node.js for test pipeline (closes #31) (Vano Devium)

1.0.12 - May, 07 2020

  • edf27e8 Fix: incorrect check for false values (#30) (John Pariseau)

1.0.11 - April, 09 2020

  • 9dd684b Chore: update deps (#27) (Lukas Aichbauer)
  • 7ca8100 Refactor: Optimized helpers (#26) (Vano Devium)

1.0.10 - February, 14 2020

  • d95aa36 Chore: bump eslint-utils from 1.4.0 to 1.4.3 (#23) (dependabot[bot])
  • 53bb4e9 Chore: bump handlebars from 4.1.2 to 4.5.3 (#24) (dependabot[bot])

1.0.9 - August, 02 2019

  • 8ae2d19 Chore: switch yarn to npm (#21) (Lukas Aichbauer)

1.0.8 - May, 25 2019

  • bd592e5 Fix: zero (0) in header and body elements (#19) (Lukas Aichbauer)

1.0.7 - April, 25 2019

  • 17a7f6e Fix: handle numbers correct (#17) (closes #16) (Lukas Aichbauer)

1.0.6 - March, 29 2019

  • 6653259 Fix: replace every double quote (#15) (Lukas Aichbauer)

1.0.5 - February, 26 2019

  • 58ec063 Fix: double quotes inside elements (closes #9) (#12) (Lukas Aichbauer)
  • 2ea0633 Refactor: add append element and separator or line break functions (closes #8) (#11) (Lukas Aichbauer)
  • c237523 Fix: space as special char (closes #7) (#10) (Lukas Aichbauer)

1.0.4 - September, 07 2018

  • 2b92efa Chore: add prepush hook and changed prepublish to prepare (#6) (Lukas Aichbauer)

1.0.3 - September, 07 2018

  • 51a76a7 Fix: babel runtime (#5) (fixes #3) (Lukas Aichbauer)

1.0.2 - June, 22 2018

  • 4e024bd Fix : handle zero values correctly (array of arrays) (#4) (Kiwy404)

1.0.1 - May, 07 2018

  • 6e91d6d Fix: handle null and undefined values (#2) (Lukas Aichbauer)

1.0.0 - April, 05 2018

  • 48cd02c Fix: pass options from index to modules (#1) (Lukas Aichbauer)
  • ff5adcb Initial commit (aichbauer)