-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
86 lines (69 loc) · 2.72 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
79
80
81
82
83
84
85
86
const numFormater = (number) => {
const SI_SYMBOL = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
let tier = (Math.log10(number) / 3) | 0;
if (tier == 0) return number;
let suffix = SI_SYMBOL[tier];
let scale = Math.pow(10, tier * 3);
let scaled = number / scale;
return scaled.toFixed(1) + suffix;
};
const getDateKeyFromEpoch = (date) =>
date.getFullYear() * 10000 + (date.getMonth() + 1) * 100 + date.getDate();
const getDetailedDateLabelFromEpoch = (date) =>
`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
const getDateLabelFromDateKey = (key) => `${Math.floor((key % 10000) / 100)}/${key % 100}`;
const getMonthDiff = (olderTime, newerTime) => {
return (
newerTime.getMonth() -
olderTime.getMonth() +
1 +
12 * (newerTime.getFullYear() - olderTime.getFullYear())
);
};
const trimString = (str, length) => {
return str.length > length ? str.substring(0, length) + '...' : str;
};
const getHourDiff = (olderTime, newerTime) => {
return Math.abs(newerTime - olderTime) / 36e5;
};
const toPercentage = (num, den) => `${(num / den).toFixed(2) * 100}%`;
const getCsvString = (csvArray) => {
let csvString = '';
for (let i = 0; i < csvArray.length; i++) {
let value = csvArray[i];
for (let j = 0; j < value.length; j++) {
let innerValue = value[j] === null ? '' : value[j].toString();
let result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0) result = '"' + result + '"';
if (j > 0) csvString += ',';
csvString += result;
}
csvString += '\n';
}
return csvString;
};
const parseMediumResponse = (response) => JSON.parse(response.split('</x>')[1]);
const parseMediumFollowerResponse = (response) => {
const firstSplit = response.split(' Followers</h2>');
if (!firstSplit || !firstSplit.length || firstSplit.length < 2) return -1;
const secondSplit = firstSplit[0].split('">');
if (!secondSplit || !secondSplit.length || secondSplit.length < 2) return -1;
const localeString = secondSplit[secondSplit.length - 1];
const parsedLocaleString = localeString.replace(',', '');
if (isNaN(parsedLocaleString)) return -1;
const followerCount = +parsedLocaleString;
if (followerCount.toLocaleString() !== localeString) return -1;
return followerCount;
};
Date.prototype.addTime = function (timeType, timeOffset) {
let result = new Date(this);
result[`set${timeType}`](result[`get${timeType}`]() + timeOffset);
return result;
};
Date.prototype.daysInThisMonth = function () {
return new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate();
};
Date.prototype.getWeek = function () {
var firstDay = new Date(this.getFullYear(), 0, 1);
return Math.ceil(((this - firstDay) / 86400000 + firstDay.getDay() + 1) / 7) - 1;
};