| 93 | } |
| 94 | |
| 95 | function parseScoringParameters<T extends { weight: number }>( |
| 96 | refs: T[], |
| 97 | scoreFn: (ref: T) => number, |
| 98 | ): { weight: number; score: number }[] { |
| 99 | if (refs.length === 0) { |
| 100 | throw new Error('Reference array cannot be empty.'); |
| 101 | } |
| 102 | |
| 103 | if (refs.some(ref => ref.weight < 0)) { |
| 104 | throw new Error('Weight cannot be negative.'); |
| 105 | } |
| 106 | |
| 107 | if (refs.every(ref => ref.weight === 0)) { |
| 108 | throw new Error('All references cannot have zero weight.'); |
| 109 | } |
| 110 | |
| 111 | const scoredRefs = refs.map(ref => ({ |
| 112 | weight: ref.weight, |
| 113 | score: scoreFn(ref), |
| 114 | })); |
| 115 | |
| 116 | if (scoredRefs.some(ref => ref.score < 0 || ref.score > 1)) { |
| 117 | throw new Error('All scores must be in range 0-1.'); |
| 118 | } |
| 119 | |
| 120 | return scoredRefs; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets audit score to 1 if it meets target. |