chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
// Words and numbers as the components write them: dates in the reader's
// locale and time zone, sizes, counts, and which URLs may become links.
// Given `fixed`, a date or a count is written as it is on any machine
// (`en-US`, in UTC): a page drawn on a server, as the browser writes it
// until it has hydrated the page, so the two agree.
/** A thing made as it is first asked for, then kept. */
function once<T>(make: () => T): () => T {
let made: T | undefined;
return () => (made ??= make());
}
/** The formats dates and counts are written in, in `locale` and `timeZone` (the machine's where not given): each made as it is first asked for, since making one takes a while (a phone's few milliseconds, as the page's code first runs) and a page uses few. */
function formats(locale?: string, timeZone?: string) {
return {
dateTime: once(() => new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', timeZone })),
date: once(() => new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeZone })),
day: once(() => new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric', timeZone })),
dayOfYear: once(() => new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric', year: 'numeric', timeZone })),
compact: once(() => new Intl.NumberFormat(locale, { notation: 'compact', maximumFractionDigits: 1 }))
};
}
const reader = formats();
const anywhere = formats('en-US', 'UTC');
/** A timestamp as a local date and time, or null when it is no date. */
export function formatDateTime(iso: string, fixed = false): string | null {
const ms = Date.parse(iso);
return Number.isNaN(ms) ? null : (fixed ? anywhere : reader).dateTime().format(ms);
}
/** A timestamp as a local date, or null when it is no date. */
export function formatDate(iso: string | null, fixed = false): string | null {
if (iso === null) return null;
const ms = Date.parse(iso);
return Number.isNaN(ms) ? null : (fixed ? anywhere : reader).date().format(ms);
}
const DAY = 86_400;
/** A timestamp in a few characters, as a feed says when: `now` under a
* minute ago (or under a minute ahead: a clock a few seconds behind the one
* that wrote it), `5m`, `3h`, `2d`; a week or more ago, or a minute or more
* ahead, the day (`Nov 15`, with the year when it is not this one). Null
* when it is no date. `now` is the clock it is read against. */
export function formatShortTime(iso: string, now: number = Date.now(), fixed = false): string | null {
const ms = Date.parse(iso);
if (Number.isNaN(ms)) return null;
const seconds = (now - ms) / 1000;
if (seconds > -60 && seconds < 7 * DAY) {
if (seconds < 60) return 'now';
if (seconds < 3600) return `${Math.floor(seconds / 60)}m`;
if (seconds < DAY) return `${Math.floor(seconds / 3600)}h`;
return `${Math.floor(seconds / DAY)}d`;
}
const year = (at: number) => (fixed ? new Date(at).getUTCFullYear() : new Date(at).getFullYear());
const { day, dayOfYear } = fixed ? anywhere : reader;
return (year(ms) === year(now) ? day : dayOfYear)().format(ms);
}
/** A size in bytes, in the unit that reads best. */
export function formatBytes(size: number): string {
if (size < 1024) return `${size} B`;
const units = ['KB', 'MB', 'GB'];
let value = size / 1024;
let unit = 0;
while (value >= 1024 && unit < units.length - 1) {
value /= 1024;
unit += 1;
}
return `${value.toFixed(value < 10 ? 1 : 0)} ${units[unit]}`;
}
/** A count in a few characters: `17`, `1.2K`. */
export function formatCompact(count: number, fixed = false): string {
return (fixed ? anywhere : reader).compact().format(count);
}
/** A length in seconds as a clock reads it: `3:07`, or `1:02:45`. */
export function formatDuration(seconds: number): string {
const total = Math.max(0, Math.floor(seconds));
const s = String(total % 60).padStart(2, '0');
const m = Math.floor(total / 60) % 60;
const h = Math.floor(total / 3600);
return h > 0 ? `${h}:${String(m).padStart(2, '0')}:${s}` : `${m}:${s}`;
}
/** Whether a string is an http or https URL: the only kind that becomes a
* link, so no record can put `javascript:` or the like under a click. */
export function httpUrl(text: string): boolean {
try {
const { protocol } = new URL(text);
return protocol === 'https:' || protocol === 'http:';
} catch {
return false;
}
}