forked from OpenBeta/openbeta-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradeUtils.ts
232 lines (223 loc) · 6.47 KB
/
GradeUtils.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
import { getScale, GradeScales, GradeScalesTypes } from '@openbeta/sandbag'
import isoCountries from 'i18n-iso-countries'
import { DisciplineType, ClimbGradeContextType } from './db/ClimbTypes.js'
/**
* Grade systems have minor variations between countries. gradeContext is a
* short abbreviated string that identifies the context in which the grade was assigned
* and should signify a regional or national variation that may be considered within
* grade comparisons.
*
* Todo: move this to @openbeta/sandbag library
*/
export enum GradeContexts {
/** Alaska (United States) */
ALSK = 'ALSK',
/** Australia */
AU = 'AU',
BRZ = 'BRZ',
FIN = 'FIN',
FR = 'FR',
HK = 'HK',
NWG = 'NWG',
POL = 'POL',
SA = 'SA',
/** Sweden */
SWE = 'SWE',
SX = 'SX',
UIAA = 'UIAA',
/** United Kingdom */
UK = 'UK',
/** United States of Ameria */
US = 'US'
}
/**
* A conversion from grade context to corresponding grade type / scale
* Todo: move this to @openbeta/sandbag
*/
export const gradeContextToGradeScales: Partial<Record<GradeContexts, ClimbGradeContextType>> = {
[GradeContexts.US]: {
trad: GradeScales.YDS,
sport: GradeScales.YDS,
bouldering: GradeScales.VSCALE,
tr: GradeScales.YDS,
alpine: GradeScales.YDS,
mixed: GradeScales.YDS,
aid: GradeScales.YDS,
snow: GradeScales.YDS, // is this the same as alpine?
ice: GradeScales.YDS // is this the same as alpine?
},
[GradeContexts.FR]: {
trad: GradeScales.FRENCH,
sport: GradeScales.FRENCH,
bouldering: GradeScales.FONT,
tr: GradeScales.FRENCH,
alpine: GradeScales.FRENCH,
mixed: GradeScales.FRENCH,
aid: GradeScales.FRENCH,
snow: GradeScales.FRENCH, // is this the same as alpine?
ice: GradeScales.FRENCH // is this the same as alpine?
},
[GradeContexts.SA]: {
trad: GradeScales.FRENCH,
sport: GradeScales.FRENCH,
bouldering: GradeScales.FONT,
tr: GradeScales.FRENCH,
alpine: GradeScales.FRENCH,
mixed: GradeScales.FRENCH,
aid: GradeScales.FRENCH,
snow: GradeScales.FRENCH, // SA does not have a whole lot of snow
ice: GradeScales.FRENCH // SA does not have a whole lot of ice
}
}
/**
* Convert a human-readable grade to the appropriate grade object.
* @param gradeStr human-readable, eg: '5.9' or '5c'.
* @param disciplines the climb disciplines
* @param context grade context
* @returns grade object
*/
export const createGradeObject = (gradeStr: string, disciplines: DisciplineType | undefined, context: ClimbGradeContextType): Partial<Record<GradeScalesTypes, string>> | undefined => {
if (disciplines == null) return undefined
return Object.keys(disciplines).reduce<Partial<Record<GradeScalesTypes, string>> | undefined>((acc, curr) => {
if (disciplines[curr] === true) {
const scaleTxt = context[curr]
const scaleApi = getScale(scaleTxt)
if (scaleApi != null && !(scaleApi.getScore(gradeStr) < 0)) {
// only assign valid grade
if (acc == null) {
acc = {
[scaleTxt]: gradeStr
}
} else {
acc[scaleTxt] = gradeStr
}
}
}
return acc
}, undefined)
}
/**
* A record of all countries with a default grade context that is not US
*/
const COUNTRIES_DEFAULT_NON_US_GRADE_CONTEXT: Record<string, GradeContexts> = {
AND: GradeContexts.FR,
ATF: GradeContexts.FR,
AUS: GradeContexts.AU,
AUT: GradeContexts.UIAA,
AZE: GradeContexts.UIAA,
BEL: GradeContexts.FR,
BGR: GradeContexts.UIAA,
BIH: GradeContexts.FR,
BLR: GradeContexts.UIAA,
BRA: GradeContexts.BRZ,
BWA: GradeContexts.SA,
CHE: GradeContexts.FR,
CUB: GradeContexts.FR,
CZE: GradeContexts.UIAA,
DEU: GradeContexts.UIAA,
DNK: GradeContexts.UIAA,
EGY: GradeContexts.FR,
ESP: GradeContexts.FR,
EST: GradeContexts.FR,
FIN: GradeContexts.FIN,
FRA: GradeContexts.FR,
GBR: GradeContexts.UK,
GRC: GradeContexts.FR,
GUF: GradeContexts.FR,
HKG: GradeContexts.HK,
HRV: GradeContexts.FR,
HUN: GradeContexts.UIAA,
IOT: GradeContexts.UK,
IRL: GradeContexts.UK,
ITA: GradeContexts.FR,
JEY: GradeContexts.UK,
JOR: GradeContexts.FR,
KEN: GradeContexts.UK,
KGZ: GradeContexts.FR,
LAO: GradeContexts.FR,
LIE: GradeContexts.FR,
LSO: GradeContexts.SA,
LTU: GradeContexts.FR,
LUX: GradeContexts.FR,
LVA: GradeContexts.FR,
MAR: GradeContexts.FR,
MCO: GradeContexts.FR,
MDA: GradeContexts.FR,
MDG: GradeContexts.FR,
MKD: GradeContexts.FR,
MLT: GradeContexts.FR,
MNE: GradeContexts.UIAA,
MYS: GradeContexts.FR,
NAM: GradeContexts.SA,
NCL: GradeContexts.FR,
NLD: GradeContexts.FR,
NOR: GradeContexts.NWG,
NZL: GradeContexts.AU,
PER: GradeContexts.FR,
PNG: GradeContexts.AU,
POL: GradeContexts.POL,
PRT: GradeContexts.FR,
PYF: GradeContexts.FR,
ROU: GradeContexts.FR,
RUS: GradeContexts.FR,
SGP: GradeContexts.FR,
SRB: GradeContexts.FR,
SVK: GradeContexts.UIAA,
SVN: GradeContexts.FR,
SWE: GradeContexts.SWE,
THA: GradeContexts.FR,
TON: GradeContexts.AU,
TUN: GradeContexts.FR,
TUR: GradeContexts.FR,
UGA: GradeContexts.SA,
UKR: GradeContexts.FR,
VNM: GradeContexts.FR,
ZAF: GradeContexts.SA
}
/**
*
* @returns all countries with their default grade context
*/
export const getCountriesDefaultGradeContext = (): { [x: string]: GradeContexts } => {
const countries = { ...COUNTRIES_DEFAULT_NON_US_GRADE_CONTEXT }
for (const alpha3Code in isoCountries.getAlpha3Codes()) {
// Any country not found will have a US Grade Context
if (!(alpha3Code in countries)) {
countries[alpha3Code] = GradeContexts.US
}
}
return countries
}
export const validDisciplines = ['trad', 'sport', 'bouldering', 'alpine', 'snow', 'ice', 'mixed', 'aid', 'tr']
/**
* Perform runtime validation of climb discipline object
* @param disciplineObj IClimbType
*/
export const sanitizeDisciplines = (disciplineObj: Partial<DisciplineType> | undefined): DisciplineType | undefined => {
if (disciplineObj == null) return undefined
const output = validDisciplines.reduce((acc, current) => {
if (disciplineObj?.[current] != null) {
acc[current] = disciplineObj[current]
} else {
acc[current] = false
}
return acc
}, {})
// @ts-expect-error
if (disciplineObj?.boulder != null) {
// @ts-expect-error
output.bouldering = disciplineObj.boulder
}
return output as DisciplineType
}
export const defaultDisciplines = (): DisciplineType => ({
trad: false,
sport: false,
bouldering: false,
alpine: false,
snow: false,
ice: false,
mixed: false,
aid: false,
tr: false
})