{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "money",
  "title": "Money",
  "description": "Money written one way everywhere: an amount in the currency’s smallest unit, its digits as ISO 4217 counts them, and the format for a bill ($10.00) or a pricing page ($10).",
  "registryDependencies": [
    "https://kinoui.com/r/number-format.json"
  ],
  "files": [
    {
      "path": "registry/lib/money.ts",
      "content": "import { numberFormatter } from '@/registry/lib/number-format'\n\n/*\n * Money, written one way everywhere: a pricing page, a seat's price, a cart\n * and a bill. An amount is held in the currency's smallest unit, as payment\n * providers send it, so sums never pick up a stray fraction of a cent.\n */\n\n/** An amount in the currency's smallest unit (cents for USD, yen for JPY), as ISO 4217 counts them. */\ntype Money = { amount: number; currency: string }\n\n/** How many digits the currency's smallest unit has: USD 2, JPY 0, KWD 3. */\nfunction minorDigits(currency: string): number {\n  return (\n    numberFormatter('en-US', { style: 'currency', currency }).resolvedOptions()\n      .maximumFractionDigits ?? 2\n  )\n}\n\n/** The amount in whole units, for the Animated number: 1999 USD is 19.99. */\nfunction majorOf(money: Money): number {\n  return money.amount / 10 ** minorDigits(money.currency)\n}\n\n/**\n * The format for an amount in whole units. `when-needed` drops the cents of a\n * whole amount (`$10`, `$12.50`), as a pricing page or a seat's price reads;\n * `always` keeps them (`$10.00`), as a bill reads.\n */\nfunction moneyFormat(\n  currency: string,\n  amount: number,\n  cents: 'always' | 'when-needed',\n): Intl.NumberFormatOptions {\n  return cents === 'when-needed' && Number.isInteger(amount)\n    ? {\n        style: 'currency',\n        currency,\n        minimumFractionDigits: 0,\n        maximumFractionDigits: 0,\n      }\n    : { style: 'currency', currency }\n}\n\n/** An amount in whole units as text, for prices kept in whole units: 12.5 USD is `$12.50`. */\nfunction formatAmount(\n  amount: number,\n  currency: string,\n  cents: 'always' | 'when-needed',\n  locale?: Intl.LocalesArgument,\n): string {\n  return numberFormatter(locale, moneyFormat(currency, amount, cents)).format(amount)\n}\n\n/** Money as text: 1999 USD is `$19.99`. */\nfunction formatMoney(\n  money: Money,\n  cents: 'always' | 'when-needed',\n  locale?: Intl.LocalesArgument,\n): string {\n  return formatAmount(majorOf(money), money.currency, cents, locale)\n}\n\nexport { formatAmount, formatMoney, majorOf, minorDigits, moneyFormat, type Money }\n",
      "type": "registry:lib",
      "target": "@lib/money.ts"
    }
  ],
  "type": "registry:lib"
}