@tailored-apps/data2csv

1.0.0 • Public • Published

data2csv

js-standard-style

data2csv is a generic data to custom separated value exporter library.

Installation

npm install @tailored-apps/data2csv

Types

Map

The map is always an array of map objects.

Name Type Mandatory Description
index String [x] The index of the data field in the incoming object
name String An optional header name for this field
length Number A max length check, if the value is too long it will be trimmed
mandatory Boolean Marks this field as required

Presenters

presenters.textPresenter

Joins the whole CSV-String on one variable and return the whole prepared string at once.

Example:

const { createData, presenters } = require('@tailored-apps/data2csv')
 ...
const dataString = await createData({ data, map, presenterFn: presenters.textPresenter })

presenters.filePresenter

Writes line per line of the CSV-String into the configured file.

filePath = 'test', fileName = 'test.csv', ...options

Name Type Default Description
filePath String 'test' The path for the generated file
fileName String 'test.csv' The name of the generated file
...options Object {} These options will be forwarded to fs.createWriteStream

Example:

const { createData, presenters } = require('@tailored-apps/data2csv')
 ...
const dataString = await createData({ data, map, presenterFn: presenters.textPresenter, fileName: 'file.csv', flags: 'a' })

Note:

The flags option will be forwarded to fs.createWriteStream

Usage

getHeader

Returns the header as CSV-String and is used in createData if writeHeaders is set to true.

Name Type Mandatory Description
map Map [x] Array of map objects
separator String [x] The value with which the tables should be separated
enclosure String [x] The value with which a set of data should be enclosed with
endOfLine String [x] The value with which a row should be ended

Example

const { getHeader } = require(('@tailored-apps/data2csv')

const map = [{
  index: 'firstname',
  name: 'Firstname'
},{
  index: 'lastname',
  name: 'Lastname'
}]

const headerString = getHeader({ map, separator: ',', enclosure: '"', endOfLine: '\n' })

Returns:

"Firstname","Lastname",\n

createData

Returns the full CSV-String of the incoming data list.

Name Type Mandatory Default Description
data Array [x] Data list, which should be mapped
map Map [x] Array of map objects
writeHeaders Boolean true If the header information should be added as first line
shouldPad Boolean false If the values should be padded to the defined max length, number with '0' and other types with ' '
propertyToLog Number 0 Defines the property that should be logged if an error occurs (Default value is the first element)
failedItems Array [] Defines a failedItems array which failed items will be pushed to
separator String ',' The value with which the tables should be separated
enclosure String '"' The value with which a set of data should be enclosed with
endOfLine String '\n' The value with which a row should be ended
logger Object console A logger instance passed to the exporter
presenterFn Function textPresenter The presenter which outputs the parsed CSV-String
...other Object {} Optional presenter function options

Example:

const { createData } = require('@tailored-apps/data2csv')

const map = [{
    index: 'id',
    length: 10 
  }, {
    index: 'firstname',
    length: 8
  }, {
    index: 'lastname',
    length: 10
  }]
const data = [{
  id: 'abc-def-gh',
  firstname: 'TestToLong',
  lastname: 'someName',
  middlename: 'somerndmiddlename'
}]
  
const dataString = await createData({ data, map, writeHeaders: false, separator: '\t', enclosure: '' })

Returns:

abc-def-gh\tTestToLo\tsomeName\t\n

Note:

The value middlename that is not given in the map will be excluded in the returned string The value firstname will be trimmed to the given length of 8 The created data string is sorted by the map indices

createDataText

Is an alias for createData which preset the textPresenter.

createDataFile

Is an alias for createData which preset the filePresenter.

Readme

Keywords

Package Sidebar

Install

npm i @tailored-apps/data2csv

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

143 kB

Total Files

22

Last publish

Collaborators

  • dabls
  • tailoredapps