csv-blink

A minimalist node based library for manage csv files.

Usage no npm install needed!

<script type="module">
  import csvBlink from 'https://cdn.skypack.dev/csv-blink';
</script>

README

CSV Blink

An easy an lightweight library to generate CSV strings and parse to json.

MIT License

codecov

GitHub top language

GitHub code size in bytes

Installation

Install csv-blink with npm

  npm install csv-blink

Usage

You can create a CSV with headers and rows as a parameters.

const CSV = require('csv-blink')

// Create an instance

const csv = new CSV(
  ['a','b','c'], 
  [
    [1,2,3],
    [4,5,6],
    [7,8,9]
  ])

To render the csv just use de file getter

csv.file

It returns a parsed string with new lines:

a,b,c
1,2,3
4,5,6
7,8,9

To get the csv as json just use de json getter

csv.json

It returns a parsed json with headers as a key:

Note: To use the json getter the headers are required

[
  { a: 1, b: 2, c: 3 },
  { a: 4, b: 5, c: 6 },
  { a: 7, b: 8, c: 9 }
]

Also can initialize only with headers:

const csv = new CSV(['a','b','c'])

or

const csv = new CSV()

csv.setHeaders(['a','b','c'])

Add each row

csv.addRow([1,2,3])
csv.addRow([4,5,6])
csv.addRow([7,8,9])

Get a row

csv.getRow(2)

Add a column

csv.addColumn('d', [10,11,12])

Get a column

You can get a column by index or header name

csv.getColumn('a')

or

csv.getColumn(3)

Set an individual cell

csv.setCell(1, 1, 10)

API Reference

Class constructor

  new CSV()
Parameter Type Description
headers array Optional. example ['a','b','c']
rows array Optional. example [[1,2,3], [2,3,4]]

API

Methods Parameters Description
.setHeaders(headers) array Set the headers of a csv.
.addRow(row) array Add a single row of values, example [[1,2,3], [4,5,6]].
.getRow(index) number Returns the values of a row.
.addColumn(header, row) header: string, row: array Add a header and a column of values of this header.
.getColumn(column) string \| number Returns the values of a column, it search by index or header name.
.setCell(x, y, value) x: number, y:number, val: value Sets the value to specified x, y position.
.getCell(x, y) number Returns the value of the x, y specified cell.
.toJson(csv) \| Static string Returns the csv passed to param as a json format, the headers of the csv are required.
Getters Returns Description
.file string Returns the headers and rows as a csv string.
.json json Returns the csv values as json format.

Readme generated with