New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

bool-chance

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bool-chance

Weighted random bool generator

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

bool-chance

A simple wrapper around Math.random for returning a random boolean, with the provided chance of being true. It exports an object with a ::get method to facilitate stubbing for unit tests.

Usage

const boolChance = require('bool-chance');

let bool = boolChance.get(0.75); // Will return true 75% of the time.
let otherBool = boolChance.get(0.5); // Will return true half of the time.

Why

Even though this is very simple, popular utility libraries like lodash don't have a method like it. Chance.js does, but its api requires you to construct an instance of a directly exported class, which can be frustrating for unit tests since you can't stub the constructor. You can stub the Chance::Chance method to get an instance, though having to do this every time you need to test a random bool is annoying.

As a result, I found myself writing utility functions like this one in a lot of my personal projects. I figured I'd save myself some trouble and just publish it. Use if you like, though I don't really have plans to add much more to it in the future.

Keywords

bool

FAQs

Package last updated on 29 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts