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

As blob feature #103

Merged
merged 2 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
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading