From 51c8d10415c4f95fc456c4f74aabb8bb4ec1edeb Mon Sep 17 00:00:00 2001 From: Alex Caza Date: Mon, 29 Apr 2024 16:37:47 -0400 Subject: [PATCH] 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 --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b0bb9d..a8c72c5 100644 --- a/README.md +++ b/README.md @@ -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 +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 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 | diff --git a/package.json b/package.json index 0b5d159..ada173a 100644 --- a/package.json +++ b/package.json @@ -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": {