Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.3.0 prep #104

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,54 @@ const csvOutputWithNewLine = addNewLine(asString(csvOutput));

The reason the `CsvOutput` type exists is to prevent accidentally passing in a string which wasn't formatted by `generateCsv` to the `download` function.

### Using `generateCsv` output as a `Blob`

A case for this would be using browser extension download methods _instead of_ the supplied `download` function. There may be scenarios where using a `Blob` might be more ergonomic.

```typescript
import { mkConfig, generateCsv, asBlob } from "export-to-csv";

// mkConfig merges your options with the defaults
// and returns WithDefaults<ConfigOptions>
const csvConfig = mkConfig({ useKeysAsHeaders: true });

const mockData = [
{
name: "Rouky",
date: "2023-09-01",
percentage: 0.4,
quoted: '"Pickles"',
},
{
name: "Keiko",
date: "2023-09-01",
percentage: 0.9,
quoted: '"Cactus"',
},
];

// Converts your Array<Object> to a CsvOutput string based on the configs
const csv = generateCsv(csvConfig)(mockData);

// Generate the Blob from the CsvOutput
const blob = asBlob(csvConfig)(csv);

// Requires URL to be available (web workers or client scripts only)
const url = URL.createObjectURL(blob);

// Assuming there's a button with an id of csv in the DOM
const csvBtn = document.querySelector("#csv");

csvBtn.addEventListener("click", () => {
// Use Chrome's downloads API for extensions
chrome.downloads.download({
url,
body: csv,
filename: "chrome-extension-output.csv",
});
});
```

## API

| Option | Default | Type | Description |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "export-to-csv",
"version": "1.2.4",
"version": "1.3.0",
"description": "Easily create CSV data from json collection",
"type": "module",
"repository": {
Expand Down
Loading