Skip to content

Commit cfb35fb

Browse files
authored
Merge pull request alexcaza#6 from devlavshah/feature_for_txt
Issue: 3 : Feature for text file alexcaza#3
2 parents ad2221a + 9238cde commit cfb35fb

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var data = [
4747
showLabels: true,
4848
showTitle: true,
4949
title: 'My Awesome CSV',
50+
useTextFile: false,
5051
useBom: true,
5152
useKeysAsHeaders: true,
5253
// headers: ['Column 1', 'Column 2', etc...] <-- Won't work with useKeysAsHeaders present!
@@ -71,6 +72,7 @@ csvExporter.generateCsv(data);
7172
| **showTitle** | false | Includes the title as the first line in the generated file |
7273
| **title** | 'My Generated Report' | This string will be used as the report title |
7374
| **useBom** | true | If true, adds a BOM character at the start of the CSV to improve file compatibility |
75+
| **useTextFile** | false | If true, returns a `.txt` file instead of `.csv` |
7476
| **useKeysAsHeaders** | false | If true, this will use the keys of the first object in the collection as the column headers|
7577
| **headers** | [] | Expects an array of strings, which if supplied, will be used as the column headers|
7678

export-to-csv.spec.ts

+49
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,54 @@ describe('ExportToCsv', () => {
7272
const exportToCsvInstance = new ExportToCsv(options);
7373
exportToCsvInstance.generateCsv(mockData);
7474

75+
});
76+
});
77+
78+
describe('ExportToCsv As A Text File', () => {
79+
it('should create a comma seperated string', () => {
80+
const options: Options = {
81+
title: "Test Csv 1",
82+
useTextFile: true,
83+
useBom: true,
84+
useKeysAsHeaders: true,
85+
};
86+
87+
const exportToCsvInstance = new ExportToCsv(options);
88+
const string = exportToCsvInstance.generateCsv(mockData, true);
89+
expect(string).toBeTruthy(typeof string === 'string');
90+
});
91+
92+
it('should use keys of first object in collection as headers', () => {
93+
const options: Options = {
94+
filename: "Test Csv 2",
95+
useTextFile: true,
96+
useBom: true,
97+
useKeysAsHeaders: true,
98+
};
99+
100+
const exportToCsvInstance = new ExportToCsv(options);
101+
const string = exportToCsvInstance.generateCsv(mockData, true);
102+
103+
const firstLine = string.split('\n')[0];
104+
const keys = firstLine.split(',').map((s: string) => s.trim());
105+
106+
const mockDataKeys = Object.keys(mockData[0]);
107+
expect(keys).toEqual(mockDataKeys);
108+
});
109+
110+
it('should initiate download through spawned browser', () => {
111+
if (!window) {
112+
pending('it should only initiate download when run in browser context');
113+
}
114+
const options: Options = {
115+
filename: "Test Csv 3",
116+
useTextFile: true,
117+
useBom: true,
118+
useKeysAsHeaders: true
119+
};
120+
121+
const exportToCsvInstance = new ExportToCsv(options);
122+
exportToCsvInstance.generateCsv(mockData);
123+
75124
});
76125
});

export-to-csv.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface Options {
66
showLabels?: boolean;
77
showTitle?: boolean;
88
title?: string;
9+
useTextFile?: boolean,
910
useBom?: boolean;
1011
headers?: string[];
1112
useKeysAsHeaders?: boolean;
@@ -23,6 +24,7 @@ export class CsvConfigConsts {
2324
public static DEFAULT_TITLE = 'My Generated Report';
2425
public static DEFAULT_FILENAME = 'generated';
2526
public static DEFAULT_SHOW_LABELS = false;
27+
public static DEFAULT_USE_TEXT_FILE= false;
2628
public static DEFAULT_USE_BOM = true;
2729
public static DEFAULT_HEADER: string[] = [];
2830
public static DEFAULT_KEYS_AS_HEADERS = false;
@@ -37,6 +39,7 @@ export const ConfigDefaults: Options = {
3739
showLabels: CsvConfigConsts.DEFAULT_SHOW_LABELS,
3840
showTitle: CsvConfigConsts.DEFAULT_SHOW_TITLE,
3941
title: CsvConfigConsts.DEFAULT_TITLE,
42+
useTextFile: CsvConfigConsts.DEFAULT_USE_TEXT_FILE,
4043
useBom: CsvConfigConsts.DEFAULT_USE_BOM,
4144
headers: CsvConfigConsts.DEFAULT_HEADER,
4245
useKeysAsHeaders: CsvConfigConsts.DEFAULT_KEYS_AS_HEADERS,
@@ -104,18 +107,21 @@ export class ExportToCsv {
104107

105108
// Create CSV blob to download if requesting in the browser and the
106109
// consumer doesn't set the shouldReturnCsv param
107-
let blob = new Blob([this._csv], { "type": "text/csv;charset=utf8;" });
110+
const FileType = this._options.useTextFile ? 'plain' : 'csv';
111+
const fileExtension = this._options.useTextFile ? '.txt' : '.csv';
112+
let blob = new Blob([this._csv], { "type": "text/" + FileType + ";charset=utf8;" });
108113

109114
if (navigator.msSaveBlob) {
110-
let filename = this._options.filename.replace(/ /g, "_") + ".csv";
115+
let filename = this._options.filename.replace(/ /g, "_") + fileExtension;
111116
navigator.msSaveBlob(blob, filename);
112117
} else {
113-
let uri = 'data:attachment/csv;charset=utf-8,' + encodeURI(this._csv);
118+
const attachmentType = this._options.useTextFile ? 'text' : 'csv';
119+
let uri = 'data:attachment/'+ attachmentType +';charset=utf-8,' + encodeURI(this._csv);
114120
let link = document.createElement("a");
115121
link.href = URL.createObjectURL(blob);
116122

117123
link.setAttribute('visibility', 'hidden');
118-
link.download = this._options.filename.replace(/ /g, "_") + ".csv";
124+
link.download = this._options.filename.replace(/ /g, "_") + fileExtension;
119125

120126
document.body.appendChild(link);
121127
link.click();

0 commit comments

Comments
 (0)