Files
faunosteo/packages/anatomy/src/legacy/vertebrae.ts
T

52 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Reads the legacy vertebral formula from the source text of
* apps/app/src/domain/species/equine/vertebrae.ts. The file is parsed as
* text, not imported: it depends on the app's '@/…' alias, which this
* package does not resolve.
*/
export interface LegacyVertebralFormula {
counts: Record<string, number>
/** "varies A–B" range from the source comment, when stated. */
variesRange: string | null
}
const REGION_ORDER = ['C', 'T', 'L', 'S', 'Cd'] as const
export function parseVertebralFormula(source: string): LegacyVertebralFormula {
const body = /=\s*\{([^}]*)\}/.exec(source)?.[1]
if (!body) throw new Error('vertebrae.ts: formula object not found')
const counts: Record<string, number> = {}
for (const match of body.matchAll(/(\w+)\s*:\s*(\d+)/g)) {
counts[match[1]!] = Number(match[2])
}
for (const code of REGION_ORDER) {
if (!(code in counts))
throw new Error(`vertebrae.ts: region ${code} missing`)
}
const range = /varies\s+(\d+)\s*[–-]\s*(\d+)/.exec(source)
return {
counts,
variesRange: range ? `${range[1]}–${range[2]}` : null,
}
}
export const VERTEBRAL_REGION_ORDER = REGION_ORDER
const REGION_NAMES: Readonly<Record<string, { fr: string; en: string }>> = {
C: { fr: 'Cervicales', en: 'Cervical' },
T: { fr: 'Thoraciques', en: 'Thoracic' },
L: { fr: 'Lombaires', en: 'Lumbar' },
S: { fr: 'Sacrales', en: 'Sacral' },
Cd: { fr: 'Caudales', en: 'Caudal' },
}
/** Ordered C → Cd, the order of the legacy picker tabs. */
export function vertebralRegions(formula: LegacyVertebralFormula) {
return REGION_ORDER.map((code) => ({
code,
name: { ...REGION_NAMES[code]! },
count: formula.counts[code]!,
}))
}