@randsum/notation
v3.0.0
Published
Dice notation parser and types for the @randsum ecosystem
Maintainers
Readme
Zero-dependency dice notation parser, validator, and type system for JavaScript and TypeScript.
Note: Most users should install
@randsum/roller, which re-exports everything from this package along with theroll()function. Install@randsum/notationdirectly only if you need parsing and validation without the rolling engine.
Installation
npm install @randsum/notation
# or
bun add @randsum/notationUsage
Parsing Notation
import { isDiceNotation, notationToOptions } from "@randsum/notation"
isDiceNotation("4d6L") // true
isDiceNotation("banana") // false
const options = notationToOptions("4d6L")
// { sides: 6, quantity: 4, modifiers: { drop: { lowest: 1 } } }Validation
import { validateNotation } from "@randsum/notation"
const result = validateNotation("4d6L")
if (result.valid) {
result.notation // '4d6L'
result.options // parsed RollOptions
} else {
result.errors // array of validation errors
}Converting Between Formats
import { optionsToNotation, optionsToDescription } from "@randsum/notation"
const notation = optionsToNotation({
sides: 6,
quantity: 4,
modifiers: { drop: { lowest: 1 } }
})
// '4d6L'
const description = optionsToDescription({
sides: 20,
quantity: 1,
modifiers: { plus: 5 }
})
// '1d20 + 5'Listing Notation Strings
import { listOfNotations } from "@randsum/notation"
const notations = listOfNotations("4d6L + 2d8")
// ['4d6L', '2d8']API
Parsing
isDiceNotation(value)- Type guard for valid dice notation stringsnotationToOptions(notation)- Parse a notation string into aRollOptionsobjectlistOfNotations(input)- Extract individual notation strings from a combined expressionvalidateNotation(notation)- Validate notation and return parsed result or errorssuggestNotationFix(notation)- Suggest corrections for invalid notation
Transformers
optionsToNotation(options)- Convert aRollOptionsobject to a notation stringoptionsToDescription(options)- Convert aRollOptionsobject to a human-readable descriptionmodifiersToNotation(modifiers)- Convert modifier options to their notation suffixmodifiersToDescription(modifiers)- Convert modifier options to a readable description
Comparison Utilities
parseComparisonNotation(notation)- Parse comparison syntax like{<3,>18}hasConditions(options)- Check if comparison options have any conditionsformatComparisonNotation(options)- Format comparison options as notationformatComparisonDescription(options)- Format comparison options as text
Types
import type {
DiceNotation,
RollOptions,
ModifierOptions,
ComparisonOptions,
DropOptions,
KeepOptions,
RerollOptions,
ReplaceOptions,
UniqueOptions,
SuccessCountOptions,
ValidationResult,
NotationSchema
} from "@randsum/notation"Modifier Schemas
All 14 modifier notation schemas are exported for use by the roller's modifier registry:
capSchema, dropSchema, keepSchema, replaceSchema, rerollSchema, explodeSchema, compoundSchema, penetrateSchema, uniqueSchema, countSuccessesSchema, multiplySchema, plusSchema, minusSchema, multiplyTotalSchema
Related Packages
- @randsum/roller - Core dice rolling engine (depends on this package)
- randsum.dev - Documentation site
