Skip to content

Commit

Permalink
fix: remove unnecessary casts, rounding from series.estimate
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Prodromou <[email protected]>
  • Loading branch information
evanp committed Apr 16, 2024
1 parent 27423d3 commit 5e1619e
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions app/src/util/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ export function estimate(series: { year: number, value: number }[], year: number
if (!next) {
return null
}
let rate = (1.0 * (next.value - first.value)) / (next.year - first.year);
let rate = (next.value - first.value) / (next.year - first.year);
let value = first.value - rate * (first.year - year)
return Math.round(value)
return value
} else if (year > series[series.length - 1].year) { // extrapolate forwards
let last = series[series.length - 1]
let prev = series.findLast(e => last.year - e.year >= year - last.year)
if (!prev) {
return null
}
let rate = (1.0 * (last.value - prev.value)) / (last.year - prev.year);
let rate = (last.value - prev.value) / (last.year - prev.year);
let value = last.value + rate * (year - last.year)
return Math.round(value)
return value
} else { // interpolate
let prev = series.findLast(e => e.year < year)
let next = series.find(e => e.year > year)
if (!prev || !next) {
return null
}
let rate = (1.0 * (next.value - prev.value)) / (next.year - prev.year);
let rate = (next.value - prev.value) / (next.year - prev.year);
let value = prev.value + rate * (year - prev.year)
return Math.round(value)
return value
}

return null;
Expand Down

0 comments on commit 5e1619e

Please sign in to comment.