chancellor

1.0.3 • Public • Published

chancellor

If you need to call a function only part of the time, then this library can make your code more readable.

If you find yourself frquently writing code like the following in your project:

if (Math.random() >= 0.30) {
    doSomething();
}

you can instead write the following using this library:

chanceof(0.30).to(doSomething);

or if you prefer using percentages:

chanceof("30%").to(doSomething);
// or
chanceof.percent(30).to(doSomething);

Installation

npm i chancellor

Importing

import chanceof from "chancellor";

Usage

If statement

if (chanceof(0.42).roll()) {
    console.log("I say hi 42% of the time");
}

or

if (chanceof(0.42).roll()) console.log("I say hi 42% of the time");

While statement

const dice = chanceof(0.7);
while (dice.roll()) console.log("Hello, world!");

Callback

const log = result => console.log(result);

chanceof(0.5, log);

Package Sidebar

Install

npm i chancellor

Weekly Downloads

7

Version

1.0.3

License

MPL-2.0

Unpacked Size

152 kB

Total Files

12

Last publish

Collaborators

  • voidvoxel