0.1.2 • Published 2 years ago

funky-csv v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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: '"',
    ...
});
OptionTypeDefaultWriterReaderDescription
filenamestringoutput.csvOutput file name
delimiterstring,Column delimiter
closurestring"Closure character for string delimiter
headerRow (*)number0Row number of header location (where to start reading)
newLinestring\nNew line ascii character
parseNumbersbooleanfalseParse 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.

0.1.2

2 years ago

0.0.9-1

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago