-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
84 lines (69 loc) · 2.45 KB
/
tools.js
File metadata and controls
84 lines (69 loc) · 2.45 KB
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
const fs = require('fs');
const config = require('./config').default;
const buildHeaders = (extraHeaders) => ({
Authorization: config.auth,
Accept: 'application/json',
'Content-Type': 'application/json',
...extraHeaders
});
exports.buildOptions = (method, uri, query, body, extraHeaders = {}) => ({
method,
uri,
qs: query,
body,
headers: buildHeaders(extraHeaders),
json: true,
});
exports.promiseWrite = (body, path) => new Promise((res, rej) => {
fs.writeFile(`${__dirname}/${path}.json`, JSON.stringify(body, null, 4), 'utf8', (err) => {
if (err) rej(err);
res();
});
});
exports.promiseRead = (path) => new Promise((res, rej) => {
fs.readFile(`${__dirname}/${path}`, (err, data) => {
if (err) rej(err);
if (!data) {
console.error(`${path} is invalid`);
}
res(JSON.parse(data));
});
});
exports.getElementNames = (path = 'elements') => new Promise((res, rej) => {
fs.readdir(`${__dirname}/${path}`, (err, files) => {
if (err) rej(err);
res(files);
});
});
exports.getElementKeyFromPath = path => path.split('.')[0];
const getFormattedName = (str) =>
str.length < 8
? `${str}\t\t\t`
: str.length < 16
? `${str}\t\t`
: `${str}\t`;
const getFormattedValue = (val) =>
val && val.toString().length > 7
? `${val.toString().slice(8)}\t`
: val && val.toString().length < 5
? `${val}\t\t`
: `${val}\t`;
exports.printAnalytics = (timingMap, analyticsAmount) => {
console.log('\n---------------------------------------------------------------------------------');
console.log('|\tElement\t\t\t| Total(s)\t| ESV(s)\t| IRS(s)\t|');
console.log(' -------------------------------------------------------------------------------');
Object.keys(timingMap)
.sort((a, b) => timingMap[b].total - timingMap[a].total)
.slice(0, parseInt(analyticsAmount, 10))
.forEach(elementKey => {
console.log(`|\t${getFormattedName(elementKey)}| ${getFormattedValue(timingMap[elementKey].total)}| ${getFormattedValue(timingMap[elementKey].esv)}| ${getFormattedValue(timingMap[elementKey].irs)}|`);
});
console.log('---------------------------------------------------------------------------------');
}
// IDK what's up here... https://stackoverflow.com/a/46842181
async function filter(arr, callback) {
const fail = Symbol();
return (await Promise.all(arr.map(async item => (await callback(item)) ? item : fail)))
.filter(i=> i!==fail);
}
exports.promiseFilter = filter;