-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
executable file
·78 lines (64 loc) · 1.87 KB
/
utils.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
'use strict';
const fs = require('fs');
const { join } = require('path');
const readFile = (path, encoding = 'utf8') => new Promise((resolve, reject) => {
fs.readFile(join(__dirname, path), encoding, (error, result) =>
error ? reject(error) : resolve(result)
);
});
const writeFile = (path, data) => new Promise((resolve, reject) => {
fs.writeFile(join(__dirname, path), data, (error, result) =>
error ? reject(error) : resolve(result)
);
});
const getMemoryUsageMessage = () => {
const labelify = (value, convertToUpperCase) => {
const result = value.charAt(0).toUpperCase() +
value.substr(1).replace(/([a-z0-9])([A-Z])/g, '$1 $2');
return convertToUpperCase ? result.toUpperCase() : result;
};
const memoryUsage = process.memoryUsage();
const message = Object.keys(memoryUsage)
.map((key, i) =>
`${labelify(key, !i)}: ${
(memoryUsage[key] / 1024 / 1024).toFixed(2)
}`
)
.join(' :: ');
return `Memory Usage (MiB) [${message}]`;
};
const elapsedTime = (debug = 1) => {
let start = process.hrtime.bigint();
const timer = {
log: message => {
if (debug) {
const diff = process.hrtime.bigint() - start;
console.debug([
message && `${message}:`,
`${(diff / 1000000n).toLocaleString('en-US')}.${diff % 1000000n} ms`,
].join(' '));
}
return timer;
},
reset: () => {
start = process.hrtime.bigint();
return timer;
},
};
return timer;
};
const parseLine = (rawLine, stringToNumberArrayIndexes = []) => {
const parsedLine = rawLine.split(' ').filter(value => value.length);
return stringToNumberArrayIndexes.length
? parsedLine.map((value, i) =>
stringToNumberArrayIndexes.includes(i) ? +value : value
)
: parsedLine;
};
module.exports = {
readFile,
writeFile,
getMemoryUsageMessage,
elapsedTime,
parseLine,
};