csv-dl
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

csv-dl

CSV for the frontend.

Description

A small library that converts a JavaScript object to a CSV file download in the browser.

The CSV file format is based on RFC 4180.

Installation

npm install csv-dl

Usage

import downloadCsv from "download-csv";

// Get your data.
const data = [
  { n: 1, s: "one" },
  { n: 2, s: "two" },
  { n: 3, s: "three" },
];

// Define your column layout.
const columns = [
  // The order here is the same as the output.
  { key: "s", title: "Word" },
  // It works with strings and numbers (and any value).
  { key: "n", title: "Number" },
  // You can use the same key twice and modify the value.
  { key: "n", title: "Doubled", value: (n) => n * 2 },
];

// Generate the CSV file and begin the download in the browser.
downloadCsv({
  // Columns from above.
  columns,
  // Data from above.
  data,
  // The ".csv" will be appended if you leave it off.
  fileName: "My CSV File",
  // By default the separator is ",", but you can change it to whatever you want.
  separator: ",",
});

Readme

Keywords

none

Package Sidebar

Install

npm i csv-dl

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

34.1 kB

Total Files

21

Last publish

Collaborators

  • brettk-dev