npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tailored-apps/data2csv

v1.0.0

Published

Tailored data to character separated values.

Downloads

48

Readme

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.