npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

platonic-dice

v1.1.0

Published

A JavaScript library for simulating TTRPG dice rolls.

Downloads

15

Readme

Platonic Dice

A flexible and extensible JavaScript library for simulating tabletop RPG dice rolls.

Overview

Platonic Dice is a JavaScript library designed to model and simulate dice rolling mechanics commonly used in tabletop role-playing games (TTRPGs), such as Dungeons & Dragons. It provides a robust API to handle standard dice rolls, advantage/disadvantage mechanics, target-based success checks, and more.

Whether you need a simple dice roller, a way to track roll history, or an extensible library to integrate with your own gaming applications, Platonic Dice is built for flexibility and ease of use.


Features

Full TTRPG Dice Support – Supports d4, d6, d8, d10, d12, d20 rolls.
Advantage & Disadvantage Rolls – Implements rolling mechanics for TTRPG rules.
Persistent Dice Objects – Track roll history for each die instance.
Custom Dice – Define custom faces with unique outcomes.
Target-Based Rolling – Test against predefined success/failure conditions.
Modifier Functions – Apply transformations to dice rolls dynamically.
Library-Friendly – Designed for easy import and use in other applications.


Installation

To install via npm, run:

npm install platonic-dice

Or via yarn:

yarn add platonic-dice

Usage Examples

1️⃣ Basic Dice Rolls

const { rollDie, rollDice } = require("platonic-dice");

console.log(rollDie("d20")); // Rolls a single d20
console.log(rollDice("d6", { count: 3 })); // Rolls three d6 dice

2️⃣ Rolling with Advantage/Disadvantage

const { RollType } = require("platonic-dice");

console.log(rollDie("d20", RollType.Advantage)); // Rolls with advantage
console.log(rollDie("d20", RollType.Disadvantage)); // Rolls with disadvantage

3️⃣ Persistent Dice Instances

const { Die } = require("platonic-dice");

const myDie = new Die("d12");
console.log(myDie.roll()); // Roll the die
console.log(myDie.history); // View roll history
console.log(myDie.toJSON()); // Get a JSON representation

4️⃣ Applying Modifiers to Rolls

const { ModifiedDie } = require("platonic-dice");

const addTwoModifier = (roll) => roll + 2;
const modDie = new ModifiedDie("d10", addTwoModifier);

console.log(modDie.roll()); // Rolls d10 and adds 2 to result
console.log(modDie.modifiedHistory); // Check modified roll history

5️⃣ Target-Based Rolling (Success/Failure)

const { TargetDie } = require("platonic-dice");

const targetDie = new TargetDie("d20", [18, 19, 20]); // Success on 18+
console.log(targetDie.roll()); // Rolls and checks success/failure
console.log(targetDie.getHistory()); // View full roll history

6️⃣ Custom Dice with Face Mapping

const { CustomDie } = require("platonic-dice");

const faceMappings = {
    1: () => "Critical Failure",
    20: () => "Critical Success",
};

const customDie = new CustomDie("d20", faceMappings, () => "Normal Roll");
console.log(customDie.roll()); // Rolls and maps result
console.log(customDie.report(true)); // View detailed roll report

7️⃣ Test Dice with Critical Success/Failure

const { TestDie, TestConditions } = require("platonic-dice");

const testConditions = new TestConditions(10, 18, 2); // Success at 10+, Crit at 18+, Fail at 2-
const testDie = new TestDie("d20", testConditions);

console.log(testDie.roll()); // Rolls and evaluates success/failure
console.log(testDie.report()); // View latest roll result and outcome

Development Roadmap

Core Dice Functionality – Standard rolls, advantage/disadvantage, roll history.
Target-Based & Test Rolls – Rolling against thresholds with outcomes.
🔲 Dice Pools & Multiple Dice Mechanics – Implement grouped rolls with different conditions.
🔲 Optional UI Component – Develop a visual dice roller for web apps.


Contributing

Want to contribute? Here’s how:

  1. Fork the repo and create your feature branch (git checkout -b feature-name).
  2. Make your changes and commit (git commit -m "Add new feature").
  3. Push to your branch (git push origin feature-name).
  4. Submit a Pull Request for review.

License

This project is licensed under the MIT License – see the LICENSE file for details.


🚀 Ready to roll some dice? Let’s play! 🎲