Skip to content

Commit 29bf3c2

Browse files

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters } from "adminforth";
1+
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes } from "adminforth";
22
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource } from "adminforth";
33
import type { PluginOptions } from './types.js';
44

@@ -69,8 +69,23 @@ export default class ImportExport extends AdminForthPlugin {
6969
// csv export
7070
const columns = this.resourceConfig.columns.filter((col) => !col.virtual);
7171

72-
const escapeCSV = (value: any) => {
73-
if (value === null || value === undefined) return '""';
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+
}
7489
const str = String(value);
7590
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
7691
return `"${str.replace(/"/g, '""')}"`;
@@ -79,7 +94,7 @@ export default class ImportExport extends AdminForthPlugin {
7994
};
8095

8196
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(',');
8398
}).join('\n');
8499

85100
// add headers

0 commit comments

Comments
 (0)