formatBytes (lib/format.ts)

invisible_dots · apps/web/src/lib/format.ts · typescript · MIT
ui, tag:knn-classified

Utility functions for formatting bytes, dates, durations, status tones, and USD amounts for UI display.

// Source: https://github.com/feder-cr/invisible_dots | License: MIT | Copyright 2026 feder-cr
// Provided via justclaw.it.com; keep this notice with the code.
import type { Tone } from "./tone";

const UNITS = ["B", "KiB", "MiB", "GiB", "TiB"];

/** Binary units, matching the config's sizes (1gb = 1024^3 bytes). */
export function formatBytes(bytes: number | null | undefined): string {
  if (typeof bytes !== "number" || !Number.isFinite(bytes) || bytes < 0) return "-";
  let value = bytes;
  let unit = 0;
  while (value >= 1024 && unit < UNITS.length - 1) {
    value /= 1024;
    unit++;
  }
  const digits = unit === 0 || value >= 100 ? 0 : 1;
  return `${value.toFixed(digits)} ${UNITS[unit]}`;
}

export function formatDate(value: string | null | undefined): string {
  if (!value) return 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key