-
Notifications
You must be signed in to change notification settings - Fork 23
/
helpers.ts
150 lines (118 loc) · 3.9 KB
/
helpers.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
export function random(min: number, max: number) {
return min + (Math.random() * (max - min));
}
export function isObject(item: any) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
export function mergeDeep(target: any, ...sources: any): any {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeDeep(target, ...sources);
}
export function numberWithCommas(x: number) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function secondsToTime(duration: number) {
const portions = [];
const secInHour = 60 * 60;
const hours = Math.trunc(duration / secInHour);
if (hours > 0) {
portions.push(hours + 'h');
duration = duration - (hours * secInHour);
}
const secInMinute = 60;
const minutes = Math.trunc(duration / secInMinute);
if (minutes > 0) {
portions.push(minutes + 'm');
duration = duration - (minutes * secInMinute);
}
const seconds = Math.trunc(duration);
if (seconds > 0) {
portions.push(seconds + 's');
}
return portions.join(' ');
}
function humanReadable(seconds: number) {
const timeUnits: any = [
[60, 'seconds'],
[60, 'minutes'],
[24, 'hours'],
[365, 'days'],
[Infinity, 'years'],
];
let unitIndex = 0;
let time = seconds;
while (time >= timeUnits[unitIndex][0] && unitIndex < timeUnits.length - 1) {
time /= timeUnits[unitIndex][0];
unitIndex++;
}
time = Math.floor(time);
const unitName = timeUnits[unitIndex][1];
return `${time} ${time === 1 ? unitName.slice(0, -1) : unitName}`;
}
export function sinceFrom(dateString: string) {
const pastDate = new Date(dateString).getTime();
const now = new Date().getTime();
const secondsDiff = Math.floor((now - pastDate) / 1000);
return humanReadable(secondsDiff);
}
export function fixDate(inputDate: Date) {
// https://stackoverflow.com/a/14569783
// return new Date( inputDate.getTime() + Math.abs(inputDate.getTimezoneOffset()*60000) )
return inputDate;
}
export function lastSeen(dateString: string) {
if (!dateString) return 'never';
let inputDate = new Date(dateString);
inputDate = fixDate(inputDate);
inputDate.setHours(0, 0, 0, 0);
const today = new Date();
today.setHours(0, 0, 0, 0);
const diff = (today as any) - (inputDate as any);
const daysDiff = Math.floor(diff / (1000 * 60 * 60 * 24));
if (daysDiff === 0) {
return 'today';
} else if (daysDiff === 1) {
return 'yesterday';
} else {
return daysDiff + ' days ago';
}
}
export function addCommas(num: number) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function calculateGemsXP(coins: number, kills: number) {
const xp = Math.floor(coins / 20) + kills
return {
xp,
gems: Math.floor(xp / 5)
}
}
export function buyFormats(amount: number) {
const values = ['<5', '<10', '10+', '20+', '50+', '100+', '200+', '500+', '1K+', '2K+', '5K+', '10K+', '20K+', '50K+', '100K+', '200K+', '500K+', '1M+', '2M+', '5M+', '10M+'];
const amountToNumber = (val: string) => {
const num = parseFloat(val.replace(/[<+KM]/g, ''));
return val.includes('M') ? num * 1000000 : val.includes('K') ? num * 1000 : num;
};
for (let i = 0; i < values.length; i++) {
const current = values[i];
const next = i + 1 < values.length ? values[i + 1] : null;
const currentVal = amountToNumber(current);
const nextVal = next ? amountToNumber(next) : Infinity;
if ((current.includes('<') && amount < currentVal) ||
(!current.includes('<') && amount >= currentVal && amount < nextVal)) {
return current;
}
}
return values[values.length - 1];
}