Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

items-picker

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

items-picker

Get a random combination from a list of items.

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

items-picker

NPM Version build codecov License

Overview

Get a random combination from a list of items.

Installation

You can install this library using npm:

npm install items-picker

Functions

getRandomCombination<T>(items: T[], key: keyof T, maxSum: number, allowDuplicates: boolean = true): T[]

Retrieves a random combination of items from the provided array. The combination's sum is constrained to be less than or equal to maxSum. The function retrieves items until no more valid items can be found.

Parameters

ParameterTypeDescription
itemsT[]Array of items to select from.
keykeyof TThe key of the item object which has a numeric value.
maxSumnumberMaximum allowed sum of the values associated with the key.
allowDuplicatesbooleanFlag indicating whether duplicate items are allowed. Defaults to true.

Returns

An array containing the combination of items based on the constraints.

Usage Example

import { getRandomCombination } from "random-item-picker";

const items = [
  { id: 1, value: 100 },
  { id: 2, value: 200 },
  { id: 3, value: 300 },
];

// Get a random combination of items with a total value of up to 500
const randomCombination = getRandomCombination(items, "value", 500, false);
console.log(randomCombination);

License

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

Keywords

random

FAQs

Package last updated on 03 Sep 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