@randsum/notation
Version:
A flexible, type-safe dice notation parser and validator
196 lines (155 loc) âĒ 5.12 kB
Markdown
<div align="center">
<img width="150" height="150" src="https://raw.githubusercontent.com/RANDSUM/randsum/main/icon.webp">
<h1>@randsum/notation</h1>
<h3>Dice notation parser and validator for randsum</h3>
[](https://www.npmjs.com/package/@randsum/notation)
[](https://bundlephobia.com/package/@randsum/notation)
[](https://www.npmjs.com/package/@randsum/notation)
[](https://github.com/RANDSUM/randsum/blob/main/LICENSE)
[](https://www.npmjs.com/package/@randsum/notation)
</div>
A powerful dice notation parser that supports:
- ðē Standard dice notation parsing (`4d6`, `2d20H`, etc.)
- ðŊ Complex modifier validation
- ð Full TypeScript support
- ð Human-readable descriptions
- ðŠķ Tree-shakeable implementation
## Installation
```bash
npm install @randsum/notation
# or
yarn add @randsum/notation
# or
bun add @randsum/notation
```
## Usage
```typescript
import {
validateNotation,
isDiceNotation,
notationToOptions,
type DiceNotation
} from '@randsum/notation'
const result = validateNotation('4d6L')
if (result.valid) {
console.log(result.description) // ["Roll 4 six-sided dice", "Drop lowest roll"]
}
isDiceNotation('2d20') // true
isDiceNotation('invalid') // false
const options = notationToOptions('4d6R{<3}')
console.log(options)
// {
// sides: 6,
// quantity: 4,
// modifiers: {
// reroll: { lessThan: 3 }
// }
// }
// Type inference examples
const validNotation: DiceNotation = '4d6L' // â
Valid
const invalidNotation: DiceNotation = 'invalid' // â Type error
// TypeScript will infer these as valid DiceNotation
const examples = {
basic: '2d20',
withModifier: '4d6L',
withArithmetic: '2d8+3',
withReroll: '4d6R{<3}',
withCustomFaces: '2d{HT}',
complex: '4d6L!R{<3}+2'
} satisfies Record<string, DiceNotation>
// Function parameters are type-checked
function rollDice(notation: DiceNotation) {
const result = validateNotation(notation)
return result
}
rollDice('2d20') // â
Valid
rollDice('invalid') // â Type error
// Type inference in arrays
const notations: DiceNotation[] = [
'2d20', // â
Valid
'4d6L', // â
Valid
'3d8!', // â
Valid
'2d{HT}' // â
Valid
// 'invalid' // â Would cause type error
]
// Usage with template literals
function createAttackRoll(bonus: number): DiceNotation {
return `1d20+${bonus}` // â
Valid DiceNotation
}
// Regular usage examples
const result = validateNotation('4d6L')
if (result.valid) {
console.log(result.description) // ["Roll 4 six-sided dice", "Drop lowest roll"]
}
isDiceNotation('2d20') // true
isDiceNotation('invalid') // false
const options = notationToOptions('4d6R{<3}')
console.log(options)
// {
// sides: 6,
// quantity: 4,
// modifiers: {
// reroll: { lessThan: 3 }
// }
// } satisfies Record<string, DiceNotation>
// Function parameters are type-checked
function rollDice(notation: DiceNotation) {
const result = validateNotation(notation)
return result
}
rollDice('2d20') // â
Valid
rollDice('invalid') // â Type error
// Type inference in arrays
const notations: DiceNotation[] = [
'2d20', // â
Valid
'4d6L', // â
Valid
'3d8!', // â
Valid
'2d{HT}' // â
Valid
// 'invalid' // â Would cause type error
]
// Usage with template literals
function createAttackRoll(bonus: number): DiceNotation {
return `1d20+${bonus}` // â
Valid DiceNotation
}
```
## Supported Notation
### Basic Syntax
- `NdS`: Roll N S-sided dice (e.g., `4d6`)
- `NdS+X`: Add X to total (e.g., `2d8+3`)
- `NdS-X`: Subtract X from total (e.g., `2d8-1`)
### Modifiers
- `L`: Drop lowest (e.g., `4d6L`)
- `H`: Keep highest (e.g., `2d20H`)
- `R{<N}`: Reroll below N (e.g., `4d6R{<3}`)
- `!`: Exploding dice (e.g., `3d8!`)
- `U`: Unique results (e.g., `3d6U`)
See [Dice Notation Reference](https://github.com/RANDSUM/randsum/blob/main/RANDSUM_DICE_NOTATION.md) for complete documentation.
## API Reference
### `validateNotation`
```typescript
const result = validateNotation('4d6L')
// Returns:
// {
// valid: true,
// type: 'numeric',
// description: ['Roll 4 six-sided dice', 'Drop lowest roll'],
// notation: { quantity: 4, sides: 6, modifiers: { drop: { lowest: 1 } } }
// }
```
### `isDiceNotation`
```typescript
isDiceNotation('2d20') // true
isDiceNotation('2d{HT}') // true (custom faces)
isDiceNotation('invalid') // false
```
### `notationToOptions`
```typescript
notationToOptions('4d6L')
// Returns options object compatible with @randsum/dice
```
## Related Packages
- [@randsum/dice](https://github.com/RANDSUM/randsum/tree/main/packages/dice): Dice rolling implementation
- [@randsum/5e](https://github.com/RANDSUM/randsum/tree/main/packages/5e): 5th Edition compatible dice rolling
<div align="center">
Made with ðđ by <a href="https://github.com/RANDSUM">RANDSUM</a>
</div>