{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-format",
  "title": "Number format",
  "description": "One number formatter for each locale and set of options, made once, so every Kino component writes numbers the same way.",
  "dependencies": [
    "@ui-sdk/core"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/lib/number-format.ts",
      "content": "import { numberFormatter as madeOnce } from '@ui-sdk/core/number'\n\n/*\n * One `Intl.NumberFormat` for each locale and set of options, however often\n * they are passed anew, so every part that writes numbers (a rolling number,\n * a chart's axis and tooltip) writes them the same way without making a\n * formatter each render. ui-sdk's Number module makes each one once; this\n * adds what it lacks: the options a formatter was made with, as given, and\n * a file's size as a file manager writes it.\n */\n\nconst optionsOf = new WeakMap<Intl.NumberFormat, Intl.NumberFormatOptions | undefined>()\n\n/** The formatter for `locale` and `format`, made once. */\nexport function numberFormatter(\n  locale: Intl.LocalesArgument,\n  format?: Intl.NumberFormatOptions,\n): Intl.NumberFormat {\n  // ui-sdk takes one tag: a list or an `Intl.Locale` gives the one `Intl` would pick from it.\n  const tag =\n    typeof locale === 'string' || locale === undefined\n      ? locale\n      : Intl.NumberFormat.supportedLocalesOf(locale)[0]\n  const formatter = madeOnce(tag, format)\n  if (!optionsOf.has(formatter)) optionsOf.set(formatter, format)\n  return formatter\n}\n\n/** The options a formatter from `numberFormatter` was made with, as given. */\nexport function formatOptionsOf(\n  formatter: Intl.NumberFormat,\n): Intl.NumberFormatOptions | undefined {\n  return optionsOf.get(formatter)\n}\n\nconst UNITS = ['kilobyte', 'megabyte', 'gigabyte', 'terabyte'] as const\n\n/** A file's size the way a file manager writes it, in steps of 1,000: `820 B`, `2.4 MB`. */\nexport function formatFileSize(bytes: number, locale?: string) {\n  if (bytes < 1000) {\n    return numberFormatter(locale, {\n      style: 'unit',\n      unit: 'byte',\n      unitDisplay: 'narrow',\n    })\n      .format(bytes)\n      .replace(/(\\d)B$/, '$1 B')\n  }\n  let size = bytes / 1000\n  let unit = 0\n  // Up a unit once the rounded number would read 1,000.\n  const digits = (value: number) => (value < 9.95 ? 1 : 0)\n  const rounded = (value: number) => Number(value.toFixed(digits(value)))\n  while (rounded(size) >= 1000 && unit < UNITS.length - 1) {\n    size /= 1000\n    unit += 1\n  }\n  return numberFormatter(locale, {\n    style: 'unit',\n    unit: UNITS[unit],\n    unitDisplay: 'short',\n    maximumFractionDigits: digits(size),\n  }).format(size)\n}\n",
      "type": "registry:lib",
      "target": "@lib/number-format.ts"
    }
  ],
  "type": "registry:lib"
}