@randsum/notation
Version:
A flexible, type-safe dice notation parser and validator
33 lines (28 loc) • 852 B
text/typescript
import { optionsConverter } from '@randsum/core'
import { isDiceNotation } from './isDiceNotation'
import type { ValidationResult } from './types'
import { notationToOptions } from './utils/notationToOptions'
export function validateNotation(notation: string): ValidationResult {
if (!isDiceNotation(notation)) {
return {
valid: false,
description: [],
digested: {},
type: 'invalid'
}
}
const digested = notationToOptions(notation)
return {
valid: true,
digested,
notation: optionsConverter.toNotation(digested),
type: caclulateDieType(digested.sides),
description: optionsConverter.toDescription(digested)
} as ValidationResult
}
function caclulateDieType(sides: number | string[]): 'custom' | 'numerical' {
if (Array.isArray(sides)) {
return 'custom'
}
return 'numerical'
}