Skip to content

Commit e7607c5

Browse files
authored
Fix issue #93 (#94)
* Fix issue #93 * Bump version
1 parent adc3518 commit e7607c5

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

lib/__specs__/helpers.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ describe("Helpers", () => {
6767
const input = mkCsvOutput("");
6868
const output = asString(addTitle(config)(input));
6969

70-
expect(output).toBe("My title");
70+
expect(output).toBe("My title\r\n");
7171
});
7272

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

78-
expect(output).toBe("My Generated Report");
78+
expect(output).toBe("My Generated Report\r\n");
7979
});
8080

8181
it("should skip title if set to false", () => {

lib/__specs__/main.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,21 @@ describe("ExportToCsv", () => {
258258

259259
expect(firstLine).toBe('"name","age"\r');
260260
});
261+
262+
it("should put the title on the first line", () => {
263+
const options: ConfigOptions = {
264+
title: "Test Csv 2",
265+
showTitle: true,
266+
useBom: false,
267+
showColumnHeaders: true,
268+
columnHeaders: ["name", "age"],
269+
};
270+
271+
const output = asString(generateCsv(options)(mockData));
272+
const firstLine = output.split("\n")[0];
273+
274+
expect(firstLine).toBe("Test Csv 2\r");
275+
});
261276
});
262277

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

433448
expect(firstLine).toBe('"name","age"\r');
434449
});
450+
451+
it("should put the title on the first line", () => {
452+
const options: ConfigOptions = {
453+
title: "Test Csv 2",
454+
showTitle: true,
455+
useBom: false,
456+
showColumnHeaders: true,
457+
columnHeaders: ["name", "age"],
458+
};
459+
460+
const output = asString(generateCsv(options)(mockData));
461+
const firstLine = output.split("\n")[0];
462+
463+
expect(firstLine).toBe("Test Csv 2\r");
464+
});
435465
});

lib/helpers.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export const addBOM =
4040
export const addTitle =
4141
(config: WithDefaults<ConfigOptions>) =>
4242
(output: CsvOutput): CsvOutput =>
43-
config.showTitle ? mkCsvOutput(unpack(output) + config.title) : output;
43+
config.showTitle
44+
? addEndOfLine(mkCsvOutput(unpack(output) + config.title))(mkCsvRow(""))
45+
: output;
4446

4547
export const addEndOfLine =
4648
(output: CsvOutput) =>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "export-to-csv",
3-
"version": "1.2.3",
3+
"version": "1.2.4",
44
"description": "Easily create CSV data from json collection",
55
"type": "module",
66
"repository": {

0 commit comments

Comments
 (0)