Skip to content

Notation API Reference

All public exports from @randsum/notation. Some internal helpers (like listOfNotations) are omitted. Three functions (isDiceNotation, notation, validateNotation), NotationParseError, and all types are also re-exported from @randsum/roller. All other functions require importing from @randsum/notation directly.

Type guard that returns true if the string is valid dice notation.

import { isDiceNotation } from '@randsum/notation'
isDiceNotation('4d6L') // true — value is typed as DiceNotation
isDiceNotation('hello') // false

Signature:

function isDiceNotation(value: string): value is DiceNotation

Assert a string is valid notation or throw NotationParseError.

function notation(value: string): DiceNotation

Parse a notation string into structured options. Accepts any string (validate first with isDiceNotation for safety). Returns an array (one entry per roll group).

import { notationToOptions, isDiceNotation } from '@randsum/notation'
if (isDiceNotation('4d6L+2')) {
const [options] = notationToOptions('4d6L+2')
// { sides: 6, quantity: 4, modifiers: { drop: { lowest: 1 }, plus: 2 } }
}

Signature:

function notationToOptions(notation: string): ParsedNotationOptions[]

Validate with detailed result. Returns ValidValidationResult or InvalidValidationResult.

import { validateNotation } from '@randsum/notation'
const result = validateNotation('4d6L')
if (result.valid) {
result.notation // DiceNotation[]
result.options // ParsedNotationOptions[]
} else {
result.error // ValidationErrorInfo
}

Signature:

function validateNotation(notation: string): ValidationResult

Suggest a corrected version of invalid notation. Returns undefined if no fix is available.

import { suggestNotationFix } from '@randsum/notation'
suggestNotationFix('d6') // '1d6'
suggestNotationFix('46') // '4d6'
suggestNotationFix('xyz') // undefined

Signature:

function suggestNotationFix(notation: string): string | undefined

The base regex pattern for matching NdS notation. Exported for tooling that needs to detect dice notation in arbitrary text.

const coreNotationPattern: RegExp

Convert a RollOptions object to a notation string.

import { optionsToNotation } from '@randsum/notation'
optionsToNotation({ sides: 6, quantity: 4, modifiers: { drop: { lowest: 1 } } })
// '4d6L'
function optionsToNotation(options: RollOptions): DiceNotation

Generate a human-readable description. Returns an array of description strings.

function optionsToDescription<T = string>(options: RollOptions<T>): string[]

Resolve a die configuration’s sides to a numeric value and optional custom faces array.

function optionsToSidesFaces<T>(options: RollOptions<T>): { sides: number; faces?: T[] }

Convert modifier options to their notation suffix.

function modifiersToNotation(modifiers: ModifierOptions | undefined): string

Convert modifier options to an array of human-readable description strings.

function modifiersToDescription(modifiers: ModifierOptions | undefined): string[]

Join an array of numbers into a human-readable list (e.g. "1, 2, and 3").

function formatHumanList(values: number[]): string

Utilities for working with the {<3,>18} comparison syntax used in modifiers.

Parse a condition string into a ComparisonOptions object.

function parseComparisonNotation(conditionString: string): ComparisonOptions

Check if a ComparisonOptions has any active conditions.

function hasConditions(options: ComparisonOptions): boolean

Format a ComparisonOptions back into notation fragments.

function formatComparisonNotation(options: ComparisonOptions): string[]

Format a ComparisonOptions as human-readable description fragments.

function formatComparisonDescription(options: ComparisonOptions): string[]

Parse notation into typed tokens for syntax highlighting or UI display.

function tokenize(notation: string): readonly Token[]
interface Token {
readonly text: string
readonly type: TokenType
readonly start: number
readonly end: number
readonly description: string
}
type TokenType =
| 'core' | 'dropLowest' | 'dropHighest' | 'keepHighest' | 'keepLowest'
| 'reroll' | 'explode' | 'compound' | 'penetrate' | 'cap' | 'replace'
| 'unique' | 'countSuccesses' | 'dropCondition' | 'plus' | 'minus'
| 'multiply' | 'multiplyTotal' | 'unknown'

Each schema is a NotationSchema defining the regex pattern, parser, and formatter for one modifier type. Used by the roller’s modifier registry.

ExportNameNotationExample
capSchemacapC{...}C{<1,>6}
dropSchemadropL / H / D{...}4d6L
keepSchemakeepK / kl4d6K3
replaceSchemareplaceV{...}V{1=6}
rerollSchemarerollR{...}R{<3}
explodeSchemaexplode!3d6!
compoundSchemacompound!!3d6!!
penetrateSchemapenetrate!p3d6!p
uniqueSchemauniqueU4d20U
countSuccessesSchemacountSuccessesS{...}5d10S{>7}
multiplySchemamultiply*N2d6*2
plusSchemaplus+N1d20+5
minusSchemaminus-N1d20-2
multiplyTotalSchemamultiplyTotal**N2d6+3**2
interface NotationSchema<TOptions = unknown> {
name: keyof ModifierOptions
priority: number
pattern: RegExp
parse: (notation: string) => Partial<ModifierOptions>
toNotation: (options: TOptions) => string | undefined
toDescription: (options: TOptions) => string[]
}

Type-safe factory for creating custom notation schemas.

function defineNotationSchema<TOptions>(
schema: NotationSchema<TOptions>
): NotationSchema<TOptions>

Thrown by notation() when a string is not valid dice notation. Extends Error (not RandsumError).

PropertyTypeDescription
code'INVALID_NOTATION'Always 'INVALID_NOTATION'
suggestionstring | undefinedA corrected notation string, when one can be inferred
messagestringHuman-readable error message including the invalid input
TypeDescription
DiceNotationBranded template literal type for valid notation strings
RollOptions<T>Configuration object for a roll (sides, quantity, modifiers, key, arithmetic)
ParsedNotationOptionsResult of parsing notation (always numeric sides, required quantity and arithmetic)
ModifierOptionsTop-level modifier configuration with all 14 modifier keys
ModifierConfigUnion of all individual modifier config value types
TypeDescription
ComparisonOptions{ greaterThan?, greaterThanOrEqual?, lessThan?, lessThanOrEqual?, exact? }
DropOptionsExtends ComparisonOptions with { highest?, lowest? }
KeepOptions{ highest?, lowest? }
RerollOptionsExtends ComparisonOptions with { max? }
ReplaceOptions{ from: number | ComparisonOptions, to: number }
UniqueOptions{ notUnique: number[] }
SuccessCountOptions{ threshold: number, botchThreshold? }
TypeDescription
ValidationResultValidValidationResult | InvalidValidationResult
ValidValidationResult{ valid: true, argument, description, options, notation, error: null }
InvalidValidationResult{ valid: false, argument, error: ValidationErrorInfo }
ValidationErrorInfo{ message: string, argument: string }