ToCsvService to escape the fields during exportCsv.

Auth Interceptor to check for logged out user.
This commit is contained in:
tanshu
2018-06-11 22:14:04 +05:30
parent a811a121cc
commit 4530052a22
6 changed files with 84 additions and 34 deletions

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { ToCsvService } from './to-csv.service';
describe('ToCsvService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ToCsvService]
});
});
it('should be created', inject([ToCsvService], (service: ToCsvService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,18 @@
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ToCsvService {
constructor() {
}
toCsv(headers: any, data: any[]): string {
const header = Object.keys(headers);
const replacer = (key, value) => value === null ? '' : value;
const csv = data.map(row => header.map(fieldName => JSON.stringify(row[headers[fieldName]], replacer)).join(','));
csv.unshift(header.join(','));
return csv.join('\r\n');
}
}