├── README.md ├── example.js ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # serialize-stream 2 | 3 | Stream json, ndjson, or csv. 4 | 5 | This is useful if you have an API and want to stream different formats depending on a param. 6 | 7 | Uses [csv-write-stream](https://github.com/maxogden/csv-write-stream) and [JSONStream](https://github.com/dominictarr/JSONStream) under the hood. 8 | 9 | ## Example 10 | 11 | ```js 12 | 13 | var ss = require('serialize-stream') 14 | 15 | ss('csv') 16 | .on('data', console.log) 17 | .end({a: 1, b: 2, c: 3}) 18 | 19 | // a,b,c 20 | // 1,2,3 21 | 22 | ss('json') 23 | .on('data', console.log) 24 | .end({a: 1, b: 2, c: 3}) 25 | 26 | // [ 27 | // {"a":1,"b":2,"c":3} 28 | // ] 29 | 30 | ss('ndjson') 31 | .on('data', console.log) 32 | .end({a: 1, b: 2, c: 3}) 33 | 34 | // {"a":1,"b":2,"c":3} 35 | 36 | ``` 37 | 38 | ## API 39 | 40 | ### `ss(format, [opts])` 41 | 42 | * `format` is one of: `'csv'`, `'json'`, or `'ndjson'`. 43 | 44 | * `opts` defaults: 45 | 46 | ```js 47 | { 48 | compact: false, // csv: will remove undefined keys (like in json) 49 | flatten: false // csv: will flatten nested objects using `object2dot`[1] 50 | } 51 | ``` 52 | 53 | [1] [object2dot](https://github.com/jpiepkow/object2dot) 54 | 55 | ## License 56 | 57 | MIT 58 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var ss = require('./') 2 | 3 | ss('csv') 4 | .on('data', console.log) 5 | .end({a: 1, b: 2, c: 3}) 6 | 7 | // a,b,c 8 | // 1,2,3 9 | 10 | ss('json') 11 | .on('data', console.log) 12 | .end({a: 1, b: 2, c: 3}) 13 | 14 | // [ 15 | // {"a":1,"b":2,"c":3} 16 | // ] 17 | 18 | ss('ndjson') 19 | .on('data', console.log) 20 | .end({a: 1, b: 2, c: 3}) 21 | 22 | // {"a":1,"b":2,"c":3} 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var csv = require('csv-write-stream') 2 | var pumpify = require('pumpify').obj 3 | var through = require('through2').obj 4 | var JSONStream = require('JSONStream') 5 | var object2dot = require('object2dot') 6 | 7 | module.exports = function (format, opts) { 8 | opts = opts || {} 9 | 10 | if (format === 'csv') return csvStream(opts) 11 | if (format === 'json') return JSONStream.stringify() 12 | if (format === 'ndjson') return ndjsonStream() 13 | 14 | return JSONStream.stringify() 15 | } 16 | 17 | function ndjsonStream () { 18 | return through(function (item, enc, cb) { 19 | cb(null, JSON.stringify(item) + '\n') 20 | }) 21 | } 22 | 23 | function csvStream (opts) { 24 | var streams = [] 25 | if (opts.compact) streams.push(compactStream()) 26 | if (opts.flatten) streams.push(flattenStream()) 27 | streams.push(csv(opts)) 28 | 29 | if (streams.length === 1) return streams[0] 30 | 31 | return pumpify.apply(pumpify, streams) 32 | } 33 | 34 | function flattenStream () { 35 | return through(function (item, enc, cb) { 36 | try { 37 | cb(null, object2dot.flatten(item)) 38 | } catch (err) { 39 | cb(err) 40 | } 41 | }) 42 | } 43 | 44 | function compactStream () { 45 | return through(function (item, enc, cb) { 46 | try { 47 | cb(null, JSON.parse(JSON.stringify(item))) 48 | } catch (err) { 49 | cb(err) 50 | } 51 | }) 52 | } 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serialize-stream", 3 | "version": "1.1.0", 4 | "description": "Stream as json, ndjson, or csv.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "David Guttman (http://davidguttman.com/)", 11 | "license": "MIT", 12 | "dependencies": { 13 | "JSONStream": "^1.1.4", 14 | "csv-write-stream": "^2.0.0", 15 | "object2dot": "^1.3.2", 16 | "pumpify": "^1.3.5", 17 | "through2": "^2.0.1" 18 | }, 19 | "devDependencies": {}, 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/davidguttman/serialize-stream.git" 23 | }, 24 | "bugs": { 25 | "url": "https://github.com/davidguttman/serialize-stream/issues" 26 | }, 27 | "homepage": "https://github.com/davidguttman/serialize-stream#readme" 28 | } 29 | --------------------------------------------------------------------------------