1 | # merge2
|
---|
2 |
|
---|
3 | Merge multiple streams into one stream in sequence or parallel.
|
---|
4 |
|
---|
5 | [![NPM version][npm-image]][npm-url]
|
---|
6 | [![Build Status][travis-image]][travis-url]
|
---|
7 | [![Downloads][downloads-image]][downloads-url]
|
---|
8 |
|
---|
9 | ## Install
|
---|
10 |
|
---|
11 | Install with [npm](https://npmjs.org/package/merge2)
|
---|
12 |
|
---|
13 | ```sh
|
---|
14 | npm install merge2
|
---|
15 | ```
|
---|
16 |
|
---|
17 | ## Usage
|
---|
18 |
|
---|
19 | ```js
|
---|
20 | const gulp = require('gulp')
|
---|
21 | const merge2 = require('merge2')
|
---|
22 | const concat = require('gulp-concat')
|
---|
23 | const minifyHtml = require('gulp-minify-html')
|
---|
24 | const ngtemplate = require('gulp-ngtemplate')
|
---|
25 |
|
---|
26 | gulp.task('app-js', function () {
|
---|
27 | return merge2(
|
---|
28 | gulp.src('static/src/tpl/*.html')
|
---|
29 | .pipe(minifyHtml({empty: true}))
|
---|
30 | .pipe(ngtemplate({
|
---|
31 | module: 'genTemplates',
|
---|
32 | standalone: true
|
---|
33 | })
|
---|
34 | ), gulp.src([
|
---|
35 | 'static/src/js/app.js',
|
---|
36 | 'static/src/js/locale_zh-cn.js',
|
---|
37 | 'static/src/js/router.js',
|
---|
38 | 'static/src/js/tools.js',
|
---|
39 | 'static/src/js/services.js',
|
---|
40 | 'static/src/js/filters.js',
|
---|
41 | 'static/src/js/directives.js',
|
---|
42 | 'static/src/js/controllers.js'
|
---|
43 | ])
|
---|
44 | )
|
---|
45 | .pipe(concat('app.js'))
|
---|
46 | .pipe(gulp.dest('static/dist/js/'))
|
---|
47 | })
|
---|
48 | ```
|
---|
49 |
|
---|
50 | ```js
|
---|
51 | const stream = merge2([stream1, stream2], stream3, {end: false})
|
---|
52 | //...
|
---|
53 | stream.add(stream4, stream5)
|
---|
54 | //..
|
---|
55 | stream.end()
|
---|
56 | ```
|
---|
57 |
|
---|
58 | ```js
|
---|
59 | // equal to merge2([stream1, stream2], stream3)
|
---|
60 | const stream = merge2()
|
---|
61 | stream.add([stream1, stream2])
|
---|
62 | stream.add(stream3)
|
---|
63 | ```
|
---|
64 |
|
---|
65 | ```js
|
---|
66 | // merge order:
|
---|
67 | // 1. merge `stream1`;
|
---|
68 | // 2. merge `stream2` and `stream3` in parallel after `stream1` merged;
|
---|
69 | // 3. merge 'stream4' after `stream2` and `stream3` merged;
|
---|
70 | const stream = merge2(stream1, [stream2, stream3], stream4)
|
---|
71 |
|
---|
72 | // merge order:
|
---|
73 | // 1. merge `stream5` and `stream6` in parallel after `stream4` merged;
|
---|
74 | // 2. merge 'stream7' after `stream5` and `stream6` merged;
|
---|
75 | stream.add([stream5, stream6], stream7)
|
---|
76 | ```
|
---|
77 |
|
---|
78 | ```js
|
---|
79 | // nest merge
|
---|
80 | // equal to merge2(stream1, stream2, stream6, stream3, [stream4, stream5]);
|
---|
81 | const streamA = merge2(stream1, stream2)
|
---|
82 | const streamB = merge2(stream3, [stream4, stream5])
|
---|
83 | const stream = merge2(streamA, streamB)
|
---|
84 | streamA.add(stream6)
|
---|
85 | ```
|
---|
86 |
|
---|
87 | ## API
|
---|
88 |
|
---|
89 | ```js
|
---|
90 | const merge2 = require('merge2')
|
---|
91 | ```
|
---|
92 |
|
---|
93 | ### merge2()
|
---|
94 |
|
---|
95 | ### merge2(options)
|
---|
96 |
|
---|
97 | ### merge2(stream1, stream2, ..., streamN)
|
---|
98 |
|
---|
99 | ### merge2(stream1, stream2, ..., streamN, options)
|
---|
100 |
|
---|
101 | ### merge2(stream1, [stream2, stream3, ...], streamN, options)
|
---|
102 |
|
---|
103 | return a duplex stream (mergedStream). streams in array will be merged in parallel.
|
---|
104 |
|
---|
105 | ### mergedStream.add(stream)
|
---|
106 |
|
---|
107 | ### mergedStream.add(stream1, [stream2, stream3, ...], ...)
|
---|
108 |
|
---|
109 | return the mergedStream.
|
---|
110 |
|
---|
111 | ### mergedStream.on('queueDrain', function() {})
|
---|
112 |
|
---|
113 | It will emit 'queueDrain' when all streams merged. If you set `end === false` in options, this event give you a notice that should add more streams to merge or end the mergedStream.
|
---|
114 |
|
---|
115 | #### stream
|
---|
116 |
|
---|
117 | *option*
|
---|
118 | Type: `Readable` or `Duplex` or `Transform` stream.
|
---|
119 |
|
---|
120 | #### options
|
---|
121 |
|
---|
122 | *option*
|
---|
123 | Type: `Object`.
|
---|
124 |
|
---|
125 | * **end** - `Boolean` - if `end === false` then mergedStream will not be auto ended, you should end by yourself. **Default:** `undefined`
|
---|
126 |
|
---|
127 | * **pipeError** - `Boolean` - if `pipeError === true` then mergedStream will emit `error` event from source streams. **Default:** `undefined`
|
---|
128 |
|
---|
129 | * **objectMode** - `Boolean` . **Default:** `true`
|
---|
130 |
|
---|
131 | `objectMode` and other options(`highWaterMark`, `defaultEncoding` ...) is same as Node.js `Stream`.
|
---|
132 |
|
---|
133 | ## License
|
---|
134 |
|
---|
135 | MIT © [Teambition](https://www.teambition.com)
|
---|
136 |
|
---|
137 | [npm-url]: https://npmjs.org/package/merge2
|
---|
138 | [npm-image]: http://img.shields.io/npm/v/merge2.svg
|
---|
139 |
|
---|
140 | [travis-url]: https://travis-ci.org/teambition/merge2
|
---|
141 | [travis-image]: http://img.shields.io/travis/teambition/merge2.svg
|
---|
142 |
|
---|
143 | [downloads-url]: https://npmjs.org/package/merge2
|
---|
144 | [downloads-image]: http://img.shields.io/npm/dm/merge2.svg?style=flat-square
|
---|