wealth/packages/island/src/organisms/creator-stats/utils.ts

103 lines
3.1 KiB
TypeScript
Raw Normal View History

import { iso, percent } from '@lumina/tools/format';
import {
COMPARISON_MIN_COUNT,
COMPARISON_THRESHOLD,
MIN_STEP_SPAN,
TREND_STEP,
TREND_WINDOW,
} from './constants';
import type {
FormCurveGeometry,
Streak,
TrendComparison,
TrendOrder,
TrendOutcome,
} from './types';
/** The last 10 outcomes, oldest first, whatever order the API sends; anything unknown runs flat. */
export function recentTrend(
trend: readonly TrendOutcome[],
order: TrendOrder,
): TrendOutcome[] {
const recent =
order === 'newest-first'
? trend.slice(0, TREND_WINDOW).reverse()
: trend.slice(-TREND_WINDOW);
return recent.map((outcome) =>
Object.hasOwn(TREND_STEP, outcome) ? outcome : 'breakeven',
);
}
export function countWins(trend: readonly TrendOutcome[]): number {
return trend.filter((outcome) => outcome === 'win').length;
}
/** Two decimals keep the path short without moving a point by a visible amount. */
function round(value: number): number {
return Math.round(value * 100) / 100;
}
/**
* The form curve in a 0–100 viewBox stretched to the plot. x runs from 100 (oldest, the RTL start)
* to 0 (newest); y is the running sum of steps, centred in a span of at least four steps.
*/
export function formCurve(trend: readonly TrendOutcome[]): FormCurveGeometry {
const values = [0];
let value = 0;
for (const outcome of trend) {
value += TREND_STEP[outcome];
values.push(value);
}
const low = Math.min(...values);
const high = Math.max(...values);
const span = Math.max(high - low, MIN_STEP_SPAN);
const base = low - (span - (high - low)) / 2;
const x = (index: number) => round(100 - (index / trend.length) * 100);
const y = (step: number) => round((1 - (step - base) / span) * 100);
const points = values.map((step, index): [number, number] => [
x(index),
y(step),
]);
const line = points
.map(([px, py], index) => `${index ? 'L' : 'M'}${px} ${py}`)
.join(' ');
const [newestX] = points[points.length - 1];
return {
points,
area: `${line} L${newestX} 100 L100 100 Z`,
baseline: y(0),
};
}
/** Recent form against the all-time rate, or null below five predictions. */
export function compareRecent(
trend: readonly TrendOutcome[],
winRate: number,
): TrendComparison | null {
if (trend.length < COMPARISON_MIN_COUNT) return null;
const delta = (countWins(trend) / trend.length) * 100 - winRate;
if (delta >= COMPARISON_THRESHOLD) return 'above';
if (delta <= -COMPARISON_THRESHOLD) return 'below';
return 'level';
}
/** The run of identical outcomes counted back from the newest. */
export function currentStreak(trend: readonly TrendOutcome[]): Streak | null {
const outcome = trend.at(-1);
if (!outcome) return null;
let count = 0;
for (let index = trend.length - 1; trend[index] === outcome; index--) {
count++;
}
return { outcome, count };
}
/** The all-time rate in the insight: one decimal only when it has one, `62٫9٪` or `60٪`. */
export function rateText(value: number): string {
const rounded = Math.round(value * 10) / 10;
return iso(percent(rounded, rounded % 1 ? 1 : 0));
}