You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise**
6
8
7
9
Inspired by [Dominic Tarr](https://github.com/dominictarr)'s [through](https://github.com/dominictarr/through) in that it's so much easier to make a stream out of a function than it is to set up the prototype chain properly: `through(function (chunk) { ... })`.
8
10
9
-
***Note: Users of Node.js 0.10 and 0.12 should install `through2@2.x`. As of through2@3.x, readable-stream@3 is being used and is not compatible with older versions of Node.js.***_v2.x support is being maintained on the [v2.x](https://github.com/rvagg/through2/tree/v2.x) branch._
Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
52
52
53
+
## Do you need this?
54
+
55
+
Since Node.js introduced [Simplified Stream Construction](https://nodejs.org/api/stream.html#stream_simplified_construction), many uses of **through2** have become redundant. Consider whether you really need to use **through2** or just want to use the core `'stream'` package (or ideally the `'readable-stream'` package from npm):
Instead of returning a `stream.Transform` instance, `through2.ctor()` returns a **constructor** for a custom Transform. This is useful when you want to use the same transform logic in multiple instances.
105
119
106
120
```js
107
-
var FToC =through2.ctor({objectMode:true}, function (record, encoding, callback) {
121
+
constFToC=through2.ctor({objectMode:true}, function (record, encoding, callback) {
108
122
if (record.temp!=null&&record.unit=="F") {
109
123
record.temp= ( ( record.temp-32 ) *5 ) /9
110
124
record.unit="C"
@@ -114,21 +128,13 @@ var FToC = through2.ctor({objectMode: true}, function (record, encoding, callbac
114
128
})
115
129
116
130
// Create instances of FToC like so:
117
-
var converter=newFToC()
131
+
constconverter=newFToC()
118
132
// Or:
119
-
var converter =FToC()
133
+
constconverter=FToC()
120
134
// Or specify/override options when you instantiate, if you prefer:
121
-
var converter =FToC({objectMode:true})
135
+
constconverter=FToC({objectMode:true})
122
136
```
123
137
124
-
## See Also
125
-
126
-
-[through2-map](https://github.com/brycebaril/through2-map) - Array.prototype.map analog for streams.
127
-
-[through2-filter](https://github.com/brycebaril/through2-filter) - Array.prototype.filter analog for streams.
128
-
-[through2-reduce](https://github.com/brycebaril/through2-reduce) - Array.prototype.reduce analog for streams.
129
-
-[through2-spy](https://github.com/brycebaril/through2-spy) - Wrapper for simple stream.PassThrough spies.
130
-
- the [mississippi stream utility collection](https://github.com/maxogden/mississippi) includes `through2` as well as many more useful stream modules similar to this one
131
-
132
138
## License
133
139
134
-
**through2** is Copyright (c) Rod Vagg and additional contributors and licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
0 commit comments