(rawWeights: number[])
| 1326 | |
| 1327 | return pairs.slice(0, 2); |
| 1328 | } |
| 1329 | |
| 1330 | function normalizeAreaPercents(rawWeights: number[]) { |
| 1331 | const total = rawWeights.reduce((sum, value) => sum + value, 0) || 1; |
| 1332 | // Scale proportionally — do NOT force equal minimums |
| 1333 | // The best-scoring crop should get the most area |
| 1334 | let normalized = rawWeights.map((value) => Math.max(5, Math.round((value / total) * 100))); |
| 1335 | let current = normalized.reduce((sum, value) => sum + value, 0); |
| 1336 | |
| 1337 | // Adjust to sum to 100 |
| 1338 | while (current > 100) { |
| 1339 | // Reduce the smallest non-minimum zone |
| 1340 | const index = normalized.reduce((minIdx, val, i, arr) => val > 5 && val > arr[minIdx] ? minIdx : i, 0); |
| 1341 | normalized[index] -= 1; |
| 1342 | current -= 1; |
| 1343 | } |
| 1344 | |
| 1345 | while (current < 100) { |
| 1346 | // Add to the largest zone (best crop gets more) |
| 1347 | normalized[normalized.indexOf(Math.max(...normalized))] += 1; |
| 1348 | current += 1; |
| 1349 | } |
| 1350 | |
| 1351 | return normalized; |
| 1352 | } |
| 1353 |
no outgoing calls
no test coverage detected