-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (39 loc) · 1.49 KB
/
index.js
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
/**
* Created by JounQin on 16/7/1.
*/
'use strict';
var Transform = require('readable-stream/transform'),
rs = require('replacestream'),
xml2js = require('xml2js');
var DEFAULT_OPTIONS = {outType: true};
module.exports = function (options) {
options = Object.assign({}, DEFAULT_OPTIONS, options);
return new Transform({
objectMode: true,
transform: function (file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
}
function process() {
if (file.isStream()) {
file.contents = file.contents.pipe(rs());
}
if (!file.isBuffer()) return callback(null, file);
new xml2js.Parser(options.parserOpts)
.parseString(file.contents, function (err, result) {
if (err) return callback(err, file);
result = options.outType
? new xml2js.Builder(options.buildOpts).buildObject(result) : JSON.stringify(result);
try {
options.callback && (result = options.callback(result) || result);
} catch (err) {
return callback(err, file);
}
file.contents = new Buffer(result);
callback(null, file);
});
}
process();
}
});
};