Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239import { formatBytes, } from '@monochromatic-dev/module-numeric-format/ts';
import type { EstimateRange, RatioRange, Size,} from './types.ts';
/** Percent scale factor for savings (ratio in [0, 1] -> percent in [0, 100]). */const PERCENT = 100;
/** Decimal places retained for the unitless ratio. */const RATIO_DECIMALS = 4;
/** Decimal places retained for the savings percentage. */const SAVINGS_DECIMALS = 1;
/** Powers of ten for {@link roundTo}, indexed by decimal count, composed without magic literals beyond the exempt -2..2 range. */const POW10: Readonly<Record<number, number>> = { 1: 10, 4: 10 * 10 * 10 * 10,};
/** Rounds a number to a fixed number of decimal places. @param value - number to round @param decimals - decimal places to keep; must be a key of {@link POW10} @returns value rounded to `decimals` places @example ```ts roundTo({ value: 0.04421, decimals: 4 }); // 0.0442 ``` */export function roundTo({ value, decimals,}: { readonly value: number; readonly decimals: number},): number { /** Scale factor for the requested precision. */ const scale = POW10[decimals] ?? 1; return Math.round(value * scale,) / scale;}
/** Clamps a number into an inclusive range. @param value - number to clamp @param min - lower bound @param max - upper bound @returns value confined to `[min, max]` @example ```ts clamp({ value: 1.3, min: 0, max: 1 }); // 1 ``` */export function clamp({ value, min, max,}: { readonly value: number; readonly min: number; readonly max: number},): number { return Math.min( max, Math.max( min, value, ), );}
/** Builds a {@link Size} from a raw byte count, attaching the human string via the shared IEC formatter. Negative or fractional inputs are floored to a non-negative integer first. @param bytes - raw byte count @returns size with `bytes` and `human` fields @example ```ts toSize(422); // { bytes: 422, human: '422 B' } ``` */export function toSize(bytes: number,): Size { /** Non-negative integer byte count for display and storage. */ const safe = Math.max( 0, Math.round(bytes,), ); return { bytes: safe, human: formatBytes(safe,), };}
/** Computes the shallow/full ratio range. The denominator's high end yields the ratio's low end and vice versa, since a larger full size means a smaller fraction. Ratio is clamped to `[0, 1]`. @param shallowBytes - exact shallow object-store bytes @param full - fused full-size interval @returns ratio point and bounds, clamped and rounded @example ```ts computeRatio({ shallowBytes: 4_404_019, full: { point: {...}, lo: {...}, hi: {...} } }); ``` */export function computeRatio( { shallowBytes, full, }: { readonly shallowBytes: number; readonly full: EstimateRange },): RatioRange { /** Ratio at a denominator, guarding division by a non-positive full size. @param denominatorBytes - full-size byte count used as the denominator @returns shallow/denominator ratio clamped to `[0, 1]`, or 1 when non-positive @example ```ts ratioAt(full.point.bytes); ``` */ function ratioAt(denominatorBytes: number,): number { if (denominatorBytes <= 0) return 1; return clamp({ value: shallowBytes / denominatorBytes, min: 0, max: 1, },); } return { hi: roundTo({ value: ratioAt(full.lo .bytes,), decimals: RATIO_DECIMALS, },), lo: roundTo({ value: ratioAt(full.hi .bytes,), decimals: RATIO_DECIMALS, },), point: roundTo({ value: ratioAt(full.point .bytes,), decimals: RATIO_DECIMALS, },), };}
/** Computes the savings percentage range from a ratio range: `savings = 1 - ratio`, so the ratio's low end maps to the savings high end. Clamped to `[0, 100]`. @param ratio - shallow/full ratio range @returns savings percentage point and bounds @example ```ts computeSavings({ ratio: { point: 0.044, lo: 0.028, hi: 0.07 } }); ``` */export function computeSavings({ ratio, }: { readonly ratio: RatioRange; },): RatioRange { /** Savings percent at a ratio value. @param ratioValue - shallow/full ratio in `[0, 1]` @returns savings percentage clamped to `[0, 100]` @example ```ts savingsAt(0.044); ``` */ function savingsAt(ratioValue: number,): number { return clamp({ value: (1 - ratioValue) * PERCENT, min: 0, max: PERCENT, },); } return { hi: roundTo({ value: savingsAt(ratio.lo,), decimals: SAVINGS_DECIMALS, },), lo: roundTo({ value: savingsAt(ratio.hi,), decimals: SAVINGS_DECIMALS, },), point: roundTo({ value: savingsAt(ratio.point,), decimals: SAVINGS_DECIMALS, },), };}