diff --git a/package.json b/package.json index 0fb7bacf..20cd0783 100644 --- a/package.json +++ b/package.json @@ -211,6 +211,7 @@ "naive-ui": "^2.35.0", "nanoid": "^5.0.9", "nanoid-dictionary": "^4.3.0", + "nestify-anything": "^3.0.3", "netmask": "^2.0.2", "netstack.js": "^2.1.1", "nginx-config-formatter": "^1.4.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7fed794f..a892b5d0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -530,6 +530,9 @@ dependencies: nanoid-dictionary: specifier: ^4.3.0 version: 4.3.0 + nestify-anything: + specifier: ^3.0.3 + version: 3.0.3 netmask: specifier: ^2.0.2 version: 2.0.2 @@ -2793,7 +2796,7 @@ packages: '@babel/helper-split-export-declaration': 7.22.6 '@babel/parser': 7.22.10 '@babel/types': 7.22.10 - debug: 4.3.4 + debug: 4.4.0 globals: 11.12.0 transitivePeerDependencies: - supports-color @@ -12692,6 +12695,11 @@ packages: engines: {node: '>=18'} dev: false + /is-what@5.2.1: + resolution: {integrity: sha512-FLNNgur29o+0/G6RcG3B6KRDCT6SvMfb7MlfjdydTZWPgBLfiemceChDhY0DHu50O35BDNbNp4rJLQXMt4fG0g==} + engines: {node: '>=18'} + dev: false + /is-windows@1.0.2: resolution: {integrity: sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==} engines: {node: '>=0.10.0'} @@ -13729,6 +13737,13 @@ packages: engines: {node: '>= 0.10.0'} dev: false + /merge-anything@6.0.6: + resolution: {integrity: sha512-F3K1W45PvTjRZzbcYIhXntNr8cux00gUxR8IzNPPG+80gNlAHZGVBwFyN4x5yjw/7QkLPKDbRQBK4KrJKo69mw==} + engines: {node: '>=18'} + dependencies: + is-what: 5.2.1 + dev: false + /merge-stream@2.0.0: resolution: {integrity: sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==} dev: true @@ -14085,6 +14100,13 @@ packages: dev: true optional: true + /nestify-anything@3.0.3: + resolution: {integrity: sha512-prpbKIksMjdlnaSrkKn86b1p883y3phhPk3euQC5nqKONLbtWTVZ0772uouIBzr2QxhaJgyylIUhll/iNdeaJQ==} + engines: {node: '>=18'} + dependencies: + merge-anything: 6.0.6 + dev: false + /netmask@2.0.2: resolution: {integrity: sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg==} engines: {node: '>= 0.4.0'} 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 deleted file mode 100644 index 557d21d6..00000000 --- a/src/tools/excel-to-data/excel-to-data.service.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -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 deleted file mode 100644 index e18cab43..00000000 --- a/src/tools/excel-to-data/excel-to-data.service.ts +++ /dev/null @@ -1,50 +0,0 @@ -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 14f40d5c..9ffa4658 100644 --- a/src/tools/excel-to-data/excel-to-data.vue +++ b/src/tools/excel-to-data/excel-to-data.vue @@ -1,12 +1,15 @@ @@ -42,17 +60,30 @@ function convertFile() { @file-upload="handleFileUpload" /> + + + + + + + +
- + Convert
+ + + {{ error }} + + diff --git a/src/tools/excel-to-data/index.ts b/src/tools/excel-to-data/index.ts index e6261ccf..d2ec5761 100644 --- a/src/tools/excel-to-data/index.ts +++ b/src/tools/excel-to-data/index.ts @@ -4,8 +4,8 @@ import { defineTool } from '../tool'; export const tool = defineTool({ name: 'Excel to data', path: '/excel-to-data', - description: 'Convert XLSX file to JSON, YAML, CSV, SQL INSERT or Markdown', - keywords: ['excel', 'csv', 'xlsx', 'sql', 'json', 'yaml', 'insert', 'markdown', 'md', 'tsv'], + description: 'Convert XLSX file to JSON, YAML, CSV, SQL INSERT, XML or Markdown', + keywords: ['excel', 'csv', 'xlsx', 'sql', 'json', 'yaml', 'insert', 'markdown', 'md', 'tsv', 'xml'], component: () => import('./excel-to-data.vue'), icon: TableExport, createdAt: new Date('2024-12-25'), diff --git a/src/utils/nestify-anything.d.ts b/src/utils/nestify-anything.d.ts new file mode 100644 index 00000000..75c51c62 --- /dev/null +++ b/src/utils/nestify-anything.d.ts @@ -0,0 +1,4 @@ +declare module "nestify-anything" { + export function nestifyObject( + payload: { [key in string]: unknown }): { [key in string]: unknown }; +} \ No newline at end of file diff --git a/src/utils/objectarray.export.test.ts b/src/utils/objectarray.export.test.ts new file mode 100644 index 00000000..c350f83f --- /dev/null +++ b/src/utils/objectarray.export.test.ts @@ -0,0 +1,90 @@ +import { describe, expect, it } from 'vitest'; +import yaml from 'yaml'; +import type { ExportFormat } from './objectarray.export'; +import { objectArrayToData } from './objectarray.export'; + +describe('objectArrayToData', () => { + const sampleData = [ + { id: 1, name: 'Alice', age: 25 }, + { id: 2, name: 'Bob', age: 30 }, + ]; + const sampleNestedData = [ + { 'id': 1, 'info.name': 'Alice', 'info.age': 25 }, + { 'id': 2, 'info.name': 'Bob', 'info.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 XML format correctly', () => { + const result = objectArrayToData(sampleData, 'xml', { tableName }); + expect(result).toBe('1Alice252Bob30'); + }); + + 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 export nested JSON format correctly', () => { + const result = objectArrayToData(sampleNestedData, 'json', { tableName, nestify: true }); + expect(result).toBe(`[ + { + \"id\": 1, + \"info\": { + \"name\": \"Alice\", + \"age\": 25 + } + }, + { + \"id\": 2, + \"info\": { + \"name\": \"Bob\", + \"age\": 30 + } + } +]`); + }); + + it('should export nested YAML format correctly', () => { + const result = objectArrayToData(sampleNestedData, 'yaml', { tableName, nestify: true }); + expect(result).toBe(`- id: 1 + info: + name: Alice + age: 25 +- id: 2 + info: + name: Bob + age: 30 +`); + }); + + it('should return empty string for unknown format', () => { + const result = objectArrayToData(sampleData, 'unknown' as ExportFormat, { tableName }); + expect(result).toBe(''); + }); +}); diff --git a/src/utils/objectarray.export.ts b/src/utils/objectarray.export.ts new file mode 100644 index 00000000..7656c3ad --- /dev/null +++ b/src/utils/objectarray.export.ts @@ -0,0 +1,58 @@ +import yaml from 'yaml'; +import convert from 'xml-js'; +import { nestifyObject } from 'nestify-anything'; + +export type ExportFormat = 'json' | 'yaml' | 'sql' | 'csv' | 'tsv' | 'csv_semicolon' | 'markdown' | 'xml'; + +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: any[], format: ExportFormat, options: { + nestify?: boolean + tableName?: string +} = {}) { + const dataPrepared = options.nestify ? data.map(obj => nestifyObject(obj)) : data; + switch (format) { + case 'json': + return JSON.stringify(dataPrepared, null, 2); + case 'yaml': + return yaml.stringify(dataPrepared); + case 'sql': + return dataPrepared.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 ${(options.tableName ?? 'MyTable')} (${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(dataPrepared[0]).map(escapeCSV).join(sep), + ...dataPrepared.map(row => Object.values(row).map(escapeCSV).join(sep)), + ].join('\n'); + case 'markdown': + return `| ${Object.keys(dataPrepared[0]).map(escapeMarkdown).join(' | ')} |\n| ${Object.keys(dataPrepared[0]).map(() => '---').join(' | ')} |\n${ + dataPrepared.map(row => `| ${Object.values(row).map(escapeMarkdown).join(' | ')} |`).join('\n')}`; + case 'xml': + return convert.js2xml({ root: dataPrepared }, { compact: true }); + default: + return ''; + } +}