-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (49 loc) · 1.72 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
/* jshint esversion: 6 */
exports.escapeSpecialCharacters = function (inputString, separator = ",") {
if (typeof separator !== "string" || separator.length < 1) {
separator = ",";
}
const result = inputString.replace(/"/g, '""');
const re = new RegExp(`["\r\n${separator.substr(0, 1)}]`);
const shouldEnclose = re.test(result);
return { result, shouldEnclose };
};
exports.stringOrNumberToString = function (input, radix = 10) {
if (typeof input === "string") {
return input;
}
if (typeof input === "number") {
return input.toString(radix);
}
return null;
};
// Some MIME types for CSV's (according to RFC 4180)
// text/csv
// text/csv; header
exports.makeCsvList = function (headers, objectList, options = { separator: ",", fullDQuote: false }) {
const { separator, fullDQuote } = options;
const result = [];
const lineList = [];
let sep = ",";
if (typeof separator === "string" && separator.length > 0) {
sep = separator.substr(0, 1);
}
headers.forEach((header) => {
if (typeof header !== "string") {
throw Error("headers in makeCsvList are not solely composed of strings");
}
const { result, shouldEnclose } = exports.escapeSpecialCharacters(header, separator);
lineList.push(shouldEnclose || fullDQuote ? `"${result}"` : result);
});
result.push(lineList.join(sep));
objectList.forEach((object) => {
lineList.length = 0;
headers.forEach((header) => {
const str = exports.stringOrNumberToString(object[header]);
const { result, shouldEnclose } = exports.escapeSpecialCharacters(str === null ? "" : str, sep);
lineList.push(shouldEnclose || fullDQuote ? `"${result}"` : result);
});
result.push(lineList.join(sep));
});
return result;
};