Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

822 B
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { downloadText } from "../download";
import { CSV_CONTENT_TYPE, type EncodeCsvOptions, encodeCsv } from "./csv";

/** Encode object rows and download them as `filename` (browser only). */
export function downloadCsv(
  rows: Record<string, unknown>[],
  filename: string,
  options?: EncodeCsvOptions
): void {
  // No rows and no explicit fields would produce a headerless empty file —
  // never useful. Explicit fields still download (template/header-only export).
  if (rows.length === 0 && !options?.fields) return;
  downloadText(encodeCsv(rows, options), filename, CSV_CONTENT_TYPE);
}