Skip to content

Commit

Permalink
refactor: match headers in CSVs to interfaces
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
musoke committed Jun 28, 2023
1 parent 2e6e532 commit c7546d9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/data/aid.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Score,Aid
score,aid
0,A0
1,A0
2,A0
Expand Down
4 changes: 2 additions & 2 deletions src/data/boulder.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Score,V Scale,Font Scale
score,v,font
0,VB-,1a
1,VB-,1a
2,VB-,1a+
Expand Down Expand Up @@ -106,4 +106,4 @@ Score,V Scale,Font Scale
104,V21,9c
105,V21,9c
106,V22,9c+
107,V22,9c+
107,V22,9c+
47 changes: 9 additions & 38 deletions src/data/csvtojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import path from 'path'

const dataDir = path.join(process.cwd(), 'src', 'data')

async function getData<T> (pathCsv: any, pathJson, parseRow): Promise<T[]> {
async function getData<T> (pathCsv: any, pathJson): Promise<T[]> {
const data: T[] = []
return await new Promise((resolve, reject) => {
fs.createReadStream(pathCsv)
.on('error', error => {
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)
Expand All @@ -31,47 +33,16 @@ async function getData<T> (pathCsv: any, pathJson, parseRow): Promise<T[]> {

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<Boulder[]> = getData(CSV_PATH_BOULDER, JSON_PATH_BOULDER, parseRowBoulder)
export const BOULDER_GRADE_TABLE: Promise<Boulder[]> = 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<Route[]> = getData(CSV_PATH_ROUTES, JSON_PATH_ROUTES, parseRowRoutes)
export const ROUTE_GRADE_TABLE: Promise<Route[]> = 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<IceGrade[]> = getData(CSV_PATH_ICE, JSON_PATH_ICE, parseRowIce)
export const ICE_GRADE_TABLE: Promise<IceGrade[]> = 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<AidGrade[]> = getData(CSV_PATH_AID, JSON_PATH_AID, parseRowAid)
export const AID_GRADE_TABLE: Promise<AidGrade[]> = getData(CSV_PATH_AID, JSON_PATH_AID)
2 changes: 1 addition & 1 deletion src/data/ice.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Score,WI,AI
score,wi,ai
0,WI1,AI1
1,WI1,AI1
2,WI1,AI1
Expand Down
4 changes: 2 additions & 2 deletions src/data/routes.csv
Original file line number Diff line number Diff line change
@@ -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-
Expand Down Expand Up @@ -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+
107,5.16a,9c+,12,40,13c,11+

0 comments on commit c7546d9

Please sign in to comment.