@adminforth/import-export 1.4.7 → 1.4.8
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/dist/index.js +19 -4
- package/index.ts +19 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters } from "adminforth";
|
|
10
|
+
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes } from "adminforth";
|
|
11
11
|
export default class ImportExport extends AdminForthPlugin {
|
|
12
12
|
constructor(options) {
|
|
13
13
|
super(options, import.meta.url);
|
|
@@ -66,9 +66,24 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
66
66
|
});
|
|
67
67
|
// csv export
|
|
68
68
|
const columns = this.resourceConfig.columns.filter((col) => !col.virtual);
|
|
69
|
-
const escapeCSV = (value) => {
|
|
70
|
-
if (value === null || value === undefined)
|
|
69
|
+
const escapeCSV = (value, column) => {
|
|
70
|
+
if (value === null || value === undefined) {
|
|
71
71
|
return '""';
|
|
72
|
+
}
|
|
73
|
+
if ((column === null || column === void 0 ? void 0 : column.type) === AdminForthDataTypes.FLOAT
|
|
74
|
+
|| (column === null || column === void 0 ? void 0 : column.type) === AdminForthDataTypes.INTEGER) {
|
|
75
|
+
// no quotes for numbers
|
|
76
|
+
return String(value);
|
|
77
|
+
}
|
|
78
|
+
if ((column === null || column === void 0 ? void 0 : column.type) === AdminForthDataTypes.BOOLEAN) {
|
|
79
|
+
// no quotes for boolean values
|
|
80
|
+
if (value === true) {
|
|
81
|
+
return 'true';
|
|
82
|
+
}
|
|
83
|
+
else if (value === false) {
|
|
84
|
+
return 'false';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
72
87
|
const str = String(value);
|
|
73
88
|
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
|
|
74
89
|
return `"${str.replace(/"/g, '""')}"`;
|
|
@@ -76,7 +91,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
76
91
|
return `"${str}"`;
|
|
77
92
|
};
|
|
78
93
|
let csv = data.data.map((row) => {
|
|
79
|
-
return columns.map((col) => escapeCSV(row[col.name])).join(',');
|
|
94
|
+
return columns.map((col) => escapeCSV(row[col.name], col)).join(',');
|
|
80
95
|
}).join('\n');
|
|
81
96
|
// add headers
|
|
82
97
|
const headers = columns.map((col) => escapeCSV(col.name)).join(',');
|
package/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters } from "adminforth";
|
|
1
|
+
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes } from "adminforth";
|
|
2
2
|
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource } from "adminforth";
|
|
3
3
|
import type { PluginOptions } from './types.js';
|
|
4
4
|
|
|
@@ -69,8 +69,23 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
69
69
|
// csv export
|
|
70
70
|
const columns = this.resourceConfig.columns.filter((col) => !col.virtual);
|
|
71
71
|
|
|
72
|
-
const escapeCSV = (value: any) => {
|
|
73
|
-
if (value === null || value === undefined)
|
|
72
|
+
const escapeCSV = (value: any, column?: AdminForthResourceColumn) => {
|
|
73
|
+
if (value === null || value === undefined) {
|
|
74
|
+
return '""';
|
|
75
|
+
}
|
|
76
|
+
if (column?.type === AdminForthDataTypes.FLOAT
|
|
77
|
+
|| column?.type === AdminForthDataTypes.INTEGER) {
|
|
78
|
+
// no quotes for numbers
|
|
79
|
+
return String(value);
|
|
80
|
+
}
|
|
81
|
+
if (column?.type === AdminForthDataTypes.BOOLEAN) {
|
|
82
|
+
// no quotes for boolean values
|
|
83
|
+
if (value === true) {
|
|
84
|
+
return 'true';
|
|
85
|
+
} else if (value === false) {
|
|
86
|
+
return 'false';
|
|
87
|
+
}
|
|
88
|
+
}
|
|
74
89
|
const str = String(value);
|
|
75
90
|
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
|
|
76
91
|
return `"${str.replace(/"/g, '""')}"`;
|
|
@@ -79,7 +94,7 @@ export default class ImportExport extends AdminForthPlugin {
|
|
|
79
94
|
};
|
|
80
95
|
|
|
81
96
|
let csv = data.data.map((row) => {
|
|
82
|
-
return columns.map((col) => escapeCSV(row[col.name])).join(',');
|
|
97
|
+
return columns.map((col) => escapeCSV(row[col.name], col)).join(',');
|
|
83
98
|
}).join('\n');
|
|
84
99
|
|
|
85
100
|
// add headers
|