-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
65 lines (53 loc) · 1.41 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
const chalk = require('chalk');
const DAY_MAP = {
0: 'Sunday',
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday',
};
const DAY_MAP_INVERSE = {
Sunday: 0,
Monday: 1,
Tuesday: 2,
Wednesday: 3,
Thursday: 4,
Friday: 5,
Saturday: 6,
};
const getDayName = date => DAY_MAP[new Date(date).getDay()];
const groupBySum = (arr, keySelector, valueSelector) => arr.reduce((acc, item) => {
const key = keySelector(item);
const value = valueSelector(item);
if (acc[key]) {
acc[key] += value;
} else {
acc[key] = value;
}
return acc;
}, {});
const sortByValueDesc = (a, b) => b.value - a.value;
const printBars = (data, { top = 10, width = 75, sortFn = sortByValueDesc } = {}) => {
const block = '\u2580';
const labelValueArr = Object.entries(data)
.map(([key, value]) => ({ label: key, value }));
const maxValue = Math.max(...labelValueArr.map(it => it.value));
labelValueArr
.sort(sortFn)
.slice(0, top)
.forEach(({ label, value }) => {
const truncatedValue = Math.trunc(value);
console.log(
chalk.yellowBright.bold(label),
' '.repeat(25 - label.length),
chalk.redBright(`₹${truncatedValue}`),
' '.repeat(5 - truncatedValue.toString().length),
block.repeat(width * truncatedValue / maxValue),
);
});
};
module.exports = {
getDayName, groupBySum, printBars, DAY_MAP_INVERSE,
};