From ece277001fab249acd0a657aa54c1ba87a57389f Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sun, 4 May 2025 23:07:40 +0200 Subject: [PATCH] refactor(Excel to Data): service + unit tests --- .../excel-to-data.service.test.ts | 48 ++++++++ .../excel-to-data/excel-to-data.service.ts | 50 ++++++++ src/tools/excel-to-data/excel-to-data.vue | 114 +++++------------- 3 files changed, 126 insertions(+), 86 deletions(-) create mode 100644 src/tools/excel-to-data/excel-to-data.service.test.ts create mode 100644 src/tools/excel-to-data/excel-to-data.service.ts diff --git a/src/tools/excel-to-data/excel-to-data.service.test.ts b/src/tools/excel-to-data/excel-to-data.service.test.ts new file mode 100644 index 00000000..557d21d6 --- /dev/null +++ b/src/tools/excel-to-data/excel-to-data.service.test.ts @@ -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(''); + }); +}); diff --git a/src/tools/excel-to-data/excel-to-data.service.ts b/src/tools/excel-to-data/excel-to-data.service.ts new file mode 100644 index 00000000..e18cab43 --- /dev/null +++ b/src/tools/excel-to-data/excel-to-data.service.ts @@ -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 ''; + } +} diff --git a/src/tools/excel-to-data/excel-to-data.vue b/src/tools/excel-to-data/excel-to-data.vue index 274bb25f..14f40d5c 100644 --- a/src/tools/excel-to-data/excel-to-data.vue +++ b/src/tools/excel-to-data/excel-to-data.vue @@ -1,94 +1,36 @@ -