{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-csv",
  "type": "registry:lib",
  "title": "core/csv",
  "description": "Framework-free behaviour shared by the ui-* components (csv); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [
    "@kanok-ui/core-table",
    "@kanok-ui/core-timers"
  ],
  "files": [
    {
      "path": "src/core/csv.ts",
      "type": "registry:lib",
      "target": "components/ui/core/csv.ts",
      "content": "/**\n * Table rows as CSV (RFC 4180) that Excel and Google Sheets open as typed: every cell quoted,\n * CRLF line ends, and — on download — a UTF-8 byte order mark so Excel reads Thai text right.\n * Text that a spreadsheet would run as a formula (starting with =, +, -, @, tab or CR) gets a\n * leading apostrophe (OWASP CSV injection); numbers stay numbers. No Carbon counterpart.\n */\n\nimport { cellText, type TableRow } from './table.ts';\nimport { later } from './timers.ts';\n\n/** A CSV column: the row property, its title, and — for a select column — the labels to write instead of the values. */\nexport interface CsvColumn {\n    key: string;\n    header: string;\n    options?: ReadonlyArray<{ value: string; label: string }>;\n}\n\nconst FORMULA_START = /^[=+\\-@\\t\\r]/;\n\n/** One cell, quoted; text a spreadsheet would run as a formula is defused with a leading apostrophe. */\nexport function csvCell(value: unknown): string {\n    let text = cellText(value);\n    if (typeof value !== 'number' && FORMULA_START.test(text)) text = `'${text}`;\n    return `\"${text.replaceAll('\"', '\"\"')}\"`;\n}\n\n/** What a cell writes: a select column's label, else the value. */\nfunction cellValue(row: TableRow, column: CsvColumn): unknown {\n    const value = row[column.key];\n    if (!column.options) return value;\n    return column.options.find((o) => o.value === cellText(value))?.label ?? value;\n}\n\n/** The header line and one line per row, CRLF between them; `cell` replaces what a cell writes. */\nexport function toCsv<R extends TableRow>(columns: readonly CsvColumn[], rows: readonly R[], cell?: (row: R, column: CsvColumn) => unknown): string {\n    const line = (cells: readonly unknown[]) => cells.map(csvCell).join(',');\n    const lines = [line(columns.map((c) => c.header)), ...rows.map((row) => line(columns.map((c) => (cell ? cell(row, c) : cellValue(row, c)))))];\n    return lines.join('\\r\\n');\n}\n\n/** A file name for the download: the given name (or \"table\") without characters file systems refuse, ending in `.csv`. */\nexport function csvFileName(name: string | undefined): string {\n    const base = (name ?? '')\n        .replace(/\\.csv$/i, '')\n        .replaceAll(/[\\\\/:*?\"<>|]/g, ' ')\n        .replaceAll(/\\s+/g, ' ')\n        .trim();\n    return `${base || 'table'}.csv`;\n}\n\n/** The browser APIs a download uses, typed here: the core compiles without the DOM lib (tsconfig.core.json). */\ninterface DownloadHost {\n    URL: { createObjectURL(blob: unknown): string; revokeObjectURL(url: string): void };\n    Blob: new (parts: string[], options: { type: string }) => unknown;\n    document: {\n        createElement(tag: 'a'): { href: string; download: string; hidden: boolean; click(): void; remove(): void };\n        body: { append(node: unknown): void };\n    };\n}\n\n/** Saves `csv` as a file (UTF-8 with a byte order mark, for Excel). Browser only. */\nexport function downloadCsv(csv: string, fileName: string) {\n    const { URL, Blob, document } = globalThis as unknown as DownloadHost;\n    const url = URL.createObjectURL(new Blob(['\\uFEFF', csv], { type: 'text/csv;charset=utf-8' }));\n    const link = Object.assign(document.createElement('a'), { href: url, download: csvFileName(fileName) });\n    link.hidden = true;\n    document.body.append(link);\n    link.click();\n    link.remove();\n    // Safari reads the blob after the click returns\n    later(() => URL.revokeObjectURL(url), 1000);\n}\n"
    }
  ]
}
