1.0.0 • Published 5 years ago

@tailored-apps/data2csv v1.0.0

Weekly downloads
2
License
ISC
Repository
gitlab
Last release
5 years ago

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.

NameTypeMandatoryDescription
indexStringxThe index of the data field in the incoming object
nameStringAn optional header name for this field
lengthNumberA max length check, if the value is too long it will be trimmed
mandatoryBooleanMarks 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

NameTypeDefaultDescription
filePathString'test'The path for the generated file
fileNameString'test.csv'The name of the generated file
...optionsObject{}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.

NameTypeMandatoryDescription
mapMapxArray of map objects
separatorStringxThe value with which the tables should be separated
enclosureStringxThe value with which a set of data should be enclosed with
endOfLineStringxThe 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.

NameTypeMandatoryDefaultDescription
dataArrayxData list, which should be mapped
mapMapxArray of map objects
writeHeadersBooleantrueIf the header information should be added as first line
shouldPadBooleanfalseIf the values should be padded to the defined max length, number with '0' and other types with ' '
propertyToLogNumber0Defines the property that should be logged if an error occurs (Default value is the first element)
failedItemsArray[]Defines a failedItems array which failed items will be pushed to
separatorString','The value with which the tables should be separated
enclosureString'"'The value with which a set of data should be enclosed with
endOfLineString'\n'The value with which a row should be ended
loggerObjectconsoleA logger instance passed to the exporter
presenterFnFunctiontextPresenterThe presenter which outputs the parsed CSV-String
...otherObject{}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.

1.0.0

5 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.2

6 years ago

0.7.0

6 years ago