Skip to content

Commit

Permalink
feat: infer grade scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugzuzg committed Sep 11, 2023
1 parent d5c4170 commit 3c7f0ba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/GradeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export const convertGrade = (
return toScale.getGrade(toScore)
}

export const inferScale = (grade: string): GradeScalesTypes | null => {
const matchedScales = Object.values(scales)
.map(scale => [scale.name, scale.isType(grade)] as const)
.filter(v => v[1])
if (matchedScales.length === 1) return matchedScales[0][0]
return null
}

export const isVScale = (grade: string): boolean => {
const scale = scales[GradeScales.VSCALE]
if (scale == null) {
Expand Down
13 changes: 12 additions & 1 deletion src/__tests__/GradeParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getScoreForSort, convertGrade, getScale } from '../GradeParser'
import { getScoreForSort, convertGrade, getScale, inferScale } from '../GradeParser'
import { GradeScales } from '../GradeScale'
import { VScale, Font, YosemiteDecimal, French, Saxon, AI, WI } from '../scales'

Expand Down Expand Up @@ -322,4 +322,15 @@ describe('Grade Scales', () => {
)
})
})

describe('inferScale', () => {
test.each([
[GradeScales.YDS, '5.10'],
[null, '6a'],
[null, 'abcdef'],
[GradeScales.VSCALE, 'V7']
])('detects %s scale for %s', (scale, grade) => {
expect(inferScale(grade)).toEqual(scale)
})
})
})
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getScale,
getScore,
getScoreForSort,
inferScale,
isVScale,
convertGrade
} from './GradeParser'
Expand Down Expand Up @@ -294,6 +295,7 @@ export {
getScoreForSort,
isVScale,
getScale,
inferScale,

Check warning on line 298 in src/index.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
GradeScales,
GradeScalesTypes,
GradeBands,
Expand Down
9 changes: 3 additions & 6 deletions src/scales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import AI from './ai'
import Aid from './aid'
import WI from './wi'
import UIAA from './uiaa'
import GradeScale, { GradeScales } from '../GradeScale'
import { GradeScales } from '../GradeScale'
export { Aid, VScale, Font, YosemiteDecimal, French, Saxon, UIAA, Ewbank, AI, WI, Norwegian }

export interface Boulder {
Expand Down Expand Up @@ -39,10 +39,7 @@ export interface AidGrade {
aid: string
}

export const scales: Record<
typeof GradeScales[keyof typeof GradeScales],
GradeScale | null
> = {
export const scales = {
[GradeScales.VSCALE]: VScale,
[GradeScales.YDS]: YosemiteDecimal,
[GradeScales.FONT]: Font,
Expand All @@ -54,4 +51,4 @@ GradeScale | null
[GradeScales.AI]: AI,
[GradeScales.WI]: WI,
[GradeScales.AID]: Aid
}
} as const

0 comments on commit 3c7f0ba

Please sign in to comment.