This repository was archived by the owner on Sep 13, 2022. It is now read-only.
forked from drkreddy/ocb-lab-data-migration-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
54 lines (45 loc) · 1.68 KB
/
lib.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
const path = require('path');
const csv = require('csvtojson');
const Promise = require('promise');
const writeFile = require('fs').writeFile;
const getDestFileName = (sourceFile, destFile) => {
const extension = path.extname(sourceFile);
return destFile || (path.basename(sourceFile, extension) + "_generated" + extension);
};
const convertCSVToJSON = (csvFilePath) => {
return new Promise((resolve, reject) => {
const results = [];
csv().fromFile(csvFilePath)
.on('json', (jsonObj) => {
results.push(jsonObj)
})
.on('done', (error) => {
if (error)
reject(error);
else
resolve(results)
});
});
};
const writeToDestFile = (contents, fileName) => {
return writeFile(fileName, contents, (err) => {
if (err)
throw err;
console.info("File has been successfully converted.");
console.info("The generated file can be found here: " + fileName);
});
};
const getIdentifier = (identifier) => convertSlashID(identifier.replace(/^(CHK)|(CP)/, ""));
const convertSlashID = (identifier) => {
const regex = new RegExp("^([0-9]{2})999/([0-9]+)$")
if (regex.test(identifier)) {
const res = regex.exec(identifier)
return res[1] + (1000 + parseInt(res[2]))
}
return identifier
}
const getTestName = (testName, value, defaultTestName) => !!value ? testName : defaultTestName;
const getDate = (dateString) => {
return new Date(dateString).toISOString().split("T")[0];
};
module.exports = {getDestFileName, convertCSVToJSON, getDate, writeToDestFile, getTestName, getIdentifier};