{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "format",
  "title": "format",
  "description": "Formats dates, byte sizes and large numbers.",
  "files": [
    {
      "path": "lib/format.ts",
      "content": "function formatDate(\n  date: Date | string | number,\n  options: Intl.DateTimeFormatOptions = {},\n  locale = \"en-US\",\n) {\n  const dateOnly =\n    typeof date === \"string\" ? /^\\d{4}-\\d{2}-(\\d{2})$/.exec(date) : null;\n  const value = new Date(date);\n  // `Date` rolls an impossible day like 2026-02-30 over into the next month.\n  if (\n    Number.isNaN(value.getTime()) ||\n    (dateOnly && value.getUTCDate() !== Number(dateOnly[1]))\n  )\n    return \"\";\n\n  // A date-only string is read as UTC midnight, so it keeps its day only when\n  // shown in UTC, whatever the runtime's or the caller's time zone.\n  const timeZone = dateOnly ? \"UTC\" : options.timeZone;\n  // `dateStyle` and `timeStyle` can't be combined with the fields below.\n  const hasStyle =\n    options.dateStyle !== undefined || options.timeStyle !== undefined;\n  return new Intl.DateTimeFormat(\n    locale,\n    hasStyle\n      ? { ...options, timeZone }\n      : {\n          month: \"long\",\n          day: \"numeric\",\n          year: \"numeric\",\n          ...options,\n          timeZone,\n        },\n  ).format(value);\n}\n\nfunction formatBytes(bytes: number, decimals = 0) {\n  const units = [\"B\", \"KB\", \"MB\", \"GB\", \"TB\"];\n  if (!Number.isFinite(bytes) || bytes < 1) return `0 ${units[0]}`;\n\n  let index = Math.min(\n    Math.floor(Math.log(bytes) / Math.log(1024)),\n    units.length - 1,\n  );\n  let value = bytes / 1024 ** index;\n  // Rounding can reach the next unit, like 1023.9 KB.\n  if (\n    index < units.length - 1 &&\n    Number(value.toFixed(index === 0 ? 0 : decimals)) >= 1024\n  ) {\n    index += 1;\n    value /= 1024;\n  }\n  return `${value.toFixed(index === 0 ? 0 : decimals)} ${units[index]}`;\n}\n\nfunction formatCompactNumber(\n  value: number,\n  options: Intl.NumberFormatOptions = {},\n  locale = \"en-US\",\n) {\n  return new Intl.NumberFormat(locale, {\n    notation: \"compact\",\n    maximumFractionDigits: Math.max(1, options.minimumFractionDigits ?? 0),\n    ...options,\n  }).format(value);\n}\n\nexport { formatBytes, formatCompactNumber, formatDate };\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}