Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: infer GradeScale from grade #156

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
getScale,
getScore,
getScoreForSort,
inferScale,
isVScale,
convertGrade
} from './GradeParser'
Expand Down Expand Up @@ -294,6 +295,7 @@
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
Loading