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 🙏

© 2024 – Pkg Stats / Ryan Hefner

roll-master

v1.0.0

Published

This is a library designed for rolling dice, which parses instructions based on a specific syntax.

Downloads

1

Readme

Tests Node.js Version

Roll Master

Roll Master is a library that parses a string and rolls dice to generate results.

Usage

import Roller from 'roll-master';

async function rollAThing() {
  const roll = "4d6r2!2";
  const roller = new Roller();
  const result = await roller.roll(roll);
}

Syntax

Roll Master uses a special syntax to define dice rolls.

Dice

To define dice rolls, use the format <count>d<sides>. For example, 4d6 represents rolling four six-sided dice.

By default, all rolls are sorted from smallest to largest. To sort them in the opposite order, use s-. Please note that only one sort directive can be used per dice definition.

To reroll, use r<count>. For example, 4d6r2 will roll four six-sided dice and reroll the two smallest ones. To reroll the largest ones, use r<count>s-. For instance, 4d6r2s- is valid.

To discard, use !<count>. For example, 4d6!2 will roll four six-sided dice and discard the two smallest ones. To discard the largest ones, use !<count>s-. For instance, 4d6!2s- works.

Reroll and discard options can be combined. For example, 4d6r2!2 will roll four six-sided dice, reroll the two smallest values, and then discard the two smallest remaining values. Keep in mind that only one reroll and one discard directive can be used per dice definition.

Group rolls using ... For example, 2d4..3 will roll two four-sided dice three times, returning an array of roll results. Conversely, 2d4*3 will sum the result of each roll and then multiply it by three.

Operations

To define operations, use the format <expression><operation><expression>. For example, 4d6+2 represents rolling four six-sided dice, summing the results, and then adding 2 to the total.

The supported operations are addition (+), subtraction (-), and multiplication (*).

Multiplication takes precedence over other operations. The remaining operations are of equal importance and are processed sequentially.

Use parentheses to define blocks, like (<expression>+(<expression>*<expression>)). For example, (4d6+3)*2.

Structure of output

  • rolls (optional): An array of objects, each representing a single dice roll or a group of dice rolls.
    • roll: A string indicating the dice roll expression (e.g., 4d6).
    • rolls: An object where each key (e.g., d0, d1) represents a die and its value is an object containing:
      • rolls: An array of numbers representing the outcomes of that particular die.
      • discarded (optional): A boolean indicating if the roll was discarded (e.g., !2 in 4d6!2).
      • rerolled (optional): A boolean indicating if the roll was rerolled (e.g., r2 in 4d6r2).
    • sum: The sum of all the dice rolls in this object.
  • result (optional): The final result after applying all operations.

Examples

  1. Simple Roll (4d6):

    {
      "roll": "4d6",
      "rolls": {
        "d0": {"rolls": [6]},
        "d1": {"rolls": [1]},
        "d2": {"rolls": [3]},
        "d3": {"rolls": [4]}
      },
      "sum": 14
    }
  2. Roll with Discard (4d6!1):

    {
      "roll": "4d6",
      "rolls": {
        "d0": {"rolls": [6]},
        "d1": {"rolls": [1], "discarded": true},
        "d2": {"rolls": [3]},
        "d3": {"rolls": [4]}
      },
      "sum": 13
    }
  3. Roll with Reroll (5d4r2):

    {
      "roll": "5d4",
      "rolls": {
        "d0": {"rolls": [4]},
        "d1": {"rolls": [1, 4], "rerolled": true},
        "d2": {"rolls": [3]},
        "d3": {"rolls": [2, 1], "rerolled": true, "discarded": true},
        "d4": {"rolls": [4]}
      },
      "sum": 15
    }