-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.tsx
115 lines (95 loc) · 2.85 KB
/
helpers.tsx
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
import { collectionNameToUrl, createDateFromDDMMYYYY } from "./CMS/helpers";
export function truncate(str: string, n: number){
if (!str) return '';
// return (str.length > n) ? str.substr(0, n-1) + '…' : str;
return (str.length > n) ? str.substr(0, n-1) + "..." : str;
};
export function handleEvents(events: any[]): any[] {
// Filter out past meetings.
var meetingDates = events.map((m: any[]) => m[0]);
meetingDates = meetingDates.map((m: string) => createDateFromDDMMYYYY(m));
meetingDates.forEach((m: Date, i: number) => {
events[i][0] = m;
})
// console.log("futureMeetings 0: ", futureMeetings);
for (var i in events) {
if (isNaN(events[i][0]) || !events[i][0].toString().length) events[i][0] = new Date('9999 09 09')
};
var d = new Date(); d.setDate(d.getDate()-1);
events = events.filter((f: any[]) => f[0] >= d);
return events;
}
export function dateDisplay(date: Date): string {
if (!date) return '';
return `${date.getFullYear()} ${MONTHS[date.getMonth()]} ${date.getDate()}`;
}
const MONTHS = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
export function addDays(date: Date, daysToAdjust: number): Date {
date.setDate(date.getDate() + daysToAdjust);
return date;
}
export function isDateInPast(date: Date): boolean {
return date < new Date();
}
export function firebaseTimestamp2Date(o: any): Date {
if (!o.seconds) {
return new Date(o);
}
return new Date(o.seconds * 1000);
}
// https://stackoverflow.com/questions/43100718/typescript-enum-to-object-array
// Needs to match {label: , value: } format for react-select dropdown.
export function enums2Map(_enum: any) {
const entries = Object.entries(_enum);
const out = [];
for (var entry of entries) {
out.push({
value: entry[0],
label: entry[1]
});
}
return out;
}
// Random N indexes from array.
export const randomNIndexes = (arr: any[], n: number): any[] => {
const shuffled = arr.sort(() => 0.5 - Math.random());
return shuffled.slice(0, n);
}
export const isValidUrl = (url: string) => {
try {
new URL(url);
} catch (e) {
console.error(e);
return false;
}
return true;
};
// Used for enums
export const enum2Object = (_enum: any, forceCollectionUrlName: boolean): any => {
const out: any = {};
// Swap the keys around.
// Also preserve the original structure/
for (var o of Object.entries(_enum)) {
var key = o[0];
var value: string = o[1] as string;
if (forceCollectionUrlName) {
value = collectionNameToUrl(value);
}
out[value] = key;
out[key] = value;
}
return out;
}