JSON2CSVTransform.js
4.47 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
const Transform = require('stream').Transform;
const Parser = require('jsonparse');
const JSON2CSVBase = require('./JSON2CSVBase');
class JSON2CSVTransform extends Transform {
constructor(opts, transformOpts) {
super(transformOpts);
// Inherit methods from JSON2CSVBase since extends doesn't
// allow multiple inheritance and manually preprocess opts
Object.getOwnPropertyNames(JSON2CSVBase.prototype)
.forEach(key => (this[key] = JSON2CSVBase.prototype[key]));
this.opts = this.preprocessOpts(opts);
this._data = '';
this._hasWritten = false;
if (this.opts.ndjson) {
this.initNDJSONParse();
} else {
this.initJSONParser();
}
if (this.opts.withBOM) {
this.push('\ufeff');
}
if (this.opts.fields) {
this.pushHeader();
}
}
/**
* Init the transform with a parser to process NDJSON data.
* It maintains a buffer of received data, parses each line
* as JSON and send it to `pushLine for processing.
*/
initNDJSONParse() {
const transform = this;
this.parser = {
_data: '',
write(chunk) {
this._data += chunk.toString();
const lines = this._data
.split('\n')
.map(line => line.trim())
.filter(line => line !== '');
lines
.forEach((line, i) => {
try {
transform.pushLine(JSON.parse(line));
} catch(e) {
if (i !== lines.length - 1) {
e.message = 'Invalid JSON (' + line + ')'
transform.emit('error', e);
}
}
});
this._data = this._data.slice(this._data.lastIndexOf('\n'));
}
};
}
/**
* Init the transform with a parser to process JSON data.
* It maintains a buffer of received data, parses each as JSON
* item if the data is an array or the data itself otherwise
* and send it to `pushLine` for processing.
*/
initJSONParser() {
const transform = this;
this.parser = new Parser();
this.parser.onValue = function (value) {
if (this.stack.length !== this.depthToEmit) return;
transform.pushLine(value);
}
this.parser._onToken = this.parser.onToken;
this.parser.onToken = function (token, value) {
transform.parser._onToken(token, value);
if (this.stack.length === 0
&& !transform.opts.fields
&& this.mode !== Parser.C.ARRAY
&& this.mode !== Parser.C.OBJECT) {
this.onError(new Error('Data should not be empty or the "fields" option should be included'));
}
if (this.stack.length === 1) {
if(this.depthToEmit === undefined) {
// If Array emit its content, else emit itself
this.depthToEmit = (this.mode === Parser.C.ARRAY) ? 1 : 0;
}
if (this.depthToEmit !== 0 && this.stack.length === 1) {
// No need to store the whole root array in memory
this.value = undefined;
}
}
}
this.parser.onError = function (err) {
if(err.message.indexOf('Unexpected') > -1) {
err.message = 'Invalid JSON (' + err.message + ')';
}
transform.emit('error', err);
}
}
/**
* Main function that send data to the parse to be processed.
*
* @param {Buffer} chunk Incoming data
* @param {String} encoding Encoding of the incoming data. Defaults to 'utf8'
* @param {Function} done Called when the proceesing of the supplied chunk is done
*/
_transform(chunk, encoding, done) {
this.parser.write(chunk);
done();
}
/**
* Generate the csv header and pushes it downstream.
*/
pushHeader() {
if (this.opts.header) {
const header = this.getHeader(this.opts);
this.emit('header', header);
this.push(header);
this._hasWritten = true;
}
}
/**
* Transforms an incoming json data to csv and pushes it downstream.
*
* @param {Object} data JSON object to be converted in a CSV row
*/
pushLine(data) {
const processedData = this.preprocessRow(data);
if (!this._hasWritten) {
this.opts.fields = this.opts.fields || Object.keys(processedData[0]);
this.pushHeader();
}
processedData.forEach(row => {
const line = this.processRow(row, this.opts);
if (line === undefined) return;
const eoledLine = (this._hasWritten ? this.opts.eol : '')
+ line;
this.emit('line', line);
this.push(eoledLine);
this._hasWritten = true;
});
}
}
module.exports = JSON2CSVTransform;