funky-csv

0.1.2 • Public • Published

Funky CSV

Make & Read CSV files without loosing your coding rhythm

Installation

$ npm i funky-csv

Create CSV Files

Backend

const FunkyCSV = require('funky-csv/node');

const csv = new FunkyCSV;

csv.setHeader([
    'Column Title 1',
    'Column Title 2',
]);

csv.setContent([
    {
        val1: 'Value column 1 row 1',
        val2: 'Value column 2 row 1',
    },
    {
        val1: 'Value column 1 row 2',
        val2: 'Value column 2 row 2',
    },
]);

csv.write().then(() => console.log('output.csv successfully created!'));

Frontend

import FunkyCSV from 'funky-csv/browser';

const csv = new FunkyCSV;

csv.setHeader(...);
csv.setContent(...);

csv.download().then(() => console.log('output.csv successfully downloaded!'));

Read and parse CSV files

Backend

import FunkyCSVReader from 'funky-csv/node-reader';

const csv = new FunkyCSVReader;
csv.read('path/to/filename.csv').then(console.log) // [{col1: field1, col2: field2}]

Frontend

import FunkyCSVReader from 'funky-csv/browser-reader';

const csv = new FunkyCSVReader;
const csvString = '"col name 1","field1"\n"col name 2","field2"\n'
csv.parse(csvString).then(console.log) // [{colName1: field1, colName2: field2}]

🪄 Column names are automatically converted to camelCase style

Custom options

Example:

const csv = new FunkyCSV({
    filename: 'custom_filename.csv',
    delimiter: ';',
    closure: '"',
    ...
});
Option Type Default Writer Reader Description
filename string output.csv Output file name
delimiter string , Column delimiter
closure string " Closure character for string delimiter
headerRow (*) number 0 Row number of header location (where to start reading)
newLine string \n New line ascii character
parseNumbers boolean false Parse string numbers to number type

Custom Header on file reading

Setting headerRow as -1 the parser returns an array of arrays representing each row without header instead of an array of objects with key-value pair. Example:

// If we don't specify headerRow
[
  ['field1', 'field2'], // row 0
  ['field3', 'field4'], // row 1
  ...                   // next rows...
]

As an alternative we can set your own custom header. Example:

const csvReader = new FunkyCSVReader;
csvReader.setHeader([
  'column title 1',
  'column title 2',
  ...
])
csvReader.read('filename.csv').then(console.log) // [{columnName1: 'field1', columnName2: 'field2'}]

Extras

Setting filename on write & download method

// nodejs
csv.write('custom_filename');

// browser
csv.download('custom_filename');

🪄 You can omit .csv extension, Funky CSV will automatically add it.

Package Sidebar

Install

npm i funky-csv

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

54.2 kB

Total Files

29

Last publish

Collaborators

  • chechoforclaz