-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
105 lines (85 loc) · 3.18 KB
/
common.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
'use strict';
const fs = require('fs');
const path = require('path');
let local = {
startTime : 0,
countLine : 0
}
/**
* Initializes the script startup time to determine the processing time at the end of the conversion process.
*/
module.exports.init = () => {
local.startTime = process.hrtime();
};
/**
* Construct the path for the JSON file to the output result.
*
* @param {string} csvFilepath - the path to the CSV file to convert
* @returns {string} the path for the JSON file to the output result
*/
module.exports.buildJsonFilepath = (csvFilepath) => {
const objectPath = path.parse(csvFilepath);
// Assuming that the CSV files to be converted is in the CSV directory.
const resultJsonFilepath = objectPath.dir.replace('CSV', 'JSON') + path.sep + objectPath.name + '.json';
return resultJsonFilepath;
};
/**
* Normalizes the CSV separator in the case of a special character.
*
* @param {string} csvSeparator - character or pattern for separate fields
* @returns {string} CSV separator normalized
*/
module.exports.normalizeCSVSeparator = (csvSeparator) => {
let normalizeCSVSeparator = csvSeparator;
// normalize string with special character to character code
if (normalizeCSVSeparator.valueOf() === '\\t') {
normalizeCSVSeparator = String.fromCharCode(9);
}
return normalizeCSVSeparator;
};
/**
* Transforms the first head row in a array
*
* @param {string} line - raw text line from CSV file
* @param {string} csvSeparator - character or pattern for separate fields
* @returns {array.string} array of head names
*/
module.exports.retrieveHeaderFields = (line, csvSeparator) => {
const headerFields = line.split(csvSeparator);
return headerFields;
};
/**
* Transforms the raw text line in javascript object
*
* @param {string} line - raw text line from CSV file
* @param {string} csvSeparator - character or pattern for separate fields
* @param {array.string} array of head names
* @return {object} representation of raw text line in javascript object
*/
module.exports.transformLineToObject = (line, csvSeparator, headerFields) => {
let arrayOfvalues = line.split(csvSeparator);
let itemResult = {};
for (let index = 0; index < headerFields.length; index++) {
let keyName = headerFields[index];
let keyValue = arrayOfvalues[index];
itemResult[keyName] = keyValue;
}
local.countLine++;
return itemResult;
};
/**
* At end of process conversion time, stop time execution.
* console.log a duration process and memory consumed
*/
module.exports.end = (pathBenchmarkOutputFile) => {
const endTime = process.hrtime(local.startTime);
let totalExecutionmilliSeconds = parseInt((endTime[0] * 1e3) + (endTime[1] * 1e-6), 10);
console.log(`Finish. Number of line read : ${local.countLine}. In ${totalExecutionmilliSeconds} miliseconds`);
const usedMemory = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`The script uses approximately ${usedMemory} MB`);
if (pathBenchmarkOutputFile) {
fs.appendFile(pathBenchmarkOutputFile, `${totalExecutionmilliSeconds};${usedMemory}\r\n`, (err) => {
if (err) throw err;
});
}
};