-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.ts
121 lines (115 loc) · 3.79 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
import isToday from 'date-fns/isToday';
import isYesterday from 'date-fns/isYesterday';
/**
* @name Get contrasting text color
* @description Get contrasting text color from a text color
* @param bgColor Background color of text.
* @returns contrasting text color
*/
export const getContrastingTextColor = (bgColor: string): string => {
const color = bgColor.replace('#', '');
const r = parseInt(color.slice(0, 2), 16);
const g = parseInt(color.slice(2, 4), 16);
const b = parseInt(color.slice(4, 6), 16);
// http://stackoverflow.com/a/3943023/112731
return r * 0.299 + g * 0.587 + b * 0.114 > 186 ? '#000000' : '#FFFFFF';
};
/**
* @name Get formatted date
* @description Get date in today, yesterday or any other date format
* @param date date
* @param todayText Today text
* @param yesterdayText Yesterday text
* @returns formatted date
*/
export const formatDate = ({
date,
todayText,
yesterdayText,
}: {
date: string;
todayText: string;
yesterdayText: string;
}) => {
const dateValue = new Date(date);
if (isToday(dateValue)) return todayText;
if (isYesterday(dateValue)) return yesterdayText;
return date;
};
/**
* @name formatTime
* @description Format time to Hour, Minute and Second
* @param timeInSeconds number
* @returns formatted time
*/
export const formatTime = (timeInSeconds: number) => {
let formattedTime = '';
if (timeInSeconds >= 60 && timeInSeconds < 3600) {
const minutes = Math.floor(timeInSeconds / 60);
formattedTime = `${minutes} Min`;
const seconds = minutes === 60 ? 0 : Math.floor(timeInSeconds % 60);
return formattedTime + `${seconds > 0 ? ' ' + seconds + ' Sec' : ''}`;
}
if (timeInSeconds >= 3600 && timeInSeconds < 86400) {
const hours = Math.floor(timeInSeconds / 3600);
formattedTime = `${hours} Hr`;
const minutes =
timeInSeconds % 3600 < 60 || hours === 24
? 0
: Math.floor((timeInSeconds % 3600) / 60);
return formattedTime + `${minutes > 0 ? ' ' + minutes + ' Min' : ''}`;
}
if (timeInSeconds >= 86400) {
const days = Math.floor(timeInSeconds / 86400);
formattedTime = `${days} Day`;
const hours =
timeInSeconds % 86400 < 3600 || days >= 364
? 0
: Math.floor((timeInSeconds % 86400) / 3600);
return formattedTime + `${hours > 0 ? ' ' + hours + ' Hr' : ''}`;
}
return `${Math.floor(timeInSeconds)} Sec`;
};
/**
* @name trimContent
* @description Trim a string to max length
* @param content String to trim
* @param maxLength Length of the string to trim, default 1024
* @param ellipsis Boolean to add dots at the end of the string, default false
* @returns trimmed string
*/
export const trimContent = (
content: string = '',
maxLength: number = 1024,
ellipsis: boolean = false
) => {
let trimmedContent = content;
if (content.length > maxLength) {
trimmedContent = content.substring(0, maxLength);
}
if (ellipsis) {
trimmedContent = trimmedContent + '...';
}
return trimmedContent;
};
/**
* @name convertSecondsToTimeUnit
* @description Convert seconds to time unit
* @param seconds number
* @param unitNames object
* @returns time and unit
* @example
* convertToUnit(60, { minute: 'm', hour: 'h', day: 'd' }); // { time: 1, unit: 'm' }
* convertToUnit(60, { minute: 'Minutes', hour: 'Hours', day: 'Days' }); // { time: 1, unit: 'Minutes' }
*/
export const convertSecondsToTimeUnit = (
seconds: number,
unitNames: { minute: string; hour: string; day: string }
) => {
if (seconds === null || seconds === 0) return { time: '', unit: '' };
if (seconds < 3600)
return { time: Number((seconds / 60).toFixed(1)), unit: unitNames.minute };
if (seconds < 86400)
return { time: Number((seconds / 3600).toFixed(1)), unit: unitNames.hour };
return { time: Number((seconds / 86400).toFixed(1)), unit: unitNames.day };
};