mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
refactor(Excel to Data): service + unit tests
This commit is contained in:
1 parent
3564ccfb7d
commit
ece277001f
3 files changed
+126
-86
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import yaml from 'yaml';
|
||||
import type { ExportFormat } from './excel-to-data.service';
|
||||
import { objectArrayToData } from './excel-to-data.service';
|
||||
|
||||
describe('objectArrayToData', () => {
|
||||
const sampleData = [
|
||||
{ id: 1, name: 'Alice', age: 25 },
|
||||
{ id: 2, name: 'Bob', age: 30 },
|
||||
];
|
||||
const tableName = 'users';
|
||||
|
||||
it('should export JSON format correctly', () => {
|
||||
const result = objectArrayToData(sampleData, 'json', tableName);
|
||||
expect(result).toBe(JSON.stringify(sampleData, null, 2));
|
||||
});
|
||||
|
||||
it('should export YAML format correctly', () => {
|
||||
const result = objectArrayToData(sampleData, 'yaml', tableName);
|
||||
expect(result).toBe(yaml.stringify(sampleData));
|
||||
});
|
||||
|
||||
it('should export SQL format correctly', () => {
|
||||
const result = objectArrayToData(sampleData, 'sql', tableName);
|
||||
expect(result).toBe(`INSERT INTO users (id, name, age) VALUES (1, 'Alice', 25);
|
||||
INSERT INTO users (id, name, age) VALUES (2, 'Bob', 30);`);
|
||||
});
|
||||
|
||||
it('should export CSV format correctly', () => {
|
||||
const result = objectArrayToData(sampleData, 'csv', tableName);
|
||||
expect(result).toContain('"id","name","age"');
|
||||
expect(result).toContain('"1","Alice","25"');
|
||||
expect(result).toContain('"2","Bob","30"');
|
||||
});
|
||||
|
||||
it('should export Markdown format correctly', () => {
|
||||
const result = objectArrayToData(sampleData, 'markdown', tableName);
|
||||
expect(result).toContain('| id | name | age |');
|
||||
expect(result).toContain('| --- | --- | --- |');
|
||||
expect(result).toContain('| 1 | Alice | 25 |');
|
||||
expect(result).toContain('| 2 | Bob | 30 |');
|
||||
});
|
||||
|
||||
it('should return empty string for unknown format', () => {
|
||||
const result = objectArrayToData(sampleData, 'unknown' as ExportFormat, tableName);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import yaml from 'yaml';
|
||||
|
||||
export type ExportFormat = 'json' | 'yaml' | 'sql' | 'csv' | 'tsv' | 'csv_semicolon' | 'markdown';
|
||||
|
||||
function escapeSQL(value: any) {
|
||||
return typeof value === 'string' ? `'${value.replace(/'/g, '\'\'')}'` : value;
|
||||
}
|
||||
|
||||
function escapeCSV(value: any) {
|
||||
return `"${String(value).replace(/"/g, '""')}"`;
|
||||
}
|
||||
|
||||
function escapeMarkdown(value: any) {
|
||||
return String(value).replace(/\|/g, '\\|').replace(/`/g, '\\`');
|
||||
}
|
||||
|
||||
export function objectArrayToData(data: object[], format: ExportFormat, tableName: string) {
|
||||
switch (format) {
|
||||
case 'json':
|
||||
return JSON.stringify(data, null, 2);
|
||||
case 'yaml':
|
||||
return yaml.stringify(data);
|
||||
case 'sql':
|
||||
return data.map((row) => {
|
||||
const keys = Object.keys(row).map(k => k?.replace(/\s/g, '')).join(', ');
|
||||
const values = Object.values(row).map(escapeSQL).join(', ');
|
||||
return `INSERT INTO ${tableName} (${keys}) VALUES (${values});`;
|
||||
}).join('\n');
|
||||
case 'csv':
|
||||
case 'tsv':
|
||||
case 'csv_semicolon':
|
||||
// eslint-disable-next-line no-case-declarations
|
||||
let sep = ',';
|
||||
if (format === 'tsv') {
|
||||
sep = '\t';
|
||||
}
|
||||
else if (format === 'csv_semicolon') {
|
||||
sep = ';';
|
||||
}
|
||||
return [
|
||||
Object.keys(data[0]).map(escapeCSV).join(sep),
|
||||
...data.map(row => Object.values(row).map(escapeCSV).join(sep)),
|
||||
].join('\n');
|
||||
case 'markdown':
|
||||
return `| ${Object.keys(data[0]).map(escapeMarkdown).join(' | ')} |\n| ${Object.keys(data[0]).map(() => '---').join(' | ')} |\n${
|
||||
data.map(row => `| ${Object.values(row).map(escapeMarkdown).join(' | ')} |`).join('\n')}`;
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -1,94 +1,36 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, ref } from 'vue';
|
||||
import { NButton, NCard, NSelect } from 'naive-ui';
|
||||
<script lang="ts" setup>
|
||||
import * as XLSX from 'xlsx';
|
||||
import yaml from 'yaml';
|
||||
import { objectArrayToData } from './excel-to-data.service';
|
||||
import type { ExportFormat } from './excel-to-data.service';
|
||||
|
||||
export default defineComponent({
|
||||
components: { NCard, NButton, NSelect },
|
||||
setup() {
|
||||
const data = ref<any[]>([]);
|
||||
const convertedData = ref<string>('');
|
||||
const selectedFormat = ref<string>('json');
|
||||
const formatLanguage = ref<string>('json');
|
||||
const tableName = ref<string>('TableName');
|
||||
const data = ref<any[]>([]);
|
||||
const convertedData = ref<string>('');
|
||||
const selectedFormat = ref<string>('json');
|
||||
const tableName = ref<string>('TableName');
|
||||
|
||||
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' },
|
||||
];
|
||||
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' },
|
||||
];
|
||||
|
||||
const escapeSQL = (value: any) =>
|
||||
typeof value === 'string' ? `'${value.replace(/'/g, '\'\'')}'` : value;
|
||||
async function handleFileUpload(file: File) {
|
||||
const workbook = XLSX.read(await file.arrayBuffer(), { type: 'binary' });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
data.value = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
|
||||
}
|
||||
|
||||
const escapeCSV = (value: any) =>
|
||||
`"${String(value).replace(/"/g, '""')}"`;
|
||||
function convertFile() {
|
||||
if (!data.value.length || !selectedFormat.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const escapeMarkdown = (value: any) =>
|
||||
String(value).replace(/\|/g, '\\|').replace(/`/g, '\\`');
|
||||
|
||||
const handleFileUpload = async (file: File) => {
|
||||
const workbook = XLSX.read(await file.arrayBuffer(), { type: 'binary' });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
data.value = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
|
||||
};
|
||||
|
||||
const convertFile = () => {
|
||||
if (!data.value.length || !selectedFormat.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (selectedFormat.value) {
|
||||
case 'json':
|
||||
formatLanguage.value = 'json';
|
||||
convertedData.value = JSON.stringify(data.value, null, 2);
|
||||
break;
|
||||
case 'yaml':
|
||||
formatLanguage.value = 'yaml';
|
||||
convertedData.value = yaml.stringify(data.value);
|
||||
break;
|
||||
case 'sql':
|
||||
formatLanguage.value = 'sql';
|
||||
convertedData.value = data.value.map((row) => {
|
||||
const keys = Object.keys(row).map(k => k?.replace(/\s/g, '')).join(', ');
|
||||
const values = Object.values(row).map(escapeSQL).join(', ');
|
||||
return `INSERT INTO ${tableName.value} (${keys}) VALUES (${values});`;
|
||||
}).join('\n');
|
||||
break;
|
||||
case 'csv':
|
||||
case 'tsv':
|
||||
case 'csv_semicolon':
|
||||
formatLanguage.value = 'text';
|
||||
// eslint-disable-next-line no-case-declarations
|
||||
let sep = ',';
|
||||
if (selectedFormat.value === 'tsv') {
|
||||
sep = '\t';
|
||||
}
|
||||
else if (selectedFormat.value === 'csv_semicolon') {
|
||||
sep = ';';
|
||||
}
|
||||
convertedData.value = [
|
||||
Object.keys(data.value[0]).map(escapeCSV).join(sep),
|
||||
...data.value.map(row => Object.values(row).map(escapeCSV).join(sep)),
|
||||
].join('\n');
|
||||
break;
|
||||
case 'markdown':
|
||||
formatLanguage.value = 'markdown';
|
||||
convertedData.value
|
||||
= `| ${Object.keys(data.value[0]).map(escapeMarkdown).join(' | ')} |\n| ${Object.keys(data.value[0]).map(() => '---').join(' | ')} |\n${
|
||||
data.value.map(row => `| ${Object.values(row).map(escapeMarkdown).join(' | ')} |`).join('\n')}`;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
return { handleFileUpload, convertFile, selectedFormat, formats, convertedData, data, tableName, formatLanguage };
|
||||
},
|
||||
});
|
||||
convertedData.value = objectArrayToData(data.value, selectedFormat.value as ExportFormat, tableName.value);
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -112,7 +54,7 @@ export default defineComponent({
|
||||
</NButton>
|
||||
</div>
|
||||
<c-card v-if="convertedData" title="Converted data">
|
||||
<textarea-copyable :value="convertedData" :language="formatLanguage" />
|
||||
<textarea-copyable :value="convertedData" :language="selectedFormat" />
|
||||
</c-card>
|
||||
</NCard>
|
||||
</template>
|
||||
Reference in new issue
Block a user