index.js
function selectWithValidation(choices) {
	// Validate argument is an array
	if(!choices || !choices.length) {
		throw new Error("Randomly Select: invalid argument, please provide a non-empty array");
	}

	// Validate that:
	// - each array entry has a 'chance' and 'result' property
	// - each 'chance' field is a number
	// - at least one result has a positive non-zero chance
	let atLeastOneChoiceHasNonZeroChance = false;
	for(let i = 0; i < choices.length; i++) {
		if(!exists(choices[i]) || !exists(choices[i].chance) || !exists(choices[i].result)) {
			throw new Error("Randomly Select: invalid argument, each array entry " +
				"must be an object with 'chance' and 'result' properties");
		} else if(typeof choices[i].chance !== 'number' || choices[i].chance < 0) {
			throw new Error("Randomly Select: invalid argument, 'chance' must be a positive number: " +
				JSON.stringify(choices[i].chance));
		} else if(choices[i].chance > 0) {
			atLeastOneChoiceHasNonZeroChance = true;
		}
	}

	if(!atLeastOneChoiceHasNonZeroChance) {
		throw new Error("Randomly Select: invalid arguments, all results have zero weight");
	}

	// Do the actual random selection and return the result
	return selectWithoutValidation(choices);
}
function selectWithValidation(choices) {
	// Validate argument is an array
	if(!choices || !choices.length) {
		throw new Error("Randomly Select: invalid argument, please provide a non-empty array");
	}

	// Validate that:
	// - each array entry has a 'chance' and 'result' property
	// - each 'chance' field is a number
	// - at least one result has a positive non-zero chance
	let atLeastOneChoiceHasNonZeroChance = false;
	for(let i = 0; i < choices.length; i++) {
		if(!exists(choices[i]) || !exists(choices[i].chance) || !exists(choices[i].result)) {
			throw new Error("Randomly Select: invalid argument, each array entry " +
				"must be an object with 'chance' and 'result' properties");
		} else if(typeof choices[i].chance !== 'number' || choices[i].chance < 0) {
			throw new Error("Randomly Select: invalid argument, 'chance' must be a positive number: " +
				JSON.stringify(choices[i].chance));
		} else if(choices[i].chance > 0) {
			atLeastOneChoiceHasNonZeroChance = true;
		}
	}

	if(!atLeastOneChoiceHasNonZeroChance) {
		throw new Error("Randomly Select: invalid arguments, all results have zero weight");
	}

	// Do the actual random selection and return the result
	return selectWithoutValidation(choices);
}
function selectWithValidation ( choices ) { // Validate argument is an array if ( ! choices || ! choices . length ) { throw new Error ( "Randomly Select: invalid argument, please provide a non-empty array" ) ; } // Validate that: // - each array entry has a 'chance' and 'result' property // - each 'chance' field is a number // - at least one result has a positive non-zero chance let atLeastOneChoiceHasNonZeroChance = false ; for ( let i = 0 ; i < choices . length ; i ++ ) { if ( ! exists ( choices [ i ] ) || ! exists ( choices [ i ] . chance ) || ! exists ( choices [ i ] . result ) ) { throw new Error ( "Randomly Select: invalid argument, each array entry " + "must be an object with 'chance' and 'result' properties" ) ; } else if ( typeof choices [ i ] . chance !== 'number' || choices [ i ] . chance < 0 ) { throw new Error ( "Randomly Select: invalid argument, 'chance' must be a positive number: " + JSON . stringify ( choices [ i ] . chance ) ) ; } else if ( choices [ i ] . chance > 0 ) { atLeastOneChoiceHasNonZeroChance = true ; } } if ( ! atLeastOneChoiceHasNonZeroChance ) { throw new Error ( "Randomly Select: invalid arguments, all results have zero weight" ) ; } // Do the actual random selection and return the result return selectWithoutValidation ( choices ) ; }
Performs validation on input before performing the random selection
Performs validation on input before performing the random selection