From c7546d9b7c0e05ff7651b64a4d67d108281cf5e4 Mon Sep 17 00:00:00 2001 From: Nathan Musoke Date: Wed, 28 Jun 2023 14:46:29 -0400 Subject: [PATCH] refactor: match headers in CSVs to interfaces There is redundant information in the mappings from column titles in our CSVs to field names of the corresponding interfaces. Simplify this by renaming the columns to match the fields they are written to. --- src/data/aid.csv | 2 +- src/data/boulder.csv | 4 ++-- src/data/csvtojson.ts | 47 +++++++++---------------------------------- src/data/ice.csv | 2 +- src/data/routes.csv | 4 ++-- 5 files changed, 15 insertions(+), 44 deletions(-) diff --git a/src/data/aid.csv b/src/data/aid.csv index bf17f0c..f7dd5de 100644 --- a/src/data/aid.csv +++ b/src/data/aid.csv @@ -1,4 +1,4 @@ -Score,Aid +score,aid 0,A0 1,A0 2,A0 diff --git a/src/data/boulder.csv b/src/data/boulder.csv index 8fa4d0d..ec91b33 100644 --- a/src/data/boulder.csv +++ b/src/data/boulder.csv @@ -1,4 +1,4 @@ -Score,V Scale,Font Scale +score,v,font 0,VB-,1a 1,VB-,1a 2,VB-,1a+ @@ -106,4 +106,4 @@ Score,V Scale,Font Scale 104,V21,9c 105,V21,9c 106,V22,9c+ -107,V22,9c+ \ No newline at end of file +107,V22,9c+ diff --git a/src/data/csvtojson.ts b/src/data/csvtojson.ts index fa9aaf8..92b64b6 100644 --- a/src/data/csvtojson.ts +++ b/src/data/csvtojson.ts @@ -8,7 +8,7 @@ import path from 'path' const dataDir = path.join(process.cwd(), 'src', 'data') -async function getData (pathCsv: any, pathJson, parseRow): Promise { +async function getData (pathCsv: any, pathJson): Promise { const data: T[] = [] return await new Promise((resolve, reject) => { fs.createReadStream(pathCsv) @@ -16,9 +16,11 @@ async function getData (pathCsv: any, pathJson, parseRow): Promise { console.warn('error in parsing:', error) reject(error) }) - .pipe(csv()) + .pipe(csv({ + mapValues: ({ header, index, value }) => { return header === 'score' ? parseInt(value) : value } + })) .on('data', (row) => { - data.push(parseRow(row)) + data.push(row) }) .on('end', () => { const parsedData = JSON.stringify(data) @@ -31,47 +33,16 @@ async function getData (pathCsv: any, pathJson, parseRow): Promise { const CSV_PATH_BOULDER = path.join(dataDir, 'boulder.csv') const JSON_PATH_BOULDER = path.join(dataDir, 'boulder.json') -function parseRowBoulder (row): Object { - return { - score: parseInt(row.Score, 10), - v: row['V Scale'], - font: row['Font Scale'] - } -} -export const BOULDER_GRADE_TABLE: Promise = getData(CSV_PATH_BOULDER, JSON_PATH_BOULDER, parseRowBoulder) +export const BOULDER_GRADE_TABLE: Promise = getData(CSV_PATH_BOULDER, JSON_PATH_BOULDER) const CSV_PATH_ROUTES = path.join(dataDir, 'routes.csv') const JSON_PATH_ROUTES = path.join(dataDir, 'routes.json') -function parseRowRoutes (row): Object { - return { - score: parseInt(row.Score, 10), - yds: row.Yosemite, - french: row.French, - uiaa: row.UIAA, - ewbank: row.Ewbank, - saxon: row.Saxon, - norwegian: row.Norwegian - } -} -export const ROUTE_GRADE_TABLE: Promise = getData(CSV_PATH_ROUTES, JSON_PATH_ROUTES, parseRowRoutes) +export const ROUTE_GRADE_TABLE: Promise = getData(CSV_PATH_ROUTES, JSON_PATH_ROUTES) const CSV_PATH_ICE = path.join(dataDir, 'ice.csv') const JSON_PATH_ICE = path.join(dataDir, 'ice.json') -function parseRowIce (row): Object { - return { - score: parseInt(row.Score, 10), - wi: row.WI, - ai: row.AI - } -} -export const ICE_GRADE_TABLE: Promise = getData(CSV_PATH_ICE, JSON_PATH_ICE, parseRowIce) +export const ICE_GRADE_TABLE: Promise = getData(CSV_PATH_ICE, JSON_PATH_ICE) const CSV_PATH_AID = path.join(dataDir, 'aid.csv') const JSON_PATH_AID = path.join(dataDir, 'aid.json') -function parseRowAid (row): Object { - return { - score: parseInt(row.Score, 10), - aid: row.Aid - } -} -export const AID_GRADE_TABLE: Promise = getData(CSV_PATH_AID, JSON_PATH_AID, parseRowAid) +export const AID_GRADE_TABLE: Promise = getData(CSV_PATH_AID, JSON_PATH_AID) diff --git a/src/data/ice.csv b/src/data/ice.csv index 39e4638..bcd7f2f 100644 --- a/src/data/ice.csv +++ b/src/data/ice.csv @@ -1,4 +1,4 @@ -Score,WI,AI +score,wi,ai 0,WI1,AI1 1,WI1,AI1 2,WI1,AI1 diff --git a/src/data/routes.csv b/src/data/routes.csv index 4e1c71d..b2e8afd 100644 --- a/src/data/routes.csv +++ b/src/data/routes.csv @@ -1,4 +1,4 @@ -Score,Yosemite,French,UIAA,Ewbank,Saxon,Norwegian +score,yds,french,uiaa,ewbank,saxon,norwegian 0,5.0,1a,1,1,1,1- 1,5.0,1a,1,1,1,1- 2,5.0,1a+,1,1,1,1- @@ -106,4 +106,4 @@ Score,Yosemite,French,UIAA,Ewbank,Saxon,Norwegian 104,5.15d,9c,12-,39,13c,11 105,5.15d,9c,12-,39,13c,11 106,5.16a,9c+,12,40,13c,11+ -107,5.16a,9c+,12,40,13c,11+ \ No newline at end of file +107,5.16a,9c+,12,40,13c,11+