1.0.3 • Published 6 days ago

@virtuoid/dice v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

Fun With JavaScript - 'Dice' static class

This repository is for the 'Dice' static class used in the Fun with JavaScript series (https://funwithjavascript.com).

Versions

1.0.3 : Fixed 2 year old bug where it didn't load

1.0.2 : Fixed bug for multiple 'd' operators in equation.

1.0.0 : Initial Version

Installation

npm install --save @virtuoid/dice

Dependencies

No dependencies

Usage

It is important to note that the Dice class is a static class. It cannot be instantiated. In fact, it will throw an error if you do!

import Dice from '@virtuoid/dice';

// Roll a single six-sided die
const rollSix = Dice.roll('d6');
const rollOneSix = Dice.roll('1d6');

// Roll three six-sided die
const rollThreeSix = Dice.roll('3d6');

// How about three die and add 2?
const modifiedRoll = Dice.roll('3d6+2');

// How about a twenty-sided die?
const twentySides = Dice.roll('d20');

// Wait! There's more! Entire Equations
const strange = Dice.roll('3+(d30-4)*(4+3d3)-16');

Methods

NameReturnsDescription
roll(dieDescriptor: string)NumberRolls die according to the passed equation (see below).

Equation Parser

The roll() method lets you pass an equation as the argument. This equation is then parsed and evaluated using normal mathematical precedence rules.

Addition, subtraction, multiplication, and division are supported. Parentheses are also supported.

There is a special operator called the Dice operator that, when used with a prefix and suffix, will roll a certain number of a certain type of die. The format is:

xdy

...where:

TokenRequired?DefaultDescription
xNo1The number of die to roll. Defaults to 1.
dYesThe letter 'd'. This must always be there, as it is the operator.
yYesThe number of sides on the die

Examples:

RollDescription
d6Roll a single six-sided die
1d6Roll a single six-sided die (same as first example)
3d6Roll three six-sided die
d20Roll a twenty-sided die
d100Roll a hundred-sided die
10d100Roll ten hundred-sided die

Github

https://github.com/TheVirtuoid/fwjs-dice
1.0.3

6 days ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago