Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tailored-apps/data2csv

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tailored-apps/data2csv

Tailored data to character separated values.

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

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
indexString[x]The 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
mapMap[x]Array of map objects
separatorString[x]The value with which the tables should be separated
enclosureString[x]The value with which a set of data should be enclosed with
endOfLineString[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.

NameTypeMandatoryDefaultDescription
dataArray[x]Data list, which should be mapped
mapMap[x]Array 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.

Keywords

FAQs

Package last updated on 15 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc