-
Notifications
You must be signed in to change notification settings - Fork 6
/
CrewTimerUtils.ts
232 lines (218 loc) · 6.85 KB
/
CrewTimerUtils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* Capitalize the first letter of a string.
* @param str
* @returns The original string with the first letter capitalized.
*/
export const capitalizeFirstLetter = (str: string): string => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
export const uppercaseLastLetter = (str: string): string => {
return str.slice(0, -1) + str.slice(-1).toUpperCase();
};
/**
* Given an event name, detect the gender.
* @param eventName
* @returns 'Men', 'Women', or 'Unknown'
*/
export const genderFromEventName = (eventName: string) => {
eventName = eventName.toLowerCase();
const matches = ['women', ' wv', ' wjv', ' wn', 'mixed', 'mx', 'men', ' mv', ' mjv', ' mn'];
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
if (eventName.includes(match)) {
if (match.includes('x')) {
return 'Mixed';
}
return matches[i].startsWith('w') ? 'Womens' : 'Mens';
}
}
return 'Unknown';
};
/**
* Given an event name, determine the type of boat involved.
* For example, 8+, 4x, 2x, K1, C2, etc
*
* @param eventName The event name to inspect.
* @returns The Class detected. '' if none detected.
*/
export const boatClassFromName = (eventName: string) => {
eventName = eventName.toLowerCase();
const matches = [/ [ck]-?[1-4]/, / para[a-z]*/, / [1-8]x/, / [24]-/, / [48]+/];
for (let i = 0; i < matches.length; i++) {
const match = eventName.match(matches[i]);
if (match) {
const boatClass = match[0].trim().toUpperCase().replace('X', 'x').replace('-', '');
return boatClass;
}
}
return '';
};
/**
* Given an event name, determine the number of athletes (aka seats) involved.
* @param eventName The event name to inspect.
* @returns The number of athlets in each entry.
*/
export const numSeatsFromName = (eventName: string) => {
const boatClass = boatClassFromName(eventName);
const match = boatClass.match(/[1-9]/);
if (match) {
return Number(match[0]);
}
return 1;
};
/**
* Given an event name, determine the distance involved.
* @param eventName The event name to inspect.
* @returns (number) The detected distance.
*/
export const distanceFromName = (eventName: string) => {
const match = eventName.match(/ ([1-9][0-9][0-9]+)/); // 3 or more digits
if (match) {
return Number(match[1]);
}
return 0;
};
/**
* Define regex for detecting progression suffixes. Each
* entry is a triple of [Abbrev Prexix, RegEx to detect, RegEx for suffix | suffix, RefEx for timingIndex]
*/
const RegexTimingNames: [string, RegExp, RegExp | string][] = [
['QAD', /QAD[1-4]$/i, /[1-4]$/],
['QEH', /QEH[1-4]$/i, /[1-4]$/],
['QAD', /QF[1-4]+$/i, /[1-4]$/],
['SAB', /SAB[1-4]+$/i, /[1-4]$/],
['SAB', /S(emi)? *[1-4]+$/i, /[1-4]$/],
['SCD', /SCD[1-4]+$/i, /[1-4]$/],
['SEF', /SEF[1-4]+$/i, /[1-4]$/],
['SGH', /SGH[1-4]+$/i, /[1-4]$/],
['TF', /T(imed)? *F(inal)? *[A-D]$/i, /[A-D]$/i], // test before FA,FB
['TF', /T(imed)? *F(inal)?$/i, 'A'],
['H', /H(eat)? *[1-9][0-9]*$/i, /[1-9][0-9]*$/], // must be after QEH
['FA', /Final$/i, ''],
['FA', /F(inal)? *A$/i, ''],
['FB', /F(inal)? *B$/i, ''],
['FC', /F(inal)? *C$/i, ''],
['FD', /F(inal)? *D$/i, ''],
['FE', /F(inal)? *E$/i, ''],
['FF', /F(inal)? *F$/i, ''],
['FG', /F(inal)? *G$/i, ''],
['FH', /F(inal)? *H$/i, ''],
['TT', /T(ime)? *T(rial)?$/i, '1'],
['TT', /T(ime)? *T(rial)?[1-9][0-9]*$/i, /[1-9][0-9]*$/],
// unsupported
['Err: Must be QAD1-4', /QAD$/i, ''],
['Err: Must be QEH1-4', /QEH$/i, ''],
['Err', /SAB$/i, 'Must be SAB11-4'],
['Err', /SCD$/i, 'Must be SCD1-4'],
];
/** Given an event name as '1 Womens Varsity H1', separate into
* eventName = 'Womens Varsity'
* bracket = 'H1'
* bracketType = 'H'
*
* @param name The Event Name extracted from the spreadsheet
* @returns {eventName: string, bracket: string bracketType: string, bracketIndex: number}
*/
const decodeEventName = (name: string, eventNum: string) => {
let bracket = 'FA';
let bracketType = 'FA';
let bracketIndex = '1';
let eventName = name.substring(eventNum.length); // remove event num
// Try each RegEx pattern until a match is found
for (let i = 0; i < RegexTimingNames.length; i++) {
const [code, identifyRegex, indexRegex] = RegexTimingNames[i];
const index = eventName.search(identifyRegex);
if (index < 0) {
continue;
}
// We have a match!
if (typeof indexRegex === 'string') {
bracketIndex = indexRegex;
} else {
bracketIndex = eventName.match(indexRegex)?.[0] || '1';
}
bracketType = code;
bracket = `${code}${bracketIndex}`.toUpperCase();
if (bracketType === 'TF') {
bracketType = `${code}${bracketIndex}`;
bracket = bracketType;
}
eventName = eventName.substring(0, index);
break;
}
eventName = eventName.trim();
const result = {
eventName,
bracket,
bracketType,
};
// console.log(JSON.stringify(result));
return result;
};
/**
* Determine if an event is a final and which level (A, B, C etc).
* If the event is not a final, '' is returned.
*
* @param eventName
* @param eventNum
* @returns '' if not final, otherwise final level A, B, C etc
*/
export const getFinalLevel = (eventName: string, eventNum: string) => {
const { bracket, bracketType } = decodeEventName(eventName, eventNum);
return bracket === bracketType ? bracket.charAt(bracket.length - 1) : '';
};
/**
*
* @param eventName
* @param eventNum
* @returns true if the level is an A final
*/
export const isAFinal = (eventName: string, eventNum: string) => {
const finalLevel = getFinalLevel(eventName, eventNum);
return finalLevel === 'A';
};
/**
* Give a list of numbers, return an array representing 'place' where 1
* represents first place. The array does not need to be sorted.
*
* Entries with the same value will have the same place. For example, if two entries
* tie for second place they both get '2' and the next event after them will get '4'.
* Place '3' would be unused.
*
* @param inputList A list of numbers
* @param dir 'asc' or 'desc' to specify direction of sorted values
* @returns Array of places representing the placement of the input array
*/
export const genPlaces = (inputList: number[], dir: 'asc' | 'desc' = 'asc') => {
if (inputList.length === 0) {
return [];
}
const indexes = inputList.map((_val, i) => i);
indexes.sort((a, b) => {
if (dir === 'desc') {
const temp = a;
a = b;
b = temp;
}
if (inputList[a] < inputList[b]) {
return -1;
} else if (inputList[a] > inputList[b]) {
return 1;
} else {
return 0;
}
});
let lastVal = inputList[0];
let lastPlace = 1;
const places = new Array<number>(inputList.length);
indexes.forEach((index, place) => {
if (lastVal === inputList[index]) {
places[index] = lastPlace;
} else {
lastVal = inputList[index];
lastPlace = place + 1;
places[index] = lastPlace;
}
});
return places;
};