-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
77 lines (54 loc) · 1.46 KB
/
utility.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
require('dotenv').config();
const moment = require('moment');
const chalk = require('chalk');
module.exports.getDiffDays = (timeOne, timeTwo, key) => {
const timeOneMoment = moment(timeOne);
const timeTwoMoment = moment(timeTwo);
let timeDiff;
if (key) {
timeDiff = timeOneMoment.diff(timeTwoMoment, key);
} else {
timeDiff = timeOneMoment.diff(timeTwoMoment, 'days');
}
return timeDiff
}
module.exports.roundToOneDecimal = (num) => {
return Math.round(num * 10) / 10;
}
module.exports.highlightNumber = (num) => {
let newNum = num;
if (num > 0) {
newNum = chalk.bold.bgGreen(`+${Math.abs(num)}%`);
} else if (num < 0){
newNum = chalk.bold.bgRed(`-${Math.abs(num)}%`);
}
return newNum;
}
module.exports.checkEnvVariable = () => {
const isContentCountEmpty = process.env.CONTENT_COUNT.length === 0;
const isDATAUrlEmpty = process.env.DATA_URL.length === 0;
if (isContentCountEmpty || isDATAUrlEmpty) {
const error = new Error('Environment variable is empty');
throw error;
}
try {
parseInt(process.env.CONTENT_COUNT);
} catch(e) {
const error = new Error('Content Count is not a number');
throw error;
}
return true
}
module.exports.highlightDemaDiff = (num) => {
let newNum = num;
if (num > 4) {
newNum = chalk.bold.bgMagenta(num);
}
if (num > 10) {
newNum = chalk.bold.bgYellow(num);
}
if (num > 100) {
newNum = chalk.bold.bgRed(num);
}
return newNum;
}