Code Monkey home page Code Monkey logo

wrand's Introduction

CI status GitHub package.json version Stars created by Leonardo Montini youTube video views

wrand

Extract one or more random elements from a weighted array.

const items = [
  { original: "Bronze", weight: 20 },
  { original: "Silver", weight: 10 },
  { original: "Gold", weight: 3 },
  { original: "Platinum", weight: 1 },
];

const picker = new RandomPicker(items);

const result = picker.pick();

โญ๏ธ If you like this library, don't forget to give it a star! โญ๏ธ

Table of contents

Installation

Node

# If you use npm
npm install wrand

# If you use yarn
yarn add wrand

Deno

If you use Deno, you can just import what you need from deno.land/x

import {
  RandomPicker,
  pick,
  pickMany,
  flatten,
} from "https://deno.land/x/wrand/mod.ts";

Usage

Hint: The test.ts file contains all possible examples. Thi is the best source of usage documentation.

RandomPicker object

You can use the RandomPicker object to pick one or more random elements from a weighted array. The array can be any type, objects are fine.

The best usage here is if you need to pick many items at different moments and you don't want to recreate the object every time.

const items = [
  { original: "Bronze", weight: 20 },
  { original: "Silver", weight: 10 },
  { original: "Gold", weight: 3 },
  { original: "Platinum", weight: 1 },
];

const picker = new RandomPicker(items);

Once the item is created, you can call all exposed methods, in particular pick and pickMany.

const result = picker.pick();

const results = picker.pickMany(10);

Standalone methods

If you only need to pick one item sporadically, you can hide the object creation and use the standalone methods instead.

The syntax is much more concise but keep in mind that the object is created underneath, you just don't have the reference and the code looks shorter.

const items = [
  { original: "Bronze", weight: 20 },
  { original: "Silver", weight: 10 },
  { original: "Gold", weight: 3 },
  { original: "Platinum", weight: 1 },
];

const result = pick(items);

Roadmap

This is a high-level roadmap. You can find more in the open issues.

  • RandomPicker class
    • Multiple picks with option for duplicates
    • Handle duplicates
  • Standalone methods
    • pick
    • pickMany
    • flatten
  • Extra features
    • Add badges to the documentation
    • Allow for custom random function
    • Keep coverage high and not forget edge cases

Contributing

Contributions of any kind are welcome! You can find more info on CONTRIBUTING.md.

wrand's People

Contributors

anoopfranc avatar balastrong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

wrand's Issues

Improve tests

As of v1.0.1 c6fba99 the coverage is already 100% but there are for sure some edge cases that are not covered, for example passing a negative amount to pickMany.

It can be useful to identify such cases and add more tests to validate/strengthen the code.

Fix format command

As is:
npm run format does not work on Windows (and probably a --write is missing too!)

To be:
The command works regardless of the OS and all files are formatted properly.

Add core concept of amount

Potentially an evolution of #3, add a new optional field, amount: number, to WeightedItem.

The amount is reduced by 1 every time the item is picked.

Prevent duplicates on a single call of pickMany

Add a boolean flag on pickMany (or a new function, pickManyDistinct) to prevent duplicates to be returned in a single call.

Keep an extra eye on cases where the requested amount is higher than the actual items and no duplicates are wanted.

Add the possiblity of adding a custom random function

We know Math.random() isn't the best random number generator option for many reasons.

In our case though, it would be a huge bonus being able to provide a custom random function as the user can already have one and our tests would be way more reliable with a random method that accepts a seed.

A new parameter can be passed to the constructor and the standalone methods to provide the random implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.