-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
108 lines (93 loc) · 2.36 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
export const COLOR_VARIANTS = [
'primary',
'success',
'danger',
'warning',
'info',
'link',
'dark',
'light',
'white',
];
export const SIZES_ABBR = ['xxs', 'xs', 's', 'm', 'l', 'xl', 'xxl'];
export const SIZES = ['xsmall', 'small', 'medium', 'large'];
export const variantValidator = (variant) => {
return COLOR_VARIANTS.includes(variant) !== -1;
};
export function humanize(str) {
if (typeof str === 'string') {
const cleanStr = str.replace(/_/g, ' ');
const upperFirst = cleanStr.charAt(0).toUpperCase() + cleanStr.slice(1);
return upperFirst;
}
}
export function humanizeHero(str) {
const cleanStr = str.split('-');
const upperFirst = cleanStr.map(
(str) => str.charAt(0).toUpperCase() + str.slice(1)
);
return upperFirst.join(' ');
}
export function urlize(str) {
return str.toLowerCase().replace(/ /g, '-');
}
//
// Calendar Utils
//
/**
* Calculates the number of weeks in the current month.
*
* @param {number} month The month.
* @param {number} year The year.
* @returns {number} The number of weeks in the month.
*/
export function weeksInMonth(month, year) {
// month = # (0 indexed), year = #
const firstDay = new Date(year, month, 1).getDay(); // day of the week of 1st
const daysInMonth = new Date(year, month + 1, 0).getDate();
return Math.ceil((firstDay + daysInMonth) / 7);
}
/**
* Generates the string representation of a date.
*
* @param {number} month The integer representation of the month.
* @param {number} day The integer representation of the day of the month.
* @param {number} year The integer representation of the year.
* @returns {string} The date as a string.
*/
export function getStringDate(month, day, year) {
const mm = month < 10 ? '0' + month : month;
const dd = day < 10 ? '0' + day : day;
return year + '-' + mm + '-' + dd;
}
export const ALL_MONTHS = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
export const ALL_DAYS_OF_WEEK = [
'Sun',
'Mon',
'Tues',
'Weds',
'Thurs',
'Fri',
'Sat',
];
export const DISPLAY_YEAR = new Date().getFullYear();
export const DISPLAY_MONTH = new Date().getMonth() + 1;
export const DISPLAY_DAY = new Date().getDate();
export const TODAYS_DATE = getStringDate(
new Date().getMonth() + 1,
new Date().getDate(),
new Date().getFullYear()
);