8000 Custom fileextension tweaks by alexcaza · Pull Request #113 · alexcaza/export-to-csv · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Custom fileextension tweaks #113

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

Merged
merged 4 commits into from
Aug 31, 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
23 changes: 22 additions & 1 deletion lib/__specs__/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from "bun:test";
import { mkConfig } from "../config.ts";
import { asBlob, generateCsv } from "../generator.ts";
import { ConfigOptions } from "../types.ts";
import { ConfigOptions, MediaType } from "../types.ts";
import { asString } from "../helpers.ts";

const mockData = [
Expand Down Expand Up @@ -344,6 +344,27 @@ describe("ExportToCsv", () => {
expect(firstLine).toBe("Test Csv 2\r");
});

it("should allow for custom file extensions", () => {
const csvOpts: ConfigOptions = {
mediaType: MediaType.csv,
};
const csvConf = mkConfig(csvOpts);

const txtOpts: ConfigOptions = {
mediaType: MediaType.plain,
};
const txtConf = mkConfig(txtOpts);

const tsvOpts: ConfigOptions = {
mediaType: MediaType.tsv,
};
const tsvConf = mkConfig(tsvOpts);

expect(csvConf.mediaType).toBe(MediaType.csv);
expect(txtConf.mediaType).toBe(MediaType.plain);
expect(tsvConf.mediaType).toBe(MediaType.tsv);
});

describe("asBlob", () => {
it("should construct a valid blob based on options", async () => {
const options: ConfigOptions = {
Expand Down
3 changes: 2 additions & 1 deletion lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WithDefaults, ConfigOptions } from "./types.ts";
import { WithDefaults, ConfigOptions, MediaType } from "./types.ts";

export const defaults: WithDefaults<ConfigOptions> = {
fieldSeparator: ",",
Expand All @@ -11,6 +11,7 @@ export const defaults: WithDefaults<ConfigOptions> = {
showColumnHeaders: true,
useTextFile: false,
fileExtension: "csv",
mediaType: MediaType.csv,
useBom: true,
columnHeaders: [],
useKeysAsHeaders: false,
Expand Down
8 changes: 5 additions & 3 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ export const asBlob =
const withDefaults = mkConfig(config);
const data = unpack(csvOutput);

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

return blob;
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./generator.ts";
export { mkConfig } from "./config.ts";
export { CsvOutput, ConfigOptions, ColumnHeader } from "./types.ts";
export { CsvOutput, ConfigOptions, ColumnHeader, MediaType } from "./types.ts";
export { asString } from "./helpers.ts";
14 changes: 14 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export type WithDefaults<T> = Required<T>;

export type ColumnHeader = string | { key: string; displayLabel: string };

export enum MediaType {
csv = "text/csv",
tsv = "text/tab-separated-values",
plain = "text/plain",
}

export type ConfigOptions = {
filename?: string;
fieldSeparator?: string;
Expand All @@ -16,8 +22,16 @@ export type ConfigOptions = {
showColumnHeaders?: boolean;
showTitle?: boolean;
title?: string;
/**
* Use `fileExtension` instead.
*
* Will be removed in the next major version (`2.x.x`)
*
* @deprecated
*/
useTextFile?: boolean;
fileExtension?: string;
mediaType?: MediaType;
useBom?: boolean;
columnHeaders?: Array<ColumnHeader>;
useKeysAsHeaders?: boolean;
Expand Down
Loading
0