0.1.1 • Published 10 months ago

items-picker v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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.

0.1.1

10 months ago

0.1.0

11 months ago