1.0.1 • Published 4 years ago

csv-blink v1.0.1

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

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()
ParameterTypeDescription
headersarrayOptional. example ['a','b','c']
rowsarrayOptional. example [[1,2,3], [2,3,4]]

API

MethodsParametersDescription
.setHeaders(headers)arraySet the headers of a csv.
.addRow(row)arrayAdd a single row of values, example [[1,2,3], [4,5,6]].
.getRow(index)numberReturns the values of a row.
.addColumn(header, row)header: string, row: arrayAdd a header and a column of values of this header.
.getColumn(column)string \| numberReturns the values of a column, it search by index or header name.
.setCell(x, y, value)x: number, y:number, val: valueSets the value to specified x, y position.
.getCell(x, y)numberReturns the value of the x, y specified cell.
.toJson(csv) \| StaticstringReturns the csv passed to param as a json format, the headers of the csv are required.
GettersReturnsDescription
.filestringReturns the headers and rows as a csv string.
.jsonjsonReturns the csv values as json format.

Readme generated with