Socket
Socket
Sign inDemoInstall

format-data

Package Overview
Dependencies
21
Maintainers
3
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    format-data

Format tabular data from object streams to different (standard) formats


Version published
Weekly downloads
110
increased by3.77%
Maintainers
3
Install size
755 kB
Created
Weekly downloads
 

Readme

Source

format-data

WindowsMac/Linux
Windows Build statusBuild Status

Module for formatting tabular data from object streams in different standard ways. Supporting csv, ndjson, json and sse. Install with npm install format-data.

Usage

formatData(format, [opts])

The opts object will be passed to the underlying serializing module (see below).

or just pass an options object with format as a key.

formatData(opts)

full control of prefix, separator, suffix

You can pass a custom prefix, separator, or suffix to the stream builder for csv and json cases.

Examples:

## default separator for csv is ','
> formatData('csv')
'a,b\n1,2\nhello,world\n'

## change separator to a tab
> formatData({format: 'csv', separator: '\t'})
'a\tb\n1\t2\nhello\tworld\n'

## default object style finishes with }]
> formatData({
  format: 'json',
  style: 'object'
})
{"rows":[{"a":1,"b":2},{"a":"hello","b":"world"}]}

## custom suffix
> formatData({
  format: 'json',
  style: 'object',
  suffix: ', "next": "/api/rows?page=2"}'
})
{"rows":[{"a":1,"b":2},{"a":"hello","b":"world"}], "next": "/api/rows?page=2"}

## suffix still added in cases of error
> formatData({
  format: 'json',
  style: 'object',
  suffix: ', "next": "/api/rows?page=2"}'
})
{"rows":[], "error":"Oh no!", "next": "/api/rows?page=2"}

format

Formats and the used modules:

  • 'csv' - require('csv-write-stream')(opts)
  • 'ndjson' - require('ndjson').stringify(opts)
  • json - new require('./json.js')(opts)
  • sse - require('ssejson').serialize(opts)

Keywords

FAQs

Last updated on 26 Jul 2015

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc