@ctrl/ngx-csv
Version:
Easily generate a CSV download in the browser with Angular
123 lines (117 loc) • 5.51 kB
JavaScript
import * as i0 from '@angular/core';
import { Directive, Input, HostBinding, NgModule } from '@angular/core';
import * as i1 from '@angular/platform-browser';
const isJsons = (array) => Array.isArray(array) &&
array.every(row => typeof row === 'object' && !(row instanceof Array));
const isArrays = (array) => Array.isArray(array) && array.every(row => Array.isArray(row));
function jsonsHeaders(array) {
return Array.from(new Set(array.map(item => Object.keys(item)).reduce((a, b) => [...a, ...b], [])));
}
function jsons2arrays(jsons, headers) {
headers = headers || jsonsHeaders(jsons);
// allow headers to have custom labels, defaulting to having the header data key be the label
let headerLabels = headers;
let headerKeys = headers;
if (isJsons(headers)) {
headerLabels = headers.map(header => header.label);
headerKeys = headers.map(header => header.key);
}
const data = jsons.map(object => headerKeys.map(header => (header in object ? object[header] : '')));
return [headerLabels, ...data];
}
const elementOrEmpty = (element) => element || element === 0 ? element : '';
function joiner(data, delimiter = ',') {
return data
.map((row, index) => row.map((element) => '"' + elementOrEmpty(element) + '"').join(delimiter))
.join(`\n`);
}
function arrays2csv(data, headers, delimiter) {
return joiner(headers ? [headers, ...data] : data, delimiter);
}
function jsons2csv(data, headers, delimiter) {
return joiner(jsons2arrays(data, headers), delimiter);
}
function string2csv(data, headers, delimiter) {
return headers ? `${headers.join(delimiter)}\n${data}` : data;
}
function toCSV(data, headers, delimiter) {
if (isJsons(data)) {
return jsons2csv(data, headers, delimiter);
}
if (isArrays(data)) {
return arrays2csv(data, headers, delimiter);
}
if (typeof data === 'string') {
return string2csv(data, headers, delimiter);
}
throw new TypeError(`Data should be a "String", "Array of arrays" OR "Array of objects" `);
}
function blob(data, uFEFF = true, headers, delimiter) {
const csv = toCSV(data, headers, delimiter);
return new Blob([uFEFF ? '\uFEFF' : '', csv], { type: 'text/csv' });
}
function buildURI(data, uFEFF = true, headers, delimiter) {
return URL.createObjectURL(blob(data, uFEFF, headers, delimiter));
}
class CsvDirective {
constructor(sanitizer) {
this.sanitizer = sanitizer;
/** the body of the csv */
this.data = [];
/** Set the seperator between values */
this.delimiter = ',';
/** adds a Byte order mark to setup the csv as UTF-8 */
this.uFEFF = true;
/** filename */
this.download = 'data.csv';
this.target = '_blank';
}
/** Set the filename of the csv. Default is `data.csv` */
set filename(a) {
this.download = a;
}
ngOnChanges() {
this.href = this.sanitizer.bypassSecurityTrustResourceUrl(buildURI(this.data, this.uFEFF, this.headers, this.delimiter));
}
}
CsvDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.2", ngImport: i0, type: CsvDirective, deps: [{ token: i1.DomSanitizer }], target: i0.ɵɵFactoryTarget.Directive });
CsvDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.0.2", type: CsvDirective, selector: "[csvLink]", inputs: { data: "data", headers: "headers", delimiter: "delimiter", filename: "filename", uFEFF: "uFEFF", target: "target" }, host: { properties: { "href": "this.href", "download": "this.download", "target": "this.target" } }, usesOnChanges: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.2", ngImport: i0, type: CsvDirective, decorators: [{
type: Directive,
args: [{ selector: '[csvLink]' }]
}], ctorParameters: function () { return [{ type: i1.DomSanitizer }]; }, propDecorators: { data: [{
type: Input
}], headers: [{
type: Input
}], delimiter: [{
type: Input
}], filename: [{
type: Input
}], uFEFF: [{
type: Input
}], href: [{
type: HostBinding
}], download: [{
type: HostBinding
}], target: [{
type: Input
}, {
type: HostBinding
}] } });
class CsvModule {
}
CsvModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.2", ngImport: i0, type: CsvModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
CsvModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.2", ngImport: i0, type: CsvModule, declarations: [CsvDirective], exports: [CsvDirective] });
CsvModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.0.2", ngImport: i0, type: CsvModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.2", ngImport: i0, type: CsvModule, decorators: [{
type: NgModule,
args: [{
declarations: [CsvDirective],
exports: [CsvDirective],
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { CsvDirective, CsvModule, arrays2csv, blob, buildURI, elementOrEmpty, isArrays, isJsons, joiner, jsons2arrays, jsons2csv, jsonsHeaders, string2csv, toCSV };
//# sourceMappingURL=ctrl-ngx-csv.mjs.map