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

Fix issue #93 #94

Merged
merged 2 commits into from
Mar 7, 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
4 changes: 2 additions & 2 deletions lib/__specs__/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ describe("Helpers", () => {
const input = mkCsvOutput("");
const output = asString(addTitle(config)(input));

expect(output).toBe("My title");
expect(output).toBe("My title\r\n");
});

it("should add use default if set to true without title given", () => {
const config = mkConfig({ showTitle: true });
const input = mkCsvOutput("");
const output = asString(addTitle(config)(input));

expect(output).toBe("My Generated Report");
expect(output).toBe("My Generated Report\r\n");
});

it("should skip title if set to false", () => {
Expand Down
30 changes: 30 additions & 0 deletions lib/__specs__/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ describe("ExportToCsv", () => {

expect(firstLine).toBe('"name","age"\r');
});

it("should put the title on the first line", () => {
const options: ConfigOptions = {
title: "Test Csv 2",
showTitle: true,
useBom: false,
showColumnHeaders: true,
columnHeaders: ["name", "age"],
};

const output = asString(generateCsv(options)(mockData));
const firstLine = output.split("\n")[0];

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

describe("ExportToCsv As A Text File", () => {
Expand Down Expand Up @@ -432,4 +447,19 @@ describe("ExportToCsv As A Text File", () => {

expect(firstLine).toBe('"name","age"\r');
});

it("should put the title on the first line", () => {
const options: ConfigOptions = {
title: "Test Csv 2",
showTitle: true,
useBom: false,
showColumnHeaders: true,
columnHeaders: ["name", "age"],
};

const output = asString(generateCsv(options)(mockData));
const firstLine = output.split("\n")[0];

expect(firstLine).toBe("Test Csv 2\r");
});
});
4 changes: 3 additions & 1 deletion lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export const addBOM =
export const addTitle =
(config: WithDefaults<ConfigOptions>) =>
(output: CsvOutput): CsvOutput =>
config.showTitle ? mkCsvOutput(unpack(output) + config.title) : output;
config.showTitle
? addEndOfLine(mkCsvOutput(unpack(output) + config.title))(mkCsvRow(""))
: output;

export const addEndOfLine =
(output: CsvOutput) =>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "export-to-csv",
"version": "1.2.3",
"version": "1.2.4",
"description": "Easily create CSV data from json collection",
"type": "module",
"repository": {
Expand Down
Loading