ng2csv 7.0.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/fesm2022/ng2csv.mjs +36 -38
- package/fesm2022/ng2csv.mjs.map +1 -1
- package/package.json +5 -4
- package/{index.d.ts → types/ng2csv.d.ts} +8 -8
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017, 2018, 2021, 2023
|
|
3
|
+
Copyright (c) 2017, 2018, 2021, 2023-2026 Richard Russell
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/fesm2022/ng2csv.mjs
CHANGED
|
@@ -3,32 +3,31 @@ import { Injectable, NgModule } from '@angular/core';
|
|
|
3
3
|
import { saveAs } from 'file-saver';
|
|
4
4
|
|
|
5
5
|
class OrderedProjectionCsvRowMapper {
|
|
6
|
+
projections;
|
|
6
7
|
constructor(projections) {
|
|
7
8
|
this.projections = projections;
|
|
8
9
|
}
|
|
9
10
|
getColumnNames() {
|
|
10
|
-
return this.projections.map(x => x[0]);
|
|
11
|
+
return this.projections.map((x) => x[0]);
|
|
11
12
|
}
|
|
12
13
|
map(obj) {
|
|
13
|
-
return this.projections.map(x => x[1](obj));
|
|
14
|
+
return this.projections.map((x) => x[1](obj));
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
class AutoCsvRowMapper {
|
|
19
|
+
derivedCsvRowMapper;
|
|
18
20
|
constructor(data) {
|
|
19
21
|
if (!data || data.length === 0) {
|
|
20
|
-
throw new Error(
|
|
22
|
+
throw new Error("Parameter 'data' must be a non-empty array of objects.");
|
|
21
23
|
}
|
|
22
24
|
if (typeof data[0] !== 'object') {
|
|
23
|
-
throw new Error(
|
|
25
|
+
throw new Error("Parameter 'data' must contain objects.");
|
|
24
26
|
}
|
|
25
27
|
const projections = [];
|
|
26
28
|
const allKeys = this.getAllKeys(data);
|
|
27
29
|
for (const key of allKeys) {
|
|
28
|
-
projections.push([
|
|
29
|
-
key,
|
|
30
|
-
this.createProjection(key)
|
|
31
|
-
]);
|
|
30
|
+
projections.push([key, this.createProjection(key)]);
|
|
32
31
|
}
|
|
33
32
|
this.derivedCsvRowMapper = new OrderedProjectionCsvRowMapper(projections);
|
|
34
33
|
}
|
|
@@ -40,7 +39,7 @@ class AutoCsvRowMapper {
|
|
|
40
39
|
}
|
|
41
40
|
createProjection(property) {
|
|
42
41
|
return (x) => {
|
|
43
|
-
if (x.
|
|
42
|
+
if (x && typeof x === 'object' && Object.hasOwn(x, property)) {
|
|
44
43
|
const propertyValue = x[property];
|
|
45
44
|
if (propertyValue === undefined || propertyValue === null) {
|
|
46
45
|
return '';
|
|
@@ -64,23 +63,20 @@ class AutoCsvRowMapper {
|
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
class CsvConfiguration {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.outputValueForUndefined = '';
|
|
74
|
-
}
|
|
66
|
+
delimiter = ',';
|
|
67
|
+
quote = '"';
|
|
68
|
+
newLine = '\r\n';
|
|
69
|
+
includeHeaderLine = true;
|
|
70
|
+
outputValueForNull = '';
|
|
71
|
+
outputValueForUndefined = '';
|
|
75
72
|
}
|
|
76
73
|
|
|
77
74
|
class Ng2CsvService {
|
|
78
75
|
download(data, filename, csvRowMapper, config = new CsvConfiguration()) {
|
|
79
76
|
const csvData = this.convertToCsv(data, csvRowMapper, config);
|
|
80
|
-
const mimeType = 'text/csv; header='
|
|
81
|
-
+ (config.includeHeaderLine ? 'present' : 'absent');
|
|
77
|
+
const mimeType = 'text/csv; header=' + (config.includeHeaderLine ? 'present' : 'absent');
|
|
82
78
|
const csvBlob = new Blob([csvData], { type: mimeType });
|
|
83
|
-
saveAs(csvBlob, filename,
|
|
79
|
+
saveAs(csvBlob, filename, { autoBom: false });
|
|
84
80
|
}
|
|
85
81
|
convertToCsv(data, csvRowMapper, config = new CsvConfiguration()) {
|
|
86
82
|
if (csvRowMapper === undefined) {
|
|
@@ -89,16 +85,18 @@ class Ng2CsvService {
|
|
|
89
85
|
const rows = [];
|
|
90
86
|
if (config.includeHeaderLine) {
|
|
91
87
|
const headerFieldSeparator = `${config.quote}${config.delimiter}${config.quote}`;
|
|
92
|
-
rows.push(config.quote
|
|
93
|
-
|
|
94
|
-
.
|
|
95
|
-
.
|
|
96
|
-
|
|
88
|
+
rows.push(config.quote +
|
|
89
|
+
csvRowMapper
|
|
90
|
+
.getColumnNames()
|
|
91
|
+
.map((x) => this.escapeQuotes(x, config.quote))
|
|
92
|
+
.join(headerFieldSeparator) +
|
|
93
|
+
config.quote);
|
|
97
94
|
}
|
|
98
95
|
for (const row of data) {
|
|
99
|
-
rows.push(csvRowMapper
|
|
100
|
-
.map(
|
|
101
|
-
.map(x => this.
|
|
96
|
+
rows.push(csvRowMapper
|
|
97
|
+
.map(row)
|
|
98
|
+
.map((x) => this.mapNullOrUndefinedValues(x, config))
|
|
99
|
+
.map((x) => this.escapeRowValue(x, config))
|
|
102
100
|
.join(config.delimiter));
|
|
103
101
|
}
|
|
104
102
|
return rows.join(config.newLine);
|
|
@@ -120,29 +118,29 @@ class Ng2CsvService {
|
|
|
120
118
|
return value.replace(new RegExp(quoteChar.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), quoteChar + quoteChar);
|
|
121
119
|
}
|
|
122
120
|
escapeRowValue(value, config) {
|
|
123
|
-
if (value.indexOf(config.delimiter) > -1
|
|
124
|
-
|
|
121
|
+
if (value.indexOf(config.delimiter) > -1 ||
|
|
122
|
+
value.indexOf(config.quote) > -1) {
|
|
125
123
|
const escapedValue = this.escapeQuotes(value, config.quote);
|
|
126
124
|
return `${config.quote}${escapedValue}${config.quote}`;
|
|
127
125
|
}
|
|
128
126
|
return value;
|
|
129
127
|
}
|
|
130
|
-
static
|
|
131
|
-
static
|
|
128
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Ng2CsvService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
129
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Ng2CsvService, providedIn: 'root' });
|
|
132
130
|
}
|
|
133
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
131
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Ng2CsvService, decorators: [{
|
|
134
132
|
type: Injectable,
|
|
135
133
|
args: [{
|
|
136
|
-
providedIn: 'root'
|
|
134
|
+
providedIn: 'root',
|
|
137
135
|
}]
|
|
138
136
|
}] });
|
|
139
137
|
|
|
140
138
|
class Ng2CsvModule {
|
|
141
|
-
static
|
|
142
|
-
static
|
|
143
|
-
static
|
|
139
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Ng2CsvModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
140
|
+
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "21.0.6", ngImport: i0, type: Ng2CsvModule });
|
|
141
|
+
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Ng2CsvModule });
|
|
144
142
|
}
|
|
145
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
143
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: Ng2CsvModule, decorators: [{
|
|
146
144
|
type: NgModule,
|
|
147
145
|
args: [{}]
|
|
148
146
|
}] });
|
package/fesm2022/ng2csv.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ng2csv.mjs","sources":["../../../projects/ng2csv/src/lib/ordered-projection-csv-row-mapper.class.ts","../../../projects/ng2csv/src/lib/auto-csv-row-mapper.class.ts","../../../projects/ng2csv/src/lib/csv-configuration.class.ts","../../../projects/ng2csv/src/lib/ng2csv.service.ts","../../../projects/ng2csv/src/lib/ng2csv.module.ts","../../../projects/ng2csv/src/public-api.ts","../../../projects/ng2csv/src/ng2csv.ts"],"sourcesContent":["import { ICsvRowMapper } from './csv-row-mapper.interface';\n\nexport class OrderedProjectionCsvRowMapper<T>\n implements ICsvRowMapper<T> {\n\n private projections: Array<[string, (obj: T) => string]>;\n\n public constructor(\n projections: Array<[string, (obj: T) => string]>) {\n this.projections = projections;\n }\n\n public getColumnNames(): string[] {\n return this.projections.map(x => x[0]);\n }\n\n public map(obj: T): string[] {\n return this.projections.map(x => x[1](obj));\n }\n}\n","import { ICsvRowMapper } from './csv-row-mapper.interface';\nimport { OrderedProjectionCsvRowMapper } from './ordered-projection-csv-row-mapper.class';\n\nexport class AutoCsvRowMapper<T> implements ICsvRowMapper<T> {\n\n private derivedCsvRowMapper: OrderedProjectionCsvRowMapper<T>;\n\n public constructor(data: T[]) {\n if (!data || data.length === 0) {\n throw new Error(\n 'Parameter \\'data\\' must be a non-empty array of objects.');\n }\n\n if (typeof data[0] !== 'object') {\n throw new Error(\n 'Parameter \\'data\\' must contain objects.'\n );\n }\n\n const projections: Array<[string, (obj: T) => string]> = [];\n\n const allKeys: string[] = this.getAllKeys(data);\n for (const key of allKeys) {\n projections.push([\n key,\n this.createProjection(key)\n ]);\n }\n\n this.derivedCsvRowMapper = new OrderedProjectionCsvRowMapper<T>(\n projections\n );\n }\n\n public getColumnNames(): string[] {\n return this.derivedCsvRowMapper.getColumnNames();\n }\n\n public map(obj: T): string[] {\n return this.derivedCsvRowMapper.map(obj);\n }\n\n private createProjection(property: string): (obj: T) => string {\n return (x: T) => {\n if (x.hasOwnProperty(property)) {\n const propertyValue = (x as any)[property];\n if (propertyValue === undefined || propertyValue === null) {\n return '';\n }\n return propertyValue.toString();\n } else {\n return '';\n }\n };\n }\n\n private getAllKeys(objs: T[]): string[] {\n const allKeys: any = {};\n\n for (const obj of objs) {\n for (const objectKey of Object.keys(obj)) {\n allKeys[objectKey] = 1;\n }\n }\n\n return Object.keys(allKeys);\n }\n}\n","export class CsvConfiguration {\n public delimiter = ',';\n public quote = '\"';\n public newLine = '\\r\\n';\n public includeHeaderLine = true;\n public outputValueForNull = '';\n public outputValueForUndefined = '';\n}\n","import { Injectable } from '@angular/core';\nimport { saveAs } from 'file-saver';\nimport { AutoCsvRowMapper } from './auto-csv-row-mapper.class';\nimport { CsvConfiguration } from './csv-configuration.class';\nimport { ICsvRowMapper } from './csv-row-mapper.interface';\n\n@Injectable({\n providedIn: 'root'\n})\nexport class Ng2CsvService {\n public download<T>(\n data: T[],\n filename: string,\n csvRowMapper?: ICsvRowMapper<T>,\n config: CsvConfiguration = new CsvConfiguration()): void {\n const csvData: string = this.convertToCsv(data, csvRowMapper, config);\n\n const mimeType: string = 'text/csv; header='\n + (config.includeHeaderLine ? 'present' : 'absent');\n\n const csvBlob: Blob = new Blob([csvData], { type: mimeType });\n saveAs(csvBlob, filename, true);\n }\n\n public convertToCsv<T>(\n data: T[],\n csvRowMapper?: ICsvRowMapper<T>,\n config: CsvConfiguration = new CsvConfiguration()): string {\n if (csvRowMapper === undefined) {\n csvRowMapper = new AutoCsvRowMapper(data);\n }\n\n const rows: string[] = [];\n\n if (config.includeHeaderLine) {\n const headerFieldSeparator =\n `${config.quote}${config.delimiter}${config.quote}`;\n rows.push(config.quote\n + csvRowMapper.getColumnNames()\n .map(x => this.escapeQuotes(x, config.quote))\n .join(headerFieldSeparator)\n + config.quote);\n }\n\n for (const row of data) {\n rows.push(csvRowMapper.map(row)\n .map(x => this.mapNullOrUndefinedValues(x, config))\n .map(x => this.escapeRowValue(x, config))\n .join(config.delimiter));\n }\n\n return rows.join(config.newLine);\n }\n\n private mapNullOrUndefinedValues(\n value: string,\n config: CsvConfiguration): string {\n switch (value) {\n case null: {\n return config.outputValueForNull;\n }\n case undefined: {\n return config.outputValueForUndefined;\n }\n default: {\n return value;\n }\n }\n }\n\n private escapeQuotes(\n value: string,\n quoteChar: string): string {\n return value.replace(\n new RegExp(quoteChar.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&'), 'g'),\n quoteChar + quoteChar);\n }\n\n private escapeRowValue(\n value: string,\n config: CsvConfiguration): string {\n if (value.indexOf(config.delimiter) > -1\n || value.indexOf(config.quote) > -1) {\n const escapedValue = this.escapeQuotes(value, config.quote);\n return `${config.quote}${escapedValue}${config.quote}`;\n }\n return value;\n }\n}\n","import { NgModule } from '@angular/core';\n\n@NgModule({})\nexport class Ng2CsvModule { }\n","/*\n * Public API Surface of ng2csv\n */\n\nexport * from './lib/ng2csv.service';\nexport * from './lib/ng2csv.module';\nexport * from './lib/auto-csv-row-mapper.class';\nexport * from './lib/csv-configuration.class';\nexport * from './lib/csv-row-mapper.interface';\nexport * from './lib/ordered-projection-csv-row-mapper.class';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MAEa,6BAA6B,CAAA;AAKxC,IAAA,WAAA,CACI,WAAgD,EAAA;AAClD,QAAA,IAAI,CAAC,WAAW,GAAG,WAAW;;IAGzB,cAAc,GAAA;AACnB,QAAA,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;;AAGjC,IAAA,GAAG,CAAC,GAAM,EAAA;AACf,QAAA,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;;AAE9C;;MChBY,gBAAgB,CAAA;AAI3B,IAAA,WAAA,CAAmB,IAAS,EAAA;QAC1B,IAAI,CAAC,IAAI,IAAI,IAAI,CA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AAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE;AACvC,YAAA,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC;YAC3D,OAAO,CAAA,EAAG,MAAM,CAAC,KAAK,CAAA,EAAG,YAAY,CAAA,EAAG,MAAM,CAAC,KAAK,CAAA,CAAE;;AAExD,QAAA,OAAO,KAAK;;8GA7EH,aAAa,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA,CAAA;AAAb,IAAA,SAAA,IAAA,CAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,aAAa,cAFZ,MAAM,EAAA,CAAA,CAAA;;2FAEP,aAAa,EAAA,UAAA,EAAA,CAAA;kBAHzB,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE;AACb,iBAAA;;;MCLY,YAAY,CAAA;8GAAZ,YAAY,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,QAAA,EAAA,CAAA,CAAA;+GAAZ,YAAY,EAAA,CAAA,CAAA;+GAAZ,YAAY,EAAA,CAAA,CAAA;;2FAAZ,YAAY,EAAA,UAAA,EAAA,CAAA;kBADxB,QAAQ;mBAAC,EAAE;;;ACFZ;;AAEG;;ACFH;;AAEG;;;;"}
|
|
1
|
+
{"version":3,"file":"ng2csv.mjs","sources":["../../../projects/ng2csv/src/lib/ordered-projection-csv-row-mapper.class.ts","../../../projects/ng2csv/src/lib/auto-csv-row-mapper.class.ts","../../../projects/ng2csv/src/lib/csv-configuration.class.ts","../../../projects/ng2csv/src/lib/ng2csv.service.ts","../../../projects/ng2csv/src/lib/ng2csv.module.ts","../../../projects/ng2csv/src/public-api.ts","../../../projects/ng2csv/src/ng2csv.ts"],"sourcesContent":["import { ICsvRowMapper } from './csv-row-mapper.interface';\n\nexport class OrderedProjectionCsvRowMapper<T> implements ICsvRowMapper<T> {\n private readonly projections: Array<\n [string, (obj: T) => string | null | undefined]\n >;\n\n public constructor(\n projections: Array<[string, (obj: T) => string | null | undefined]>\n ) {\n this.projections = projections;\n }\n\n public getColumnNames(): string[] {\n return this.projections.map((x) => x[0]);\n }\n\n public map(obj: T): (string | null | undefined)[] {\n return this.projections.map((x) => x[1](obj));\n }\n}\n","import { ICsvRowMapper } from './csv-row-mapper.interface';\nimport { OrderedProjectionCsvRowMapper } from './ordered-projection-csv-row-mapper.class';\n\nexport class AutoCsvRowMapper<T extends object> implements ICsvRowMapper<T> {\n private derivedCsvRowMapper: OrderedProjectionCsvRowMapper<T>;\n\n public constructor(data: T[]) {\n if (!data || data.length === 0) {\n throw new Error(\"Parameter 'data' must be a non-empty array of objects.\");\n }\n\n if (typeof data[0] !== 'object') {\n throw new Error(\"Parameter 'data' must contain objects.\");\n }\n\n const projections: Array<[string, (obj: T) => string]> = [];\n\n const allKeys: string[] = this.getAllKeys(data);\n for (const key of allKeys) {\n projections.push([key, this.createProjection(key)]);\n }\n\n this.derivedCsvRowMapper = new OrderedProjectionCsvRowMapper<T>(\n projections\n );\n }\n\n public getColumnNames(): string[] {\n return this.derivedCsvRowMapper.getColumnNames();\n }\n\n public map(obj: T): (string | null | undefined)[] {\n return this.derivedCsvRowMapper.map(obj);\n }\n\n private createProjection(property: string): (obj: T) => string {\n return (x: T) => {\n if (x && typeof x === 'object' && Object.hasOwn(x, property)) {\n const propertyValue = x[property as keyof T];\n if (propertyValue === undefined || propertyValue === null) {\n return '';\n }\n return propertyValue.toString();\n } else {\n return '';\n }\n };\n }\n\n private getAllKeys(objs: T[]): string[] {\n const allKeys: any = {};\n\n for (const obj of objs) {\n for (const objectKey of Object.keys(obj)) {\n allKeys[objectKey] = 1;\n }\n }\n\n return Object.keys(allKeys);\n }\n}\n","export class CsvConfiguration {\n public delimiter = ',';\n public quote = '\"';\n public newLine = '\\r\\n';\n public includeHeaderLine = true;\n public outputValueForNull = '';\n public outputValueForUndefined = '';\n}\n","import { Injectable } from '@angular/core';\nimport { saveAs } from 'file-saver';\nimport { AutoCsvRowMapper } from './auto-csv-row-mapper.class';\nimport { CsvConfiguration } from './csv-configuration.class';\nimport { ICsvRowMapper } from './csv-row-mapper.interface';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class Ng2CsvService {\n public download<T extends object>(\n data: T[],\n filename: string,\n csvRowMapper?: ICsvRowMapper<T>,\n config: CsvConfiguration = new CsvConfiguration()\n ): void {\n const csvData: string = this.convertToCsv(data, csvRowMapper, config);\n\n const mimeType: string =\n 'text/csv; header=' + (config.includeHeaderLine ? 'present' : 'absent');\n\n const csvBlob: Blob = new Blob([csvData], { type: mimeType });\n saveAs(csvBlob, filename, { autoBom: false });\n }\n\n public convertToCsv<T extends object>(\n data: T[],\n csvRowMapper?: ICsvRowMapper<T>,\n config: CsvConfiguration = new CsvConfiguration()\n ): string {\n if (csvRowMapper === undefined) {\n csvRowMapper = new AutoCsvRowMapper(data);\n }\n\n const rows: string[] = [];\n\n if (config.includeHeaderLine) {\n const headerFieldSeparator = `${config.quote}${config.delimiter}${config.quote}`;\n rows.push(\n config.quote +\n csvRowMapper\n .getColumnNames()\n .map((x) => this.escapeQuotes(x, config.quote))\n .join(headerFieldSeparator) +\n config.quote\n );\n }\n\n for (const row of data) {\n rows.push(\n csvRowMapper\n .map(row)\n .map((x) => this.mapNullOrUndefinedValues(x, config))\n .map((x) => this.escapeRowValue(x, config))\n .join(config.delimiter)\n );\n }\n\n return rows.join(config.newLine);\n }\n\n private mapNullOrUndefinedValues(\n value: string | null | undefined,\n config: CsvConfiguration\n ): string {\n switch (value) {\n case null: {\n return config.outputValueForNull;\n }\n case undefined: {\n return config.outputValueForUndefined;\n }\n default: {\n return value;\n }\n }\n }\n\n private escapeQuotes(value: string, quoteChar: string): string {\n return value.replace(\n new RegExp(quoteChar.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&'), 'g'),\n quoteChar + quoteChar\n );\n }\n\n private escapeRowValue(value: string, config: CsvConfiguration): string {\n if (\n value.indexOf(config.delimiter) > -1 ||\n value.indexOf(config.quote) > -1\n ) {\n const escapedValue = this.escapeQuotes(value, config.quote);\n return `${config.quote}${escapedValue}${config.quote}`;\n }\n return value;\n }\n}\n","import { NgModule } from '@angular/core';\n\n@NgModule({})\nexport class Ng2CsvModule { }\n","/*\n * Public API Surface of ng2csv\n */\n\nexport * from './lib/ng2csv.service';\nexport * from './lib/ng2csv.module';\nexport * from './lib/auto-csv-row-mapper.class';\nexport * from './lib/csv-configuration.class';\nexport * from './lib/csv-row-mapper.interface';\nexport * from './lib/ordered-projection-csv-row-mapper.class';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MAEa,6BAA6B,CAAA;AACvB,IAAA,WAAW;AAI5B,IAAA,WAAA,CACE,WAAmE,EAAA;AAEnE,QAAA,IAAI,CAAC,WAAW,GAAG,WAAW;IAChC;IAEO,cAAc,GAAA;AACnB,QAAA,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1C;AAEO,IAAA,GAAG,CAAC,GAAM,EAAA;AACf,QAAA,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC/C;AACD;;MCjBY,gBAAgB,CAAA;AACnB,IAAA,mBAAmB;AAE3B,IAAA,WAAA,CAAmB,IAAS,EAAA;QAC1B,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;AAC9B,YAAA,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC;QAC3E;QAEA,IAAI,OAAO,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,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAChC;AACA,YAAA,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC;YAC3D,OAAO,CAAA,EAAG,MAAM,CAAC,KAAK,CAAA,EAAG,YAAY,CAAA,EAAG,MAAM,CAAC,KAAK,CAAA,CAAE;QACxD;AACA,QAAA,OAAO,KAAK;IACd;uGArFW,aAAa,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAAb,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,aAAa,cAFZ,MAAM,EAAA,CAAA;;2FAEP,aAAa,EAAA,UAAA,EAAA,CAAA;kBAHzB,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;MCLY,YAAY,CAAA;uGAAZ,YAAY,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,QAAA,EAAA,CAAA;wGAAZ,YAAY,EAAA,CAAA;wGAAZ,YAAY,EAAA,CAAA;;2FAAZ,YAAY,EAAA,UAAA,EAAA,CAAA;kBADxB,QAAQ;mBAAC,EAAE;;;ACFZ;;AAEG;;ACFH;;AAEG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ng2csv",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
|
+
"description": "An Angular library for exporting data to CSV.",
|
|
4
5
|
"dependencies": {
|
|
5
6
|
"tslib": "^2.0.0"
|
|
6
7
|
},
|
|
7
8
|
"peerDependencies": {
|
|
8
|
-
"@angular/core": "^
|
|
9
|
+
"@angular/core": "^21.0.0",
|
|
9
10
|
"file-saver": "^2.0.0"
|
|
10
11
|
},
|
|
11
12
|
"repository": {
|
|
@@ -24,13 +25,13 @@
|
|
|
24
25
|
},
|
|
25
26
|
"homepage": "https://github.com/rars/ng2csv#readme",
|
|
26
27
|
"module": "fesm2022/ng2csv.mjs",
|
|
27
|
-
"typings": "
|
|
28
|
+
"typings": "types/ng2csv.d.ts",
|
|
28
29
|
"exports": {
|
|
29
30
|
"./package.json": {
|
|
30
31
|
"default": "./package.json"
|
|
31
32
|
},
|
|
32
33
|
".": {
|
|
33
|
-
"types": "./
|
|
34
|
+
"types": "./types/ng2csv.d.ts",
|
|
34
35
|
"default": "./fesm2022/ng2csv.mjs"
|
|
35
36
|
}
|
|
36
37
|
},
|
|
@@ -11,12 +11,12 @@ declare class CsvConfiguration {
|
|
|
11
11
|
|
|
12
12
|
interface ICsvRowMapper<T> {
|
|
13
13
|
getColumnNames(): string[];
|
|
14
|
-
map(obj: T): string[];
|
|
14
|
+
map(obj: T): (string | null | undefined)[];
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
declare class Ng2CsvService {
|
|
18
|
-
download<T>(data: T[], filename: string, csvRowMapper?: ICsvRowMapper<T>, config?: CsvConfiguration): void;
|
|
19
|
-
convertToCsv<T>(data: T[], csvRowMapper?: ICsvRowMapper<T>, config?: CsvConfiguration): string;
|
|
18
|
+
download<T extends object>(data: T[], filename: string, csvRowMapper?: ICsvRowMapper<T>, config?: CsvConfiguration): void;
|
|
19
|
+
convertToCsv<T extends object>(data: T[], csvRowMapper?: ICsvRowMapper<T>, config?: CsvConfiguration): string;
|
|
20
20
|
private mapNullOrUndefinedValues;
|
|
21
21
|
private escapeQuotes;
|
|
22
22
|
private escapeRowValue;
|
|
@@ -30,20 +30,20 @@ declare class Ng2CsvModule {
|
|
|
30
30
|
static ɵinj: i0.ɵɵInjectorDeclaration<Ng2CsvModule>;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
declare class AutoCsvRowMapper<T> implements ICsvRowMapper<T> {
|
|
33
|
+
declare class AutoCsvRowMapper<T extends object> implements ICsvRowMapper<T> {
|
|
34
34
|
private derivedCsvRowMapper;
|
|
35
35
|
constructor(data: T[]);
|
|
36
36
|
getColumnNames(): string[];
|
|
37
|
-
map(obj: T): string[];
|
|
37
|
+
map(obj: T): (string | null | undefined)[];
|
|
38
38
|
private createProjection;
|
|
39
39
|
private getAllKeys;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
declare class OrderedProjectionCsvRowMapper<T> implements ICsvRowMapper<T> {
|
|
43
|
-
private projections;
|
|
44
|
-
constructor(projections: Array<[string, (obj: T) => string]>);
|
|
43
|
+
private readonly projections;
|
|
44
|
+
constructor(projections: Array<[string, (obj: T) => string | null | undefined]>);
|
|
45
45
|
getColumnNames(): string[];
|
|
46
|
-
map(obj: T): string[];
|
|
46
|
+
map(obj: T): (string | null | undefined)[];
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
export { AutoCsvRowMapper, CsvConfiguration, Ng2CsvModule, Ng2CsvService, OrderedProjectionCsvRowMapper };
|