format-data
Windows | Mac/Linux |
---|---|
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
The opts
object will be passed to the underlying serializing module (see below).
or just pass an options object with format
as a key.
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 ','> 'a,b\n1,2\nhello,world\n' ## change separator to a tab> 'a\tb\n1\t2\nhello\tworld\n' ## default object style finishes with }> "rows":"a":1"b":2"a":"hello""b":"world" ## custom suffix> "rows":"a":1"b":2"a":"hello""b":"world" "next": "/api/rows?page=2" ## suffix still added in cases of error> "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)