Skip to content

Commit 51c8d10

Browse files
authored
v1.3.0 prep (#104)
* Update lockfile bun.lockb was referring to incorrect playwright version. * Add asBlob method Useful for cases where you need to have a Blob object. Like downloading using browser extension APIs. * Bump version * Add documentation for `asBlob` * Add clarifying comment * Format markdown
1 parent d906164 commit 51c8d10

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,54 @@ const csvOutputWithNewLine = addNewLine(asString(csvOutput));
135135

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

138+
### Using `generateCsv` output as a `Blob`
139+
140+
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.
141+
142+
```typescript
143+
import { mkConfig, generateCsv, asBlob } from "export-to-csv";
144+
145+
// mkConfig merges your options with the defaults
146+
// and returns WithDefaults<ConfigOptions>
147+
const csvConfig = mkConfig({ useKeysAsHeaders: true });
148+
149+
const mockData = [
150+
{
151+
name: "Rouky",
152+
date: "2023-09-01",
153+
percentage: 0.4,
154+
quoted: '"Pickles"',
155+
},
156+
{
157+
name: "Keiko",
158+
date: "2023-09-01",
159+
percentage: 0.9,
160+
quoted: '"Cactus"',
161+
},
162+
];
163+
164+
// Converts your Array<Object> to a CsvOutput string based on the configs
165+
const csv = generateCsv(csvConfig)(mockData);
166+
167+
// Generate the Blob from the CsvOutput
168+
const blob = asBlob(csvConfig)(csv);
169+
170+
// Requires URL to be available (web workers or client scripts only)
171+
const url = URL.createObjectURL(blob);
172+
173+
// Assuming there's a button with an id of csv in the DOM
174+
const csvBtn = document.querySelector("#csv");
175+
176+
csvBtn.addEventListener("click", () => {
177+
// Use Chrome's downloads API for extensions
178+
chrome.downloads.download({
179+
url,
180+
body: csv,
181+
filename: "chrome-extension-output.csv",
182+
});
183+
});
184+
```
185+
138186
## API
139187

140188
| Option | Default | Type | Description |

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "export-to-csv",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"description": "Easily create CSV data from json collection",
55
"type": "module",
66
"repository": {

0 commit comments

Comments
 (0)