Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(self-review): optimise util functions and add comments
Browse files Browse the repository at this point in the history
gilest committed Dec 1, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 4784d7e commit 4da96c3
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
@@ -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,
);
@@ -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 4da96c3

Please sign in to comment.