Skip to content

Roller API Reference

Everything exported from @randsum/roller. Items marked re-export originate from @randsum/notation and are re-exported for convenience — see the Notation API Reference for full details.

Roll dice. Accepts numbers, notation strings, options objects, or any combination. An optional RollConfig object can be passed as the last argument to customize the random function.

import { roll } from '@randsum/roller'
roll(20) // number: 1d20
roll('4d6L') // notation string
roll({ sides: 6, quantity: 4, modifiers: { drop: { lowest: 1 } } })
roll('1d20+5', '2d6+3') // multiple groups
roll('2d6', { randomFn: () => 0.5 }) // custom RNG

Signature:

function roll<T = string>(
...args: [...RollArgument<T>[], RollConfig]
): RollerRollResult<T>

Validate a dice notation string and return a detailed result with parsed structure or error.

import { validateNotation } from '@randsum/roller'
const result = validateNotation('4d6L')
if (result.valid) {
console.log(result.notation) // ['4d6L'] — array of DiceNotation
console.log(result.options) // [{ sides: 6, quantity: 4, ... }] — array of ParsedNotationOptions
} else {
console.log(result.error) // { message: '...', argument: '...' }
}

Signature:

function validateNotation(notation: string): ValidationResult

Type guard that checks whether a string is valid dice notation.

import { isDiceNotation } from '@randsum/roller'
if (isDiceNotation(userInput)) {
// userInput is now typed as DiceNotation
const result = roll(userInput)
}

Signature:

function isDiceNotation(value: string): value is DiceNotation

Assert a string is valid dice notation or throw NotationParseError.

Signature:

function notation(value: string): DiceNotation

Validators for numeric input. Used by the roller engine and game packages. Each throws ValidationError on failure.

FunctionDescription
validateInteger(value, name)Asserts value is an integer
validateRange(value, min, max, name)Asserts value is within [min, max]
validateNonNegative(value, name)Asserts value is >= 0
validateFinite(value, name)Asserts value is finite

RandsumError extends Error and adds a code property. ValidationError, ModifierError, and RollError all extend RandsumError. NotationParseError extends Error directly (it is re-exported from @randsum/notation).

ClassExtendsCodeThrown when
RandsumErrorError(varies)Base class for roller errors
ValidationErrorRandsumErrorVALIDATION_ERRORInvalid input (zero sides, negative quantity, etc.)
ModifierErrorRandsumErrorMODIFIER_ERRORModifier produces invalid state (e.g. dropping all dice)
RollErrorRandsumErrorROLL_ERRORGeneral roll execution failure
NotationParseError (re-export)ErrorINVALID_NOTATIONInvalid dice notation string

Constant object with all error code strings:

const ERROR_CODES = {
INVALID_NOTATION: 'INVALID_NOTATION',
MODIFIER_ERROR: 'MODIFIER_ERROR',
VALIDATION_ERROR: 'VALIDATION_ERROR',
ROLL_ERROR: 'ROLL_ERROR'
} as const

Return type of roll(). Extends RollResult.

PropertyTypeDescription
totalnumberCombined total of all roll groups
resultT[]Aggregate result values (defaults to string[])
rollsRollRecord<T>[]Full record for each roll group

Detailed record of a single roll group.

PropertyTypeDescription
argumentRollArgument<T>Original input for this group
notationDiceNotationDice notation string
descriptionstring[]Human-readable descriptions
parametersRollParams<T>Fully resolved roll parameters
rollsnumber[]Die values after modifiers
initialRollsnumber[]Die values before modifiers
modifierLogsModifierLog[]Log of each modifier application
appliedTotalnumberTotal including arithmetic modifiers
totalnumberFinal total for this group
customResultsT[] (optional)Custom face results for non-numeric dice

Generic base type.

PropertyTypeDescription
rollsTRollRecord[]Individual roll records
resultTResultAggregate result
TypeDescription
RollArgument<T>RollOptions<T> | DiceNotation | number
RollOptions<T>Options object with sides, quantity, modifiers (re-export)
RollConfig{ randomFn?: RandomFn } — pass as last argument to roll()
RandomFn() => number — must return [0, 1)
DiceNotationBranded string type for valid notation (re-export)

All modifier option types are re-exported from @randsum/notation.

TypeDescription
ModifierOptionsTop-level modifier configuration object
ModifierConfigUnion of all individual modifier configs
ComparisonOptionsConditions like { greaterThan: 3, lessThan: 18 }
DropOptionsExtends ComparisonOptions with { lowest?, highest? }
KeepOptions{ lowest?: number, highest?: number }
RerollOptionsReroll conditions and limits
ReplaceOptionsValue replacement rules
UniqueOptionsUnique constraint config
SuccessCountOptionsCount dice meeting a threshold
TypeDescription
RollParams<T>Fully resolved parameters (extends RollOptions, adds faces, argument, description, notation)
RequiredNumericRollParameters{ quantity: number, sides: number }
ModifierLog{ modifier, options, added, removed } — log entry for one modifier
NumericRollBonus{ rolls: number[], logs: ModifierLog[] } — intermediate modifier state
TypeDescription
ValidationResultValidValidationResult | InvalidValidationResult
ValidValidationResult{ valid: true, argument, description, options, notation, error: null }
InvalidValidationResult{ valid: false, argument, error: ValidationErrorInfo }
ValidationErrorInfo{ message: string, argument: string }