lotto-draw
Version:
A simple tool used to pick random elements from a mutable collection of weighted participants
45 lines (44 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLotto = void 0;
var Lotto_1 = require("./Lotto");
/**
* A function that creates and returns a Lotto instance.
* @param participantsOrOptions An array of initial participants or options relating to the creation of a Lotto instance.
* @returns A new Lotto instance.
*/
function createLotto(participantsOrOptions) {
// If no initial participants or lotto options were provided as an argument then we can just return a new lotto instance now.
if (!participantsOrOptions) {
return new Lotto_1.Lotto();
}
// Check whether we were provided with an array of initial participants or a lotto options object.
if (Array.isArray(participantsOrOptions)) {
// We are dealing with a pre-defined array of participants.
var participants = participantsOrOptions;
var lotto_1 = new Lotto_1.Lotto();
// If the lotto participants have been defined upfront then we will need to add them all to our lotto instance now.
participants.forEach(function (_a) {
var participant = _a[0], tokens = _a[1];
return lotto_1.add(participant, tokens);
});
// Return the Lotto instance.
return lotto_1;
}
else {
// We are dealing with some lotto options.
var random = participantsOrOptions.random, participants = participantsOrOptions.participants;
// Create a Lotto instance passing the custom RNG function to use in place of Math.random() (which could be undefined).
var lotto_2 = new Lotto_1.Lotto(random);
// If the lotto participants have been defined upfront as part of the options then we will need to add them all to our lotto instance now.
if (participants) {
participants.forEach(function (_a) {
var participant = _a[0], tokens = _a[1];
return lotto_2.add(participant, tokens);
});
}
// Return the Lotto instance.
return lotto_2;
}
}
exports.createLotto = createLotto;