Skip to content

Commit

Permalink
Add asBlob method
Browse files Browse the repository at this point in the history
Useful for cases where you need to have a Blob object. Like downloading using browser extension APIs.
  • Loading branch information
alexcaza committed Apr 29, 2024
1 parent 15963a2 commit 38e81d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
22 changes: 21 additions & 1 deletion lib/__specs__/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from "bun:test";
import { mkConfig } from "../config.ts";
import { generateCsv } from "../generator.ts";
import { asBlob, generateCsv } from "../generator.ts";
import { ConfigOptions } from "../types.ts";
import { asString } from "../helpers.ts";

Expand Down Expand Up @@ -343,4 +343,24 @@ describe("ExportToCsv", () => {

expect(firstLine).toBe("Test Csv 2\r");
});

describe("asBlob", () => {
it("should construct a valid blob based on options", async () => {
const options: ConfigOptions = {
title: "Test Csv 2",
showTitle: true,
useBom: false,
showColumnHeaders: true,
columnHeaders: ["name", "age"],
};

const output = generateCsv(options)(mockData);
const blob = asBlob(options)(output);
const text = await blob.text();

expect(blob.type).toBe("text/csv;charset=utf8;");
expect(text.split("\n")[0]).toBe("Test Csv 2\r");
expect(blob.size).toBe(65);
});
});
});
35 changes: 26 additions & 9 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ export const generateCsv =
return output;
};

/**
* Returns the Blob representation of the CsvOutput generated
* by `generateCsv`. This is useful if you need to access the
* data for downloading in other contexts; like browser extensions.
*/
export const asBlob =
(config: ConfigOptions) =>
(csvOutput: CsvOutput): Blob => {
const withDefaults = mkConfig(config);
const data = unpack(csvOutput);

// Create blob from CsvOutput either as text or csv file.
const fileType = withDefaults.useTextFile ? "plain" : "csv";
const blob = new Blob([data], {
type: `text/${fileType};charset=utf8;`,
});

return blob;
};

/**
*
* **Only supported in browser environment.**
Expand All @@ -77,20 +97,17 @@ export const download =
);
}

const withDefaults = mkConfig(config);
const data = unpack(csvOutput);

// Create blob from CsvOutput either as text or csv file.
const fileType = withDefaults.useTextFile ? "plain" : "csv";
const blob = asBlob(config)(csvOutput);

const withDefaults = mkConfig(config);
const fileExtension = withDefaults.useTextFile ? "txt" : "csv";
let blob = new Blob([data], {
type: `text/${fileType};charset=utf8;`,
});
const fileName = `${withDefaults.filename}.${fileExtension}`;

// Create link element in the browser and set the download
// attribute to the blob that was created.
let link = document.createElement("a");
link.download = `${withDefaults.filename}.${fileExtension}`;
const link = document.createElement("a");
link.download = fileName;
link.href = URL.createObjectURL(blob);

// Ensure the link isn't visible to the user or cause layout shifts.
Expand Down

0 comments on commit 38e81d8

Please sign in to comment.