smart-dice
v1.0.0
Published
A smart dice rolling library for JavaScript and TypeScript.
Downloads
106
Readme
smart-dice
smart-dice is a lightweight dice rolling library for JavaScript and TypeScript.
Features
- Parses classic dice notation:
XdY +/- Z - Supports advantage (
h) and disadvantage (l) in1dYh/1dYl - Returns full roll details (
rolls,total, critical flags) - Uses mixed entropy (
Math.random()+Date.now()) for roll generation
Installation
npm install smart-diceQuick Start
import { rollDice } from "smart-dice";
const result = rollDice("2d6 + 3");
console.log(result);Input Format
The function accepts this pattern:
[numberOfDice]d[dieType][h|l]? [+-modifier]
Examples:
1d20 + 52d6 + 31d20h + 4(advantage)1d20l - 1(disadvantage)
API
rollDice(input: string): RollResult
Parses a dice expression, performs the rolls, applies the modifier, and returns:
interface RollResult {
input: string;
rolls: number[];
modifier: number;
advantageType: null | string;
advantageRoll: null | number;
total: number;
hasCritical: boolean;
hasCriticalFailure: boolean;
}Behavior Notes
- Advantage/disadvantage is only valid with a single die (
1d20h,1d20l) totalis calculated from the sum of allrollsplusmodifierhasCriticalistrueif any roll equals the die type (for example20on ad20)hasCriticalFailureistrueif any roll equals1
Error Cases
rollDice throws an error in these cases:
Invalid input formatwhen dice notation is invalidModifier is requiredwhen+Nor-Nis missingAdvantage/Disadvantage can only be applied to a single die
Local Development
npm install
npm run test:run
npm run buildAvailable scripts:
npm testnpm run test:runnpm run lintnpm run lint:fixnpm run build
Contributing
Contributions are welcome.
- Open an issue
- Open a pull request
License
This project is licensed under the MIT License.
