Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rahulv.dev/weighted-list

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rahulv.dev/weighted-list

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

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
0
Weekly downloads
 
Created
Source

@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

MethodDescription
constructor(rng: IRNG)Creates a new WeightedList instance with a random number generator.
add(item: T, weight: number): WeightedList<T>Adds an item with a specified weight to the list.
pickRandom(): Promise<T>Asynchronously picks a random item based on the weights.
pickRandomEntry(): Promise<WeightedListItem<T>>Asynchronously picks a random entry (item and weight).
remove(item: T): WeightedListItem<T> | nullRemoves an item by its object value and returns the removed entry.
removeItems(item: WeightedListItem<T>): WeightedListItem<T> | nullRemoves an item by its WeightedListItem instance.
clear(): voidClears all items from the list.

WeightedListItem

PropertyDescription
obj: TThe object stored in the list item.
weight: numberThe 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

Keywords

FAQs

Package last updated on 03 Aug 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc