|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const through2 = require('through2'); |
4 | | - |
5 | | - |
6 | | -module.exports = function(options) { |
7 | | - const opts = options || {}; |
8 | | - const countBy = opts.countBy || 1; |
9 | | - const expanding = opts.expandArray || false; |
10 | | - |
11 | | - let items = []; |
12 | | - |
13 | | - const stream = through2({ objectMode: true }, |
14 | | - function(chunk, enc, cb) { |
15 | | - try { |
16 | | - const data = JSON.parse(chunk); |
17 | | - if (expanding && (data instanceof Array)) { |
18 | | - items = items.concat(data); |
19 | | - } else { |
20 | | - items.push(data); |
21 | | - } |
22 | | - } catch (err) { |
23 | | - cb(err, null); |
24 | | - return; |
25 | | - } |
26 | | - |
27 | | - while (items.length >= countBy) { |
28 | | - const enqItems = items.splice(0, countBy); |
29 | | - this.push(countBy > 1 ? enqItems : enqItems[0]); |
30 | | - } |
31 | | - cb(); |
32 | | - }, function(cb) { |
33 | | - if (items.length) { |
34 | | - this.push(items); |
35 | | - } |
36 | | - cb(); |
37 | | - }); |
38 | | - |
39 | | - return stream; |
| 3 | +const Transform = require('stream').Transform; |
| 4 | +const util = require('util'); |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * JSONTransform |
| 9 | + * |
| 10 | + * @param {Object} opts - Transform options |
| 11 | + * @param {Number} opts.countBy - number of block items |
| 12 | + * @param {Boolean} opts.expandArray - Flatten an array into items |
| 13 | + */ |
| 14 | +function JSONTransform(opts) { |
| 15 | + if (!(this instanceof JSONTransform)) |
| 16 | + return new JSONTransform(opts); |
| 17 | + |
| 18 | + const opts_ = opts || {}; |
| 19 | + this._countBy = opts_.countBy || 1; |
| 20 | + this._expanding = opts_.expandArray || false; |
| 21 | + delete opts_.countBy; |
| 22 | + delete opts_.expandArray; |
| 23 | + |
| 24 | + this._items = []; |
| 25 | + |
| 26 | + opts_.objectMode = true; |
| 27 | + Transform.call(this, opts_); |
40 | 28 | } |
| 29 | +util.inherits(JSONTransform, Transform); |
| 30 | + |
| 31 | +JSONTransform.prototype._transform = function(chunk, encoding, cb) { |
| 32 | + try { |
| 33 | + const data = JSON.parse(chunk); |
| 34 | + if (this._expanding && (data instanceof Array)) { |
| 35 | + this._items = this._items.concat(data); |
| 36 | + } else { |
| 37 | + this._items.push(data); |
| 38 | + } |
| 39 | + } catch (err) { |
| 40 | + cb(err, null); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + while (this._items.length >= this._countBy) { |
| 45 | + if (!this._enqueue(this._shiftItems())) break; |
| 46 | + } |
| 47 | + cb(); |
| 48 | +} |
| 49 | + |
| 50 | +JSONTransform.prototype._flush = function(cb) { |
| 51 | + const items = this._items; |
| 52 | + |
| 53 | + while (items.length >= this._countBy) { |
| 54 | + this._enqueue(this._shiftItems()); |
| 55 | + } |
| 56 | + |
| 57 | + if (items.length) this._enqueue(items); |
| 58 | + cb(); |
| 59 | +} |
| 60 | + |
| 61 | +JSONTransform.prototype._enqueue = function(items) { |
| 62 | + return this.push(this._countBy > 1 ? items : items[0]); |
| 63 | +} |
| 64 | + |
| 65 | +JSONTransform.prototype._shiftItems = function() { |
| 66 | + return this._items.splice(0, this._countBy); |
| 67 | +} |
| 68 | + |
| 69 | +module.exports = JSONTransform; |
0 commit comments