23 lines
1008 B
JavaScript
23 lines
1008 B
JavaScript
// A small change marker for a value, matching the HUD's summary arrows: a bold
|
|
// green up arrow, a red down arrow, doubled once a big move happened, or an
|
|
// equals sign when nothing moved. Returns null when there is no baseline, so
|
|
// callers can append it only when there is something to compare against.
|
|
|
|
export function trendArrow(current, previous, { doubleAt = null, equalEpsilon = 0.5 } = {}) {
|
|
if (current === undefined || current === null) return null;
|
|
if (previous === undefined || previous === null) return null;
|
|
const diff = current - previous;
|
|
const $arrow = $("<span class='stat-arrow'></span>");
|
|
if (Math.abs(diff) <= equalEpsilon) {
|
|
$arrow.text("=").addClass("equal");
|
|
} else {
|
|
const rising = diff > 0;
|
|
const double = doubleAt !== null && previous !== 0 &&
|
|
Math.abs(diff) / Math.abs(previous) > doubleAt;
|
|
$arrow
|
|
.text(rising ? (double ? "▲▲" : "▲") : (double ? "▼▼" : "▼"))
|
|
.addClass(rising ? "up" : "down");
|
|
}
|
|
return $arrow;
|
|
}
|