This repository was archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (116 loc) · 3.35 KB
/
Copy pathindex.js
File metadata and controls
135 lines (116 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var through = require('through3')
, ast = require('mkast');
/**
* Injects custom stream transform classes into the pipeline.
*
* Accepts a single function, array of functions or an object with a
* `transforms` array.
*
* Functions have the signature:
*
* ```javascript
* function(through, ast, opts)
* ```
*
* They are passed the [through][] and [ast][mkast] modules to help with
* creating stream subclasses and inspecting nodes and the original options.
*
* If you are using multiple stream transformations the options are shared
* between them so take care to avoid name collisions.
*
* Each function **must** return a transform stream subclass.
*
* The returned subclass is instantiated and when multiple transform functions
* are being used a pipeline is created between the streams in the order
* supplied.
*
* When a single stream is being created it is returned otherwise an array
* of all the created streams is returned.
*
* If both the `input` and `output` options are given additional wrapper
* streams are created that parse JSON from the input stream and write JSON
* to the output stream, in this instance you may pass a callback function
* which is added as a listener for the `error` and `finish` events on the
* output stream; the output stream is returned.
*
* @function transform
* @param {Function|Array|Object} opts processing options.
* @param {Function} [cb] callback function.
*
* @option {Array} transforms list of transform functions.
* @option {Readable} [input] input stream.
* @option {Writable} [output] output stream.
*
* @throws TypeError if the target is not a function.
* @throws TypeError if the return value is not a function.
* @throws TypeError if the stream instance has no pipe function.
*
* @returns an output stream or array of streams.
*/
function transform(opts, cb) {
opts = opts || {};
if(typeof opts === 'function') {
opts = {
transforms: [opts]
}
}else if(Array.isArray(opts)) {
opts = {
transforms: opts
}
}
var transforms = opts.transforms || []
, func
, Stream
, first
, stream
, previous
, i
, io = Boolean(opts.input && opts.output)
, streams = [];
for(i = 0;i < transforms.length;i++) {
func = transforms[i];
if(typeof func !== 'function') {
throw new TypeError('transform function expected');
}
Stream = func(through, ast, opts);
if(typeof Stream !== 'function') {
throw new TypeError(
'transform function should return stream constructor');
}
stream = new Stream(opts);
streams.push(stream);
if(!first) {
first = stream;
}
if(typeof stream.pipe !== 'function') {
throw new TypeError('stream is missing pipe() method');
}
if(previous && !io) {
previous.pipe(stream);
}
previous = stream;
}
if(!opts.input || !opts.output) {
if(streams.length <= 1) {
return first;
}
return stream;
}
// set up input stream
stream = ast.parser(opts.input);
// inject custom stream transformations
for(i = 0;i < streams.length;i++) {
stream = stream.pipe(streams[i]);
}
// set up output stream
stream
.pipe(ast.stringify())
.pipe(opts.output);
if(cb) {
opts.output
.once('error', cb)
.once('finish', cb);
}
return opts.output;
}
module.exports = transform;