most-weighted-value.ts (756B)
1 import { WeightedValue } from './weighted-value'; 2 3 /** 4 * Returns the most weighted value 5 */ 6 export function mostWeightedValue(values: WeightedValue[], keepDuplicate = true) { 7 let chosen: WeightedValue = { weight: -1, value: undefined }; 8 let matchesFound = 0; 9 for(const weightedValue of values) { 10 11 // Track if there are equals 12 if (weightedValue.weight == chosen.weight) { 13 matchesFound++; 14 } 15 16 // Skip lte 17 if (weightedValue.weight <= chosen.weight) { 18 continue; 19 } 20 21 // Found a new high 22 chosen = weightedValue; 23 matchesFound = 0; 24 } 25 26 // equal weights may need to be discarded 27 if (matchesFound && (!keepDuplicate)) { 28 return undefined; 29 } 30 31 // Return highest weighted value 32 return chosen.value; 33 }