-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (44 loc) · 1.43 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
require("replaceall-shim");
const escapeObject = (obj) => {
if(obj === null || obj === undefined) return "";
let str = JSON.stringify( obj );
str = str.replaceAll('"', "'");
return `${str}`;
};
const escapeString = (str) => {
str = str.replaceAll('"', "'");
return `"${str}"`;
};
const convert = (sourceJson) => {
if (!Array.isArray(sourceJson) || !sourceJson.length) throw new Error('Currently only JSON arrays are supported...');
let headersObj = {};
let headersArr = [];
let valuesArr = [];
sourceJson.forEach((m, row) => {
let currentRow = [];
Object.entries(m).forEach(([k, v]) => {
if (!headersObj.hasOwnProperty(k)) {
headersObj[k] = headersArr.length;
headersArr.push(k);
}
const currId = headersObj[k];
if (Array.isArray(v)) {
v = v.map( item => {
if(typeof item === "object") return escapeObject(item);
if(typeof item === "string") return escapeString(item).replaceAll('"', "'");
return item;
});
currentRow[currId] = `"${v.join(',')}"`
} else if (typeof v === "object") {
currentRow[currId] = `"${escapeObject(v)}"`;
} else if(typeof v === 'string'){
currentRow[currId] = escapeString(v);
} else {
currentRow[currId] = `"${v}"`;
}
});
valuesArr[row] = currentRow.join(',');
});
return headersArr.join(',') + '\n' + valuesArr.join('\n');
};
exports.convert = convert;