UNPKG

jipe

Version:

Filter, extract from and pretty print a stream of JSON objects.

38 lines (30 loc) 930 B
var util = require('util'); var Transform = require('stream').Transform; util.inherits(JSONPrintStream, Transform); var debug = require('debug')('jipe.pp'); var _ = require('underscore'); function JSONPrintStream(options) { if (!(this instanceof JSONPrintStream)) { return new JSONPrintStream(options); } var conf = _.extend({ indent: 4 }, options); Transform.call(this, options); this._writableState.objectMode = true; this._readableState.objectMode = false; this._indent = conf.indent; } JSONPrintStream.prototype._transform = function(obj, _, done) { if (obj !== JSONPrintStream.NULL) { debug('printing %j', obj); this.push(JSON.stringify(obj, null, this._indent) + '\n'); } else { debug('printing null'); this.push('null\n'); } done(); }; /* null placeholder since passing in null ends the stream */ JSONPrintStream.NULL = new Object(); module.exports = JSONPrintStream;