From 10e40688fea9036e74dd41a4f8f57055cb63766b Mon Sep 17 00:00:00 2001 From: Nathan Musoke Date: Tue, 18 Jul 2023 18:05:10 -0400 Subject: [PATCH] WIP: refactor: Import contexts from sandbag openbeta-graphql defines grade contexts used to map climbs to grade scales. The front end needs to do this too, so the existing functionality was copied to sandbag. Reuse that instead of having parallel implementations.. --- src/GradeUtils.ts | 194 +--------------------------------------------- 1 file changed, 2 insertions(+), 192 deletions(-) diff --git a/src/GradeUtils.ts b/src/GradeUtils.ts index d869fdfa..73ab5bab 100644 --- a/src/GradeUtils.ts +++ b/src/GradeUtils.ts @@ -1,103 +1,7 @@ -import { getScale, GradeScales, GradeScalesTypes } from '@openbeta/sandbag' -import isoCountries from 'i18n-iso-countries' +import { getScale, GradeScalesTypes, GradeContexts, gradeContextToGradeScales, getCountriesDefaultGradeContext } from '@openbeta/sandbag' 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> = { - [GradeContexts.AU]: { - trad: GradeScales.EWBANK, - sport: GradeScales.EWBANK, - bouldering: GradeScales.VSCALE, - tr: GradeScales.EWBANK, - deepwatersolo: GradeScales.EWBANK, - alpine: GradeScales.YDS, - mixed: GradeScales.YDS, - aid: GradeScales.AID, - snow: GradeScales.YDS, // is this the same as alpine? - ice: GradeScales.WI - }, - [GradeContexts.US]: { - trad: GradeScales.YDS, - sport: GradeScales.YDS, - bouldering: GradeScales.VSCALE, - tr: GradeScales.YDS, - deepwatersolo: GradeScales.YDS, - alpine: GradeScales.YDS, - mixed: GradeScales.YDS, - aid: GradeScales.AID, - snow: GradeScales.YDS, // is this the same as alpine? - ice: GradeScales.WI - }, - [GradeContexts.FR]: { - trad: GradeScales.FRENCH, - sport: GradeScales.FRENCH, - bouldering: GradeScales.FONT, - tr: GradeScales.FRENCH, - deepwatersolo: GradeScales.FRENCH, - alpine: GradeScales.FRENCH, - mixed: GradeScales.FRENCH, - aid: GradeScales.AID, - snow: GradeScales.FRENCH, // is this the same as alpine? - ice: GradeScales.WI - }, - [GradeContexts.SA]: { - trad: GradeScales.FRENCH, - sport: GradeScales.FRENCH, - bouldering: GradeScales.FONT, - tr: GradeScales.FRENCH, - deepwatersolo: GradeScales.FRENCH, - alpine: GradeScales.FRENCH, - mixed: GradeScales.FRENCH, - aid: GradeScales.AID, - snow: GradeScales.FRENCH, // SA does not have a whole lot of snow - ice: GradeScales.WI - }, - [GradeContexts.UIAA]: { - trad: GradeScales.UIAA, - sport: GradeScales.UIAA, - bouldering: GradeScales.FONT, - tr: GradeScales.UIAA, - deepwatersolo: GradeScales.FRENCH, - alpine: GradeScales.UIAA, - mixed: GradeScales.UIAA, // TODO: change to MI scale, once added - aid: GradeScales.UIAA, - snow: GradeScales.UIAA, // TODO: remove `snow` since it duplicates `ice` - ice: GradeScales.WI - } -} +export { GradeContexts, gradeContextToGradeScales, getCountriesDefaultGradeContext } /** * Convert a human-readable grade to the appropriate grade object. @@ -127,100 +31,6 @@ export const createGradeObject = (gradeStr: string, disciplines: DisciplineType }, undefined) } -/** - * A record of all countries with a default grade context that is not US - */ -const COUNTRIES_DEFAULT_NON_US_GRADE_CONTEXT: Record = { - 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', 'deepwatersolo', 'alpine', 'snow', 'ice', 'mixed', 'aid', 'tr'] /**