Skip to content

Commit

Permalink
chore(self-review): optimise util functions and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gilest committed Dec 1, 2023
1 parent 6a1c357 commit 7dd05e9
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions ember-file-upload/src/system/rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ export function estimatedRate(allRates: number[]): number {
const rates = allRates.slice(CALCULATE_FROM_LAST * -1).reverse();
const weights = generateWeights(rates.length);

// Multiply each rate by its respective weight for a weighted average
return rates.reduce((acc, rate, index) => {
const weight = weights[index] as number;
return acc + rate * weight;
}, 0);
}

export function generateWeights(totalRates: number): number[] {
const proportions: number[] = [];

Array.from({ length: totalRates }).forEach((_, index) => {
proportions.push(proportionForPosition(index + 1));
// Generate an array the same length as totalRates filled with proportioanl weights
const proportions = Array.from({ length: totalRates }).map((_, index) => {
return proportionForPosition(index + 1);
});

const proportionTotal = proportions.reduce((acc, value) => acc + value, 0);
// Convert proportional weights to real weights by division
const realWeights = proportions.map(
(proportion) => proportion / proportionTotal,
);
Expand All @@ -38,9 +39,7 @@ export function generateWeights(totalRates: number): number[] {

export function proportionForPosition(position: number) {
for (const { threshold, proportion } of THRESHOLDS) {
if (position <= threshold) {
return proportion;
}
if (position <= threshold) return proportion;
}
return DEFAULT_PROPORTION;
}

0 comments on commit 7dd05e9

Please sign in to comment.