JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 31
  • Score
    100M100P100Q19496F
  • License MIT

Library for rolling dice based on dice-syntax used in RPGs.

Package Exports

  • d20

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (d20) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

d20.js

Javascript library for rolling RPG dice. Supports dice notation such as "4d6" and "d20+2".

Installation

In the browser

Download the files from GitHub and include the d20.js file somewhere in your HTML page.

<script src="path/to/d20.js"></script>

As a Node.js module

Add the git repository as a dependency in your package.json file (you can specify a version branch instead of "master" if you want a specific version).

{
    "dependencies": {
        "d20": "git://github.com/michaelenger/d20.js.git#master"
    }
}

require it in your app.

var d20 = require('d20');

Usage

Both methods of using the library provides a d20 object with the roll() method which is used to roll dice.

d20.roll(20); // roll a 20-sided die
d20.roll('4d6'); // roll four 6-sided dice
d20.roll('2d8+1'); // roll two 8-sided dice and add 1 to the result
d20.roll('1d8 +1 +2 -20'); // roll an 8-sided die with multiple modifiers

You can get the result as an array of values rather than a single result if you pass true along as the second parameter. Note that the results will be sorted in ascending order except for the modifiers which will be in their order of apperance.

d20.roll(20, true);
d20.roll('4d6', true);
d20.roll('2d8+1', true);
d20.roll('1d8 +1 +2 -20', true);

Testing

The library can be tested by installing the dependencies and running npm test:

npm install
npm test