From 7b2d9365a04f0191e35f981d0c7298b54e172afb Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 20 Dec 2021 23:52:24 -0500 Subject: [PATCH] Fix undefined issue --- src/routes/getSkipSegments.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index d1d543d..93812d2 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -177,9 +177,17 @@ function getWeightedRandomChoice(choices: T[], amountOf weight: number } + let forceIncludedChoices: T[] = []; + let filteredChoices = choices; + if (predicate) { + const splitArray = partition(choices, predicate); + filteredChoices = splitArray[0]; + forceIncludedChoices = splitArray[1]; + } + //assign a weight to each choice let totalWeight = 0; - let choicesWithWeights: TWithWeight[] = choices.map(choice => { + const choicesWithWeights: TWithWeight[] = filteredChoices.map(choice => { const boost = Math.min(choice.reputation, 4); //The 3 makes -2 the minimum votes before being ignored completely @@ -190,13 +198,6 @@ function getWeightedRandomChoice(choices: T[], amountOf return { ...choice, weight }; }); - let forceIncludedChoices: T[] = []; - if (predicate) { - const splitArray = partition(choicesWithWeights, predicate); - choicesWithWeights = splitArray[0]; - forceIncludedChoices = splitArray[1]; - } - // Nothing to filter for if (amountOfChoices >= choicesWithWeights.length) { return choices;