mini-diceroller
v1.6.2
Published
A bare bones ts-parsec diceroller for easy embedding
Readme
Mini Diceroller
A mini language written using ts-parsec to evaluate a string of rolled dice with arithmetic expressions.
Supported Syntax
| Syntax | Description | Example |
|--------|-------------|---------|
| NdS | Roll N S-sided dice | 2d6 |
| dS | Roll a single S-sided die | d20 |
| ^dS | Roll with advantage (2 dice, take highest) | ^d20 |
| vdS | Roll with disadvantage (2 dice, take lowest) | vd20 |
| + - | Addition / Subtraction | 1d20 + 5 - 2 |
| * / | Multiplication / Division | 1d20 / 2 |
| ( ) | Grouping | (1d6 + 2) * 3 |
| -expr | Unary negation | -1d6, -(1d6 + 5) |
npm Commands
npm run test- Runs test suitenpm run build- Compiles TypeScript and outputs todist/
API
import { exec, cmdEvaluate, parse, evaluate } from "mini-diceroller";
// Parse + evaluate, returns EvaluatedExpression
const result = exec("2d6 + 3");
result.v; // total rolled value
result.stats.min; // minimum possible value
result.stats.max; // maximum possible value
result.stats.avg; // average expected value
// Parse + evaluate, returns a formatted string
cmdEvaluate("2d6 + 3");
// => '[4, 2] + 3 > 9 {min: 5, max: 15, avg: 10 }'
// Lower-level: parse only, returns an Expression tree
const expr = parse("2d6 + 3");Local REPL
[mini-diceroller]$ node
> const cmd = await import("./dist/index.js")
> cmd.cmdEvaluate("1d20 + 2")
'[13] + 2 > 15 {min: 3, max: 22, avg: 12.5 }'