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

dice-notation-js

v1.0.3

Published

A dice notation parser and roller.

Downloads

41

Readme

Dice Notation

Build Status Coverage Status NPM

Parses dice notation and rolls dice.

Install

Browser

You can grab a hosted version here:

<script src="https://cdn.jsdelivr.net/gh/chapelr/dice-notation@latest/dist/dice.min.js" type="text/javascript"></script>

Or include dist/dice.min.js or dice.js as appropriate in your webpage.

This will expose the global Dice API, or create a UMD module, if appropriate.

NPM

npm install dice-notation-js

Then require or import it:

const Dice = require('dice-notation-js');

Usage

Exposes three functions, Dice(), Dice.detailed() and Dice.parse().

Dice()

Syntax:

  • Dice(notation [, randomFunction])
  • Dice(number, type [, randomFunction])

Takes a string of dice notation, or two numbers representing the number and type of dice to roll, and optionally a randomization function (that replaces Math.random()) and returns the result of the dice roll.

Arguments:

  • notation (string): You can pass the funciton a string of dice notation, e.g., 3d6+2, or 1d20.
  • number (number): You can pass the function a number of dice to roll and the number of sides each die should have. This argument is the number of dice to roll.
  • type (number): You can pass the function a number of dice to roll and the number of sides each die should have. This argument is the number of sides each die should have.
  • randomFunction (function) optional: You may pass a function that returns a random number between 0 and 1 that will be used in place of Math.random(), such as to use a seedable PRNG.

Examples

Basic use:

// All of the following are functionally the same:

Dice('3d6+2');
Dice('3d6') + 2;
Dice(3, 6) + 2;

Using a seedable PRNG, such as seedrandom:

const Dice = require('dice-notation-js');
const prng = require('seedrandom')('hello');

Dice('3d6+2', prng);
Dice('3d6', prng) + 2;
Dice(3, 6, prng) + 2;

Dice.detailed()

Syntax:

  • Dice.detailed(notation [, randomFunction])
  • Dice.detailed(number, type [, randomFunction])

Takes a string of dice notation, rolls it like Dice(), but returns additional details for your use.

Arguments:

  • notation (string): You can pass the funciton a string of dice notation, e.g., 3d6+2, or 1d20.
  • number (number): You can pass the function a number of dice to roll and the number of sides each die should have. This argument is the number of dice to roll.
  • type (number): You can pass the function a number of dice to roll and the number of sides each die should have. This argument is the number of sides each die should have.
  • randomFunction (function) optional: You may pass a function that returns a random number between 0 and 1 that will be used in place of Math.random(), such as to use a seedable PRNG.

Examples

var roll = Dice.detailed('3d6+10');

console.log(roll); 
// -> { number: 3, type: 6, modifier: 10, rolls: [ 2, 1, 6 ], result: 19 } 

Dice.parse()

Syntax: Dice.parse(notation)

Takes a string of dice notation and returns an object parsed from it. For example, the notation 6d4-1 returns the following object:

{
    number : 6,
    type : 4,
    modifier -1
}

Arguments:

  • notation (string): You can pass the funciton a string of dice notation, e.g., 3d6+2, or 1d20.

Examples

Basic use:

Dice.parse('1d10') // -> { number : 1, type : 10, modifier :  0 }
Dice.parse('3d6+2') // -> { number : 3, type : 6, modifier :  2 }
Dice.parse('2d4-1') // -> { number : 2, type : 4, modifier : -1 }