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-expression-evaluator

v0.1.2

Published

Parses and evaluates dice expressions using simulated dice rolls

Downloads

110

Readme

dice-expression-evaluator NPM version Build Status Dependency Status Coverage percentage

dice-expession-evaluator is used to parse dice expressions and to evaluate them using simulated dice rolls.

Installation

$ npm install --save dice-expression-evaluator

Dice expressions

Dice expressions are used to specify calculations that are based on a series of dice rolls. For example, this is a dice expression that represents a single, standard 6-sided die:

1d6

This can be shortened to:

d6

What if we rolled five of those dice at once and added up?

5d6

To roll two other 10-sided dice and subtract the sum of the two from above?

5d6 - 2d10

What if we also wanted to add 15 to ensure that the final result does not go below zero?

5d6 - 2d10 + 15

If the number of sides is 100, it can be replaced with a % sign (percentage, out of 100, got it?)

5d100 == 5d%

Dice expressions can also be specified as:

  • Any string denoting a whole number (don't start with 0 or minus sign please) is a dice expression.
  • Any string of form x?(d|D)y where x and y are both whole numbers (see warning above) is a dice expression. ? means x is optional and (d|D) means either d or D works.
  • Any string of form x?(d|D)% where x and y are both whole numbers is a dice expression.
  • Any string of form DiceExpression + DiceExpression or form DiceExpression - DiceExpression is a dice expression.
  • Leading or trailing spaces don't matter and neither do spaces between terms and operators. Just like in math.

API

var DiceExpression = require('dice-expression-evaluator');

var d = new DiceExpression('2d5 + 4d2 + 10');
  • d(): evaluates the dice expression by simulating dice rolls and returns the resulting roll
  • d.min(): returns the minimum possible roll for the dice expression
  • d.max(): returns the maximum possible roll for the dice expression
  • d.roll(): evaluates the dice expression by simulating dice rolls and returns the resulting roll as well as the value for each term within the dice expression and the individual dice rolls within that term.

Examples

d() // 20
d() // 18
d() // 23
d.min() // 16
d.max() // 28
d.roll() // { roll: 23, diceSums: [7, 6, 10], diceRaw: [[3, 4], [1, 2, 2, 1], [10]] }

Future plans

We may add support for

  • Coefficients for terms so that we can multiply dice rolls by a constant value
  • Other arithmetic operators
  • Nested expressions using parantheses

License

MIT © Dan Kang