Skip to content

Commit

Permalink
Merge pull request #26 from rob-moore/master
Browse files Browse the repository at this point in the history
added option to change null values to empty strings, fixed typo
  • Loading branch information
alhazmy13 authored Jan 13, 2019
2 parents be92166 + 1c8da22 commit 721f37a
Show file tree
Hide file tree
Showing 4 changed files with 1,109 additions and 1,086 deletions.
10 changes: 10 additions & 0 deletions Angular5-csv.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ describe('Component: Angular2Csv', () => {
expect(labels[0]).toEqual('name');
expect(labels[1]).toEqual('age');
});

it('should return nulls as empty strings if the options is selected', () => {
let component = new Angular5Csv([{name: null, age: null}], 'My Report', {useBom: false, nullToEmptyString: true});
let csv = component['csv'];
let csv_rows = csv.split(CsvConfigConsts.EOL);
let first_row = csv_rows[0].replace(/"/g, '').split(',');
expect(first_row[0]).toEqual('');
expect(first_row[1]).toBe('');

})
});
16 changes: 13 additions & 3 deletions Angular5-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Options {
useBom: boolean;
headers: string[];
noDownload: boolean;
nullToEmptyString: boolean;
}

export class CsvConfigConsts {
Expand All @@ -26,6 +27,7 @@ export class CsvConfigConsts {
public static DEFAULT_USE_BOM = true;
public static DEFAULT_HEADER: any[] = [];
public static DEFAULT_NO_DOWNLOAD = false;
public static DEFAULT_NULL_TO_EMPTY_STRING = false;

}

Expand All @@ -39,7 +41,8 @@ export const ConfigDefaults: Options = {
title: CsvConfigConsts.DEFAULT_TITLE,
useBom: CsvConfigConsts.DEFAULT_USE_BOM,
headers: CsvConfigConsts.DEFAULT_HEADER,
noDownload: CsvConfigConsts.DEFAULT_NO_DOWNLOAD
noDownload: CsvConfigConsts.DEFAULT_NO_DOWNLOAD,
nullToEmptyString: CsvConfigConsts.DEFAULT_NULL_TO_EMPTY_STRING
};

export class Angular5Csv {
Expand Down Expand Up @@ -130,7 +133,7 @@ export class Angular5Csv {
for (let i = 0; i < this.data.length; i++) {
let row = "";
for (const index in this.data[i]) {
row += this.formartData(this.data[i][index]) + this._options.fieldSeparator;
row += this.formatData(this.data[i][index]) + this._options.fieldSeparator;
}

row = row.slice(0, -1);
Expand All @@ -142,7 +145,7 @@ export class Angular5Csv {
* Format Data
* @param {any} data
*/
formartData(data: any) {
formatData(data: any) {

if (this._options.decimalseparator === 'locale' && Angular5Csv.isFloat(data)) {
return data.toLocaleString();
Expand All @@ -160,6 +163,13 @@ export class Angular5Csv {
return data;
}

if (this._options.nullToEmptyString) {
if(data === null) {
return data = '';
}
return data;
}

if (typeof data === 'boolean') {
return data ? 'TRUE' : 'FALSE';
}
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ new Angular5Csv(data, 'My Report');
| **showTitle** | false | |
| **useBom** | true | If true, adds a BOM character at the start of the CSV |
| **noDownload** | false | If true, disables automatic download and returns only formatted CSV |
| **nullToEmptyString** | false | If true, all null values will be changed to empty strings |


## Options Example
Expand All @@ -78,7 +79,8 @@ new Angular5Csv(data, 'My Report');
title: 'Your title',
useBom: true,
noDownload: true,
headers: ["First Name", "Last Name", "ID"]
headers: ["First Name", "Last Name", "ID"],
nullToEmptyString: true,
};

Angular5Csv(data, filename, options);
Expand All @@ -90,4 +92,5 @@ new Angular5Csv(data, 'My Report');


* [sn123](https://github.com/sn123)
* [arf1980](https://github.com/arf1980)
* [arf1980](https://github.com/arf1980)
* [rob-moore](https://github.com/rob-moore)
Loading

0 comments on commit 721f37a

Please sign in to comment.