-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
366 lines (358 loc) · 12.6 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Module dependencies.
*/
var os = require('os');
var _ = require('underscore');
/**
* Main function that converts json to csv
*
* @param {Object|Array} json
* @param {Object} [options]
* @param {Function} callback(err, csv) - Callback function
* if error, returning error in call back.
* if csv is created successfully, returning csv output to callback.
*/
module.exports = function(json,userOptions,callback) {
callback = callback ? callback : function(){};
if(_.isFunction(userOptions))
callback = userOptions;
else
options = parseOptions(userOptions);
generateCsv(json, callback);
};
/**
* Replaces the default options with the custom user options
*
* @param {Options} userOptions
*/
function parseOptions(userOptions){
/**
* Default options
*/
var options = {
headerPathString: '.', // String
rowDelimiter: ',', // String
textDelimiter: '"', // String
arrayPathString: ';', // String
undefinedString: '', // String
endOfLine: null, // String
mainPathItem: null, // String
booleanTrueString: null, // String
booleanFalseString: null, // String
includeHeaders: true, // Boolean
orderHeaders: false, // Boolean
verticalOutput: true, // Boolean
//Handlers
handleString: null, // Function
handleNumber: null, // Function
handleBoolean: null, // Function
handleDate: null, // Function
//handleArray: null, // Function
//handleObject: null // Function
};
return _.extend(options, userOptions);
}
/**
* Generates a CSV file with optional headers based on the passed JSON,
* with can be an Object or Array.
*
* @param {Object|Array} json
* @param {Function} callback(err,csv) - Callback function
* if error, returning error in call back.
* if csv is created successfully, returning csv output to callback.
*/
function generateCsv(json, callback) {
//Check if the passed json is an Array
if(_.isArray(json)){
var parseResult = [];
json.forEach(function (item) {
//Call checkType to list all items inside this object
//Items are returned as a object {item: 'Prop Value, Item Name',
// value: 'Prop Data Value'}
var itemResult = checkType(item, options.mainPathItem);
parseResult.push(itemResult);
});
//Generate the csv headers based on prop usage over the elements
var headers = generateHeaders(parseResult);
//Generate the csv output
var fileRows = [];
if(options.includeHeaders){
//Add the headers to the first line
fileRows.push(headers.join(options.rowDelimiter));
}
parseResult.forEach(function (result) {
//Initialize the array with empty strings to handle 'unpopular' headers
var resultRows = [Array(headers.length).join(".").split(".")];
result.forEach(function(element){
var placed = false;
resultRows.forEach(function (row) {
if(!placed && row[headers.indexOf(element.item)] == ''){
row[headers.indexOf(element.item)] = element.value;
placed = true;
}
});
if(!placed){
var newRow = Array(headers.length).join(".").split(".");
newRow[headers.indexOf(element.item)] = element.value;
resultRows.push(newRow);
}
});
resultRows.forEach(function (row) {
fileRows.push(row.join(options.rowDelimiter));
});
});
//Merge all rows in a single output with the correct End of Line string
var outputFile = fileRows.join(options.endOfLine || os.EOL || '\n');
return callback(null, outputFile);
}
//Check if the passed json is an Object
else if(_.isObject(json)){
var fileRows = [];
var horizontalRows = [[],[]];
var textDelimiterRegex = new RegExp("\\" + options.textDelimiter, 'g');
var endOfLine = options.endOfLine || os.EOL || '\n';
for(var prop in json){
var prefix = "";
if(options.mainPathItem)
prefix = options.mainPathItem + options.headerPathString;
var parseResult = checkType(json[prop], prefix + prop);
parseResult.forEach(function (result) {
var value = result.value ? result.value.toString() : options.undefinedString;
// Escape the textDelimiters contained in the field
/*(https://tools.ietf.org/html/rfc4180)
7. If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote.
For example: "aaa","b""bb","ccc"
*/
value = value.replace(textDelimiterRegex, options.textDelimiter+options.textDelimiter);
// Escape the whole field if it contains a rowDelimiter or a linebreak
if (value.indexOf(options.rowDelimiter) >= 0 || value.indexOf(endOfLine) >= 0) {
value = options.textDelimiter + value + options.textDelimiter;
}
//Type header;value
if(options.verticalOutput){
var row = [result.item, value];
fileRows.push(row.join(options.rowDelimiter));
}else{
horizontalRows[0].push(result.item);
horizontalRows[1].push(value);
}
});
}
if(!options.verticalOutput){
fileRows.push(horizontalRows[0].join(options.rowDelimiter));
fileRows.push(horizontalRows[1].join(options.rowDelimiter));
}
//Merge all rows in a single output with the correct End of Line string
var outputFile = fileRows.join(options.endOfLine || os.EOL || '\n');
return callback(null, outputFile);
}
return callback(new Error('Unable to parse the JSON object, its not an Array or Object.'));
}
/**
* Generate the file headers with optional sorting by header usage
*
* @param {Array} parseResult
* @returns {Array} headers
*/
function generateHeaders(parseResult){
var headers = [];
var headerCount = [];
//Check all the parsed json results
parseResult.forEach(function (result) {
result.forEach(function (element) {
if(headerCount[element.item])
headerCount[element.item] += 1;
else
headerCount[element.item] = 1;
})
});
var headerSort = [];
//Create a sortable collection of headers
for(var header in headerCount){
if(headerCount.hasOwnProperty(header)){
headerSort.push({
name: header,
count: headerCount[header]
});
}
}
if(options.orderHeaders){
//Sort the headers based on the count.
headerSort = _.sortBy(headerSort,'count').reverse();
}
//Create the headers list
headerSort.forEach(function (header) {
headers.push(header.name);
});
//Return the headers
return headers;
}
/**
* Check the element type of the element call the correct handle function
*
* @param element Element that will be checked
* @param item Used to make the headers/path breadcrumb
*/
function checkType(element, item){
var result = [];
//Check if element is a String
if(_.isString(element)){
var resultString = options.handleString ? options.handleString(element, item) : handleString(element, item);
result.push({
item: item,
value: resultString
});
}
//Check if element is a Number
else if(_.isNumber(element)){
var resultNumber = options.handleNumber ? options.handleNumber(element, item) : handleNumber(element, item);
result.push({
item: item,
value: resultNumber
});
}
//Check if element is a Boolean
else if(_.isBoolean(element)){
var resultBoolean = options.handleBoolean ? options.handleBoolean(element, item) : handleBoolean(element, item);
result.push({
item: item,
value: resultBoolean
});
}
//Check if element is a Date
else if(_.isDate(element)){
var resultDate = options.handleDate ? options.handleDate(element, item) : handleDate(element, item);
result.push({
item: item,
value: resultDate
});
}
//Check if element is an Array
else if(_.isArray(element)){
var resultArray = handleArray(element, item);
resultArray.forEach(function (resultArrayElement) {
if(resultArrayElement.item != null && item){
resultArrayElement.item = item + options.headerPathString + resultArrayElement.item;
}
if(!resultArrayElement.item && item){
resultArrayElement.item = item;
}
result.push(resultArrayElement);
});
}
//Check if element is a Object
else if(_.isObject(element)){
var resultObject = handleObject(element, item);
resultObject.forEach(function (resultObjectElement) {
if(resultObjectElement.item != null && item) {
resultObjectElement.item = item + options.headerPathString + resultObjectElement.item;
}
if(!resultObjectElement.item && item){
resultObjectElement.item = item;
}
result.push(resultObjectElement);
});
}
//Finished, returning the result now
return result;
}
/**
* Handle all Objects
*
* @param {Object} obj
* @returns {Array} result
*/
function handleObject(obj){
var result = [];
//Look every object props
for(var prop in obj){
var propData = obj[prop];
//Check the propData type
var resultCheckType = checkType(propData, prop);
//Append to results
result = result.concat(resultCheckType);
}
return result;
}
/**
* Handle all Arrays, merges arrays with primitive types in a single value
*
* @param {Array} array
* @returns {Array} result
*/
function handleArray(array){
var result = [];
array.forEach(function(element) {
//Check the propData type
var resultCheckType = checkType(element);
//Append to results
result = result.concat(resultCheckType);
});
//Check for results without itens, merge all itens with the first occurrence
var firstElementWithoutItem;
result = result.filter(function (resultElement, resultIndex) {
//Check if there is a firstElement and if the current element dont have a item
if(resultElement.item == null && firstElementWithoutItem != null){
//Append the value to the firstElementWithoutItem.
result[firstElementWithoutItem].value += options.arrayPathString + resultElement.value;
//Remove the item
return false;
}
//Set the firstElement if its not set
if(firstElementWithoutItem == null && resultElement.item == null)
firstElementWithoutItem = resultIndex;
//Keep the item in the array
return true;
});
//Finished, returning result now
return result;
}
/**
* Handle all Boolean variables, can be replaced with options.handleBoolean
*
* @param {Boolean} boolean
* @returns {String} result
*/
function handleBoolean(boolean){
var result;
//Check for booolean options
if(boolean){
result = options.booleanTrueString || 'true';
}else{
result = options.booleanFalseString || 'false';
}
//Finished, returning result now
return result;
}
/**
* Handle all String variables, can be replaced with options.handleString
*
* @param {String} string
* @returns {String} string
*/
function handleString(string){
//Finished, returning the string now
return string;
}
/**
* Handle all Number variables, can be replaced with options.handleNumber
*
* @param {Number} number
* @returns {Number} number
*/
function handleNumber(number){
//Finished, returning the number now
return number;
}
/**
* Handle all Date variables, can be replaced with options.handleDate
*
* @param {Date} number
* @returns {string} result
*/
function handleDate(date){
//Finished, returning the number now
return date.toLocaleString();
}