lotto-draw
Version:
A simple tool used to pick random elements from a mutable collection of weighted participants
56 lines (55 loc) • 2.62 kB
TypeScript
/**
* The options that can be supplied when drawing a single winning ticket.
*/
export declare type DrawOptions = {
/** Whether the ticket is redrawable. Defaults to 'true'. */
redrawable?: boolean;
};
/**
* The options that can be supplied when drawing multiple winning tickets.
*/
export declare type DrawMultipleOptions = DrawOptions & {
/** Whether duplicate entries of participants with multiple winning tickets should be removed from the result. Defaults to 'false'. */
unique?: boolean;
};
/**
* Represents a lotto consisting of a number of pickable ticket-holding participants.
*/
export declare class Lotto<TParticipant> {
/** The array of participants that are holding tickets in the lotto. */
private _participants;
/** The custom RNG to use in place of Math.random(). */
private readonly _customRandom;
/**
* Creates a new instance of Lotto.
* @param customRandom The custom RNG to use in place of Math.random().
*/
constructor(customRandom?: () => number);
/**
* Adds a participant with the specified number of tickets, or adds to the participant ticket count if the participant already holds tickets.
* @param participant The participant to add or to increase the ticket count for if they already hold tickets.
* @param tickets The number of tickets, defaults to 1.
* @returns The Lotto instance.
*/
add(participant: TParticipant, tickets?: number): Lotto<TParticipant>;
/**
* Removes the specified number of tickets for the given participant from the draw, or all tickets if a ticket number is not defined.
* @param participant The participant to remove tickets for.
* @param tickets The number of tickets to remove, or undefined if all tickets are to be removed.
* @returns The Lotto instance.
*/
remove(participant: TParticipant, tickets?: number): Lotto<TParticipant>;
/**
* Draw a winning ticket and return the participant that holds the ticket.
* @param options The draw options.
* @returns The participant that holds the winning ticket.
*/
draw(options?: DrawOptions): TParticipant | null;
/**
* Draws multiple winning tickets and return an array of the participants that hold the winning tickets.
* @param tickets The number of winning tickets to draw.
* @param options The draw multiple options.
* @returns An array of the participants that hold the winning tickets.
*/
drawMultiple(tickets: number, options?: DrawMultipleOptions): TParticipant[];
}