UNPKG

1.7 kBJavaScriptView Raw
1var Transform = require('readable-stream/transform')
2 , inherits = require('util').inherits
3 , xtend = require('xtend')
4
5
6// a noop _transform function
7function noop (chunk, enc, callback) {
8 callback(null, chunk)
9}
10
11
12// create a new export function, used by both the main export and
13// the .ctor export, contains common logic for dealing with arguments
14function through2 (construct) {
15 return function (options, transform, flush) {
16 if (typeof options == 'function') {
17 flush = transform
18 transform = options
19 options = {}
20 }
21
22 if (typeof transform != 'function')
23 transform = noop
24
25 if (typeof flush != 'function')
26 flush = null
27
28 return construct(options, transform, flush)
29 }
30}
31
32
33// main export, just make me a transform stream!
34module.exports = through2(function (options, transform, flush) {
35 var t2 = new Transform(options)
36
37 t2._transform = transform
38
39 if (flush)
40 t2._flush = flush
41
42 return t2
43})
44
45
46// make me a reusable prototype that I can `new`, or implicitly `new`
47// with a constructor call
48module.exports.ctor = through2(function (options, transform, flush) {
49 function Through2 (override) {
50 if (!(this instanceof Through2))
51 return new Through2(override)
52
53 this.options = xtend(options, override)
54
55 Transform.call(this, this.options)
56 }
57
58 inherits(Through2, Transform)
59
60 Through2.prototype._transform = transform
61
62 if (flush)
63 Through2.prototype._flush = flush
64
65 return Through2
66})
67
68
69module.exports.obj = through2(function (options, transform, flush) {
70 var t2 = new Transform(xtend({ objectMode: true }, options))
71
72 t2._transform = transform
73
74 if (flush)
75 t2._flush = flush
76
77 return t2
78})