From 1a92073749d541f91ae4e82089cc6488d761c08a Mon Sep 17 00:00:00 2001 From: Alex Caza Date: Fri, 26 Jul 2024 14:33:11 -0400 Subject: [PATCH 1/4] Add mediaType option and enum Allow users to specify one of three media types. We're keeping the type narrow on purpose to avoid potential misuse. --- lib/config.ts | 3 ++- lib/index.ts | 2 +- lib/types.ts | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/config.ts b/lib/config.ts index ac212f8..c2de374 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -1,4 +1,4 @@ -import { WithDefaults, ConfigOptions } from "./types.ts"; +import { WithDefaults, ConfigOptions, MediaType } from "./types.ts"; export const defaults: WithDefaults = { fieldSeparator: ",", @@ -11,6 +11,7 @@ export const defaults: WithDefaults = { showColumnHeaders: true, useTextFile: false, fileExtension: "csv", + mediaType: MediaType.csv, useBom: true, columnHeaders: [], useKeysAsHeaders: false, diff --git a/lib/index.ts b/lib/index.ts index 64bb58c..c745fb7 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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"; diff --git a/lib/types.ts b/lib/types.ts index ffe562a..0cd66d8 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -7,6 +7,12 @@ export type WithDefaults = Required; 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; @@ -18,6 +24,7 @@ export type ConfigOptions = { title?: string; useTextFile?: boolean; fileExtension?: string; + mediaType?: MediaType; useBom?: boolean; columnHeaders?: Array; useKeysAsHeaders?: boolean; From 2eadd7d0b344df451768dd44ec4463803f388fed Mon Sep 17 00:00:00 2001 From: Alex Caza Date: Fri, 26 Jul 2024 14:35:18 -0400 Subject: [PATCH 2/4] Use new mediaType option --- lib/generator.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/generator.ts b/lib/generator.ts index c7175e9..369749d 100644 --- a/lib/generator.ts +++ b/lib/generator.ts @@ -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; From 8456097faecc66619eb04cf5e38415d1326cffe9 Mon Sep 17 00:00:00 2001 From: Alex Caza Date: Fri, 26 Jul 2024 14:57:39 -0400 Subject: [PATCH 3/4] Add tests for mediaType --- lib/__specs__/main.spec.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/__specs__/main.spec.ts b/lib/__specs__/main.spec.ts index b4373fa..e435a7b 100644 --- a/lib/__specs__/main.spec.ts +++ b/lib/__specs__/main.spec.ts @@ -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 = [ @@ -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 = { From 2f98f8e1abc115845ff0b08ae86d2235b4c656a4 Mon Sep 17 00:00:00 2001 From: Alex Caza Date: Fri, 26 Jul 2024 14:57:57 -0400 Subject: [PATCH 4/4] Add @deprecated to useTextFile option --- lib/types.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/types.ts b/lib/types.ts index 0cd66d8..750f1c4 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -22,6 +22,13 @@ 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;