mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
feat(new tool): CSV to JSON, YAML, SQL INSERT, Markdown, CSV, XML
Fix part of #1481 and #394
This commit is contained in:
1 parent
b982fa5e56
commit
93532cec55
3 files changed
+145
-1
No files matched your search
@@ -0,0 +1,130 @@
|
||||
<script lang="ts" setup>
|
||||
import { convertCsvToArray } from '../csv-to-json/csv-to-json.service';
|
||||
import { objectArrayToData } from '@/utils/objectarray.export';
|
||||
import type { ExportFormat } from '@/utils/objectarray.export';
|
||||
|
||||
const inputType = ref<'file' | 'content'>('file');
|
||||
const csvContent = ref('');
|
||||
const fileInput = ref() as Ref<File | null>;
|
||||
const error = ref('');
|
||||
|
||||
const convertedData = ref<string>('');
|
||||
const selectedFormat = ref<string>('json');
|
||||
const tableName = ref<string>('TableName');
|
||||
const nestify = ref(false);
|
||||
const typedValues = ref(false);
|
||||
|
||||
const formats = [
|
||||
{ label: 'JSON', value: 'json' },
|
||||
{ label: 'YAML', value: 'yaml' },
|
||||
{ label: 'SQL INSERT', value: 'sql' },
|
||||
{ label: 'CSV (comma)', value: 'csv' },
|
||||
{ label: 'CSV (semicolon)', value: 'csv_semicolon' },
|
||||
{ label: 'CSV (tab)', value: 'tsv' },
|
||||
{ label: 'Markdown', value: 'markdown' },
|
||||
{ label: 'XML', value: 'xml' },
|
||||
];
|
||||
|
||||
async function handleFileUpload(file: File) {
|
||||
if (file) {
|
||||
fileInput.value = file;
|
||||
}
|
||||
}
|
||||
|
||||
function readFileAsString(file: File) {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const fr = new FileReader();
|
||||
fr.onload = () => {
|
||||
resolve(fr.result as string || '');
|
||||
};
|
||||
fr.onerror = reject;
|
||||
fr.readAsText(file);
|
||||
});
|
||||
}
|
||||
|
||||
async function convertFile() {
|
||||
let csvContentValue = csvContent.value;
|
||||
const file = fileInput.value;
|
||||
error.value = '';
|
||||
try {
|
||||
if (inputType.value === 'file' && file) {
|
||||
csvContentValue = await readFileAsString(file);
|
||||
}
|
||||
|
||||
const data = convertCsvToArray(csvContentValue, typedValues.value);
|
||||
convertedData.value = objectArrayToData(data, selectedFormat.value as ExportFormat, {
|
||||
tableName: tableName.value,
|
||||
nestify: nestify.value,
|
||||
});
|
||||
}
|
||||
catch (e: any) {
|
||||
error.value = e.toString();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NCard title="CSV Converter">
|
||||
<c-card>
|
||||
<n-radio-group v-model:value="inputType" name="radiogroup" mb-2 flex justify-center>
|
||||
<n-space>
|
||||
<n-radio
|
||||
value="file"
|
||||
label="File"
|
||||
/>
|
||||
<n-radio
|
||||
value="content"
|
||||
label="Content"
|
||||
/>
|
||||
</n-space>
|
||||
</n-radio-group>
|
||||
|
||||
<c-file-upload
|
||||
v-if="inputType === 'file'"
|
||||
accept=".csv,.tsv"
|
||||
title="Drag and drop a CSV file here, or click to select a file"
|
||||
@file-upload="handleFileUpload"
|
||||
/>
|
||||
|
||||
<c-input-text
|
||||
v-if="inputType === 'content'"
|
||||
v-model:value="csvContent"
|
||||
label="Paste your CSV Content:"
|
||||
placeholder="Your CSV..."
|
||||
multiline
|
||||
rows="8"
|
||||
data-test-id="input"
|
||||
/>
|
||||
</c-card>
|
||||
|
||||
<n-space justify="center">
|
||||
<n-form-item label="Typed Values" label-placement="left">
|
||||
<n-checkbox v-model:checked="typedValues" />
|
||||
</n-form-item>
|
||||
<n-form-item label="Nestify ('a.b.c' to nested objects)" label-placement="left">
|
||||
<n-checkbox v-model:checked="nestify" />
|
||||
</n-form-item>
|
||||
</n-space>
|
||||
|
||||
<NFormItem label="Select output format:" label-placement="left">
|
||||
<NSelect v-model:value="selectedFormat" :options="formats" placeholder="Select format" />
|
||||
</NFormItem>
|
||||
|
||||
<c-input-text v-if="selectedFormat === 'sql'" v-model:value="tableName" label="Table Name:" label-placement="left" />
|
||||
|
||||
<div mt-3 flex justify-center>
|
||||
<NButton :disabled="!((inputType === 'file' && fileInput) || csvContent)" @click="convertFile">
|
||||
Convert
|
||||
</NButton>
|
||||
</div>
|
||||
|
||||
<c-alert v-if="error">
|
||||
{{ error }}
|
||||
</c-alert>
|
||||
|
||||
<c-card v-if="convertedData" title="Converted data">
|
||||
<textarea-copyable :value="convertedData" :language="selectedFormat" />
|
||||
</c-card>
|
||||
</NCard>
|
||||
</template>
|
||||
@@ -0,0 +1,14 @@
|
||||
import { TableExport } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'Csv to Data',
|
||||
path: '/csv-to-data',
|
||||
description: 'Convert CSV file to JSON, YAML, CSV, SQL INSERT, XML or Markdown',
|
||||
keywords: ['excel', 'csv', 'xlsx', 'sql', 'json', 'yaml', 'insert', 'markdown', 'md', 'tsv', 'xml'],
|
||||
component: () => import('./csv-to-data.vue'),
|
||||
icon: TableExport,
|
||||
createdAt: new Date('2025-05-01'),
|
||||
redirectFrom: ['/csv-to-json'],
|
||||
category: 'Converters',
|
||||
});
|
||||
@@ -15,7 +15,7 @@ function deserializeValue(value: string, tryParseValues: boolean): unknown {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (value === '') {
|
||||
if (value === '' || typeof value === 'undefined') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user