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

@rahulv.dev/weighted-list

v1.0.2

Published

A weighted list provider to pick a random entry on the basis of the weights of the objects.

Downloads

5

Readme

@rahulv.dev/weighted-list

Description

@rahulv.dev/weighted-list is a TypeScript library that provides a weighted list implementation. This allows you to add items with associated weights and then randomly select items based on those weights. The library is ideal for scenarios where you need to randomly pick elements with different probabilities.

This package depends on @rahulv.dev/rng for secure and cryptographic random number generation.

Installation

Install both the weighted list and the RNG package:

npm install @rahulv.dev/weighted-list @rahulv.dev/rng

Usage

Importing the Packages

First, import the WeightedList, WeightedListItem, and RNG classes into your TypeScript or JavaScript project:

import { WeightedList } from "@rahulv.dev/weighted-list";
import { RNG } from "@rahulv.dev/rng";

Setting Up RNG

Initialize the RNG before using it in the WeightedList:

const rng = RNG.init();
const weightedList = new WeightedList<string>(rng);

Adding and Picking Items

You can add items with specific weights and pick them randomly:

weightedList.add("apple", 10);
weightedList.add("banana", 20);
const randomItem = await weightedList.pickRandom();
console.log(randomItem); // Likely 'banana'

Removing Items

You can remove items either by their object or by their WeightedListItem:

const removedItem = weightedList.remove("apple");
const item = new WeightedListItem("banana", 20);
const removedEntry = weightedList.removeItems(item);

Clearing the List

Clear all items from the list:

weightedList.clear();

API Reference

WeightedList

| Method | Description | | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | | constructor(rng: IRNG) | Creates a new WeightedList instance with a random number generator. Will initialise own RNG if not provided. | | add(item: T, weight: number): WeightedList<T> | Adds an item with a specified weight to the list. | | pickRandom(): T | Picks a random item based on the weights. | | pickRandomEntry(): WeightedListItem<T> | Picks a random entry (item and weight). | | remove(item: T): WeightedListItem<T> \| null | Removes an item by its object value and returns the removed entry. | | removeItems(item: WeightedListItem<T>): WeightedListItem<T> \| null | Removes an item by its WeightedListItem instance. | | clear(): void | Clears all items from the list. |

WeightedListItem

| Property | Description | | ---------------- | -------------------------------------- | | obj: T | The object stored in the list item. | | weight: number | The weight associated with the object. |

Dependency: @rahulv.dev/rng

This package requires @rahulv.dev/rng, a secure random number generator that uses the native Crypto API for generating cryptographic random numbers. You can generate random numbers within specific ranges or for multiple values at once.

Refer to the @rahulv.dev/rng documentation for more details.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Issues

If you encounter any issues, feel free to open a ticket on the issues page.

Author

Rahul Vashishtha – https://rahulv.dev