Files
stats/packages/common/src/get-previous-metric.ts
Carl-Gerhard Lindesvärd 32e91959f6 chore(root): migrate to biome
2024-09-18 23:46:11 +02:00

40 lines
757 B
TypeScript

import { isNil } from 'ramda';
import type { PreviousValue } from '@openpanel/validation';
import { round } from './math';
export function getPreviousMetric(
current: number,
previous: number | null | undefined,
): PreviousValue {
if (isNil(previous)) {
return undefined;
}
const diff = round(
((current > previous
? current / previous
: current < previous
? previous / current
: 0) -
1) *
100,
1,
);
return {
diff:
Number.isNaN(diff) || !Number.isFinite(diff) || current === previous
? null
: diff,
state:
current > previous
? 'positive'
: current < previous
? 'negative'
: 'neutral',
value: previous,
};
}