-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (110 loc) · 2.96 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
const CsvReadableStream = require("csv-reader");
const { createArrayCsvWriter, createObjectCsvWriter } = require("csv-writer");
const fs = require("fs");
const getFileName = (filePath) => filePath.split("/").pop();
/**
* Read the csv file in the given file path and return as array of array / json
*
* @param {string} filePath - path of the csv file to read
* @param {boolean} skipHeader - Boolean indicating whether to skip header or not,
* by default it will not be skipped
* @param {boolean} asObject - Boolean indicating whether to return the data as array
* of array or json
* @returns {Array<json>} - array containing data in each row as json object
*/
exports.csvFileReader = (
filePath = "",
skipHeader = false,
asObject = false
) => {
const csvData = [];
return new Promise((resolve, reject) => {
let inputstream = fs.createReadStream(filePath, "utf-8");
inputstream
.pipe(
new CsvReadableStream({
parseBooleans: true,
parseNumbers: true,
skipHeader,
asObject,
trim: true,
})
)
.on("data", (row) => {
csvData.push(row);
})
.on("error", (err) => {
console.log(`***** Error: ${err.message} from csvFileReader.js ******`);
reject(err);
})
.on("end", () => {
console.log(
` ====> CSV File - ${getFileName(filePath)} is read with ${
csvData.length
} rows \n`
);
resolve(csvData);
});
});
};
/**
* Receives data for csv in and write to the given file path with file name
*
* @param {string} filePath -
* @param {Array<Array<T>>} csvData
* @param {boolean} overwriteFile
*/
exports.createCSVWithArr = async (
filePath,
csvData = [[]],
overwriteFile = false
) => {
console.log("\n\nWriting records");
//Creating csv with array doesnt need the headers
if (!overwriteFile && fs.existsSync(filePath))
throw new Error(
`\nxxxxxxxx Create CSV error: ${filePath} already exists. Do you want to overwrite xxxxxxxx`
);
const arrCsvWriter = createArrayCsvWriter({
path: filePath,
encoding: "utf-8",
});
await arrCsvWriter.writeRecords(csvData);
console.log(
`\n+++++++++ CSV file (${getFileName(
filePath
)}) successfully created with ${csvData.length} rows +++++++++`
);
};
/**
* Receives data for csv as array of json object and write the data with given file name
* and file path
*
* @param {string} filePath
* @param {json} header
* @param {} csvData
* @param {boolean} overwriteFile
*/
exports.createCSVWithObj = async (
filePath,
header,
csvData = [[]],
overwriteFile = false
) => {
if (!overwriteFile && fs.existsSync(filePath))
throw new Error(
`\nxxxxxxxx Create CSV error: ${filePath} already exists. Do you want to overwrite xxxxxxxx`
);
const objCsvWriter = createObjectCsvWriter({
path: filePath,
encoding: "utf-8",
header,
alwaysQuote: false,
});
await objCsvWriter.writeRecords(csvData);
console.log(
`\n+++++++++ CSV file (${getFileName(
filePath
)}) successfully created with ${csvData.length} rows +++++++++`
);
};