Skip to content

Commit

Permalink
v1.3.0 prep (#104)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexcaza committed Apr 29, 2024
1 parent d906164 commit 51c8d10
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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

0 comments on commit 51c8d10

Please sign in to comment.