You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.5 KiB

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