| 1 |
# labeled-stream-splicer |
| 2 |
|
| 3 |
[stream splicer](https://npmjs.org/package/stream-splicer) with labels |
| 4 |
|
| 5 |
[](http://travis-ci.org/browserify/labeled-stream-splicer) |
| 6 |
|
| 7 |
# example |
| 8 |
|
| 9 |
Here's an example that exposes a label for `deps` and `pack`: |
| 10 |
|
| 11 |
``` js |
| 12 |
var splicer = require('labeled-stream-splicer'); |
| 13 |
var through = require('through2'); |
| 14 |
var deps = require('module-deps'); |
| 15 |
var pack = require('browser-pack'); |
| 16 |
var lstream = require('lstream'); |
| 17 |
|
| 18 |
var pipeline = splicer.obj([ |
| 19 |
'deps', [ deps() ], |
| 20 |
'pack', [ pack({ raw: true }) ] |
| 21 |
]); |
| 22 |
|
| 23 |
pipeline.get('deps').unshift(lstream()); |
| 24 |
|
| 25 |
pipeline.get('deps').push(through.obj(function (row, enc, next) { |
| 26 |
row.source = row.source.toUpperCase(); |
| 27 |
this.push(row); |
| 28 |
next(); |
| 29 |
})); |
| 30 |
|
| 31 |
process.stdin.pipe(pipeline).pipe(process.stdout); |
| 32 |
``` |
| 33 |
|
| 34 |
Here the `deps` sub-pipeline is augmented with a post-transformation that |
| 35 |
uppercases its source input. |
| 36 |
|
| 37 |
# methods |
| 38 |
|
| 39 |
``` js |
| 40 |
var splicer = require('labeled-stream-splicer') |
| 41 |
``` |
| 42 |
|
| 43 |
The API is the same as |
| 44 |
[stream-splicer](https://npmjs.org/package/stream-splicer), |
| 45 |
except that `pipeline.get()`, `pipeline.splice()`, and `pipeline.indexOf()` can |
| 46 |
accept string labels in addition to numeric indexes. |
| 47 |
|
| 48 |
## var pipeline = splicer(streams, opts) |
| 49 |
|
| 50 |
Create a `pipeline` duplex stream given an array of `streams`. Each `stream` |
| 51 |
will be piped to the next. Writes to `pipeline` get written to the first stream |
| 52 |
and data for reads from `pipeline` come from the last stream. |
| 53 |
|
| 54 |
To signify a label, a stream may have a `.label` property or a string may be |
| 55 |
placed in the `streams` array. |
| 56 |
|
| 57 |
For example, for streams `[ a, 'foo', b, c, 'bar', d ]`, this pipeline is |
| 58 |
constructed internally: |
| 59 |
|
| 60 |
``` |
| 61 |
a.pipe(b).pipe(c).pipe(d) |
| 62 |
``` |
| 63 |
|
| 64 |
with a label `'foo`' that points to `b` and a label `'bar'` that points to `d`. |
| 65 |
If `a` or `c` has a `.label` property, that label would be used for addressing. |
| 66 |
|
| 67 |
Input will get written into `a`. Output will be read from `d`. |
| 68 |
|
| 69 |
If any of the elements in `streams` are arrays, they will be converted into |
| 70 |
nested labeled pipelines. This is useful if you want to expose a hookable |
| 71 |
pipeline with grouped insertion points. |
| 72 |
|
| 73 |
## var pipeline = splicer.obj(streams, opts) |
| 74 |
|
| 75 |
Create a `pipeline` with `opts.objectMode` set to true for convenience. |
| 76 |
|
| 77 |
## var removed = pipeline.splice(index, howMany, stream, ...) |
| 78 |
|
| 79 |
Splice the pipeline starting at `index`, removing `howMany` streams and |
| 80 |
replacing them with each additional `stream` argument provided. |
| 81 |
|
| 82 |
The streams that were removed from the splice and returned. |
| 83 |
|
| 84 |
`index` can be an integer index or a label. |
| 85 |
|
| 86 |
## pipeline.push(stream, ...) |
| 87 |
|
| 88 |
Push one or more streams to the end of the pipeline. |
| 89 |
|
| 90 |
The stream arguments may have a `label` property that will be used for string |
| 91 |
lookups. |
| 92 |
|
| 93 |
## var stream = pipeline.pop() |
| 94 |
|
| 95 |
Pop a stream from the end of the pipeline. |
| 96 |
|
| 97 |
## pipeline.unshift(stream, ...) |
| 98 |
|
| 99 |
Unshift one or more streams to the begining of the pipeline. |
| 100 |
|
| 101 |
The stream arguments may have a `label` property that will be used for string |
| 102 |
lookups. |
| 103 |
|
| 104 |
## var stream = pipeline.shift() |
| 105 |
|
| 106 |
Shift a stream from the begining of the pipeline. |
| 107 |
|
| 108 |
## var stream = pipeline.get(index) |
| 109 |
|
| 110 |
Return the stream at index `index`. |
| 111 |
|
| 112 |
`index` can be an integer or a string label. |
| 113 |
|
| 114 |
# install |
| 115 |
|
| 116 |
With [npm](https://npmjs.org) do: |
| 117 |
|
| 118 |
``` |
| 119 |
npm install labeled-stream-splicer |
| 120 |
``` |
| 121 |
|
| 122 |
# license |
| 123 |
|
| 124 |
MIT |