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

@ms-sol/dice-roll

v0.1.0

Published

Simple library to interpret, execute dice rolls and calculate odds of given dice result. It uses dice description in dnd form (e.g. 2d6+4) or range (27-34). It can be used in any game that uses dice rolls.

Downloads

400

Readme

@ms-sol/dice-roll

This library provides a simple way to interpret and execute dice rolls in the format commonly used in Dungeons & Dragons (D&D) and other tabletop roleplaying games (TTRPGs).

Installation

Install the library using npm or yarn:

npm install @ms-sol/dice-roll

Usage

This library offers two functions:

rollDice(input: string | DiceSetup): number Takes a dice notation string (e.g., "2d6", "4d10", "3d8+2", allowed notations below) as input. Alternatively you can pass DiceSetup interface implementing object which is returned by parseDice function.

Returns an object containing the roll results. The object typically includes properties like the total rolled value, the individual dice rolls, and any modifiers applied.

parseDice(notation: string): DiceSetup | null Parses the dice notation string into a more structured representation (DiceSetup interface). Returns an object with properties:

  • count: number of dice
  • die: depending on your input this object's descripting (A) the die's minimum, maximum and optional modifier or (B) custom sides of the die.

CommonJS (CJS):

const diceRoll = require('@ms-sol/dice-roll');

const result = diceRoll.rollDice('2d6');
console.log(result); // result from 2*1=2 till 2*6=12

ES Modules (ESM):

Importing specific functions:

import { rollDice, parseDice } from '@ms-sol/dice-roll';

const result = rollDice('3d8');
console.log(result); // result from 3*1=3 till 3*8=24

Importing the entire library:

import diceRoll from '@ms-sol/dice-roll';

const result = diceRoll.rollDice('4d10');
console.log(result); // result from 4*1=4 till 4*10=40

Allowed input string formats

"d6" // standard 6-sided die
"3d4" // three 4-sided dice

"d4+2" // 4-sided die with +2 modifier to die roll result
"d4-2" // 4-sided die with -2 modifier to die roll result

"4-17" // range from 4-17, equivalent of d14+3

"2d[10,20,30,40,50,60]" // two dice with 6 custom sides (10,20,30,40,50,60)
"2d[10,20,30,40,50,60]+5" // as above but with additional +5 modifier to dice roll result

// HINT: any spaces you input are discarded
" d 4 - 2" // will be treated as "d4-2"

// HINT 2: both lower- and uppercase 'd' are allowed
" D 4 - 2" // will be treated as "d4-2"

Calculating result probability

It might be usefull to calculate the probability of a given result in certain dice setup.

// use string dice setup
calculateProbability('d6', 1) // returns 0.1667 (16.67%)

// or parsed object
const diceSetup = parseDice('d6');
calculateProbability(diceSetup, 1) // returns 0.1667 (16.67%)

License

This library is licensed under the MIT License. See the LICENSE file for details.