mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-21 21:44:40 +03:00
feat(Excel to Data): handle nestify, XML and Sheet Selection
This commit is contained in:
1 parent
41d24a52e7
commit
b982fa5e56
9 files changed
+218
-110
No files matched your search
@@ -211,6 +211,7 @@
|
|||||||
"naive-ui": "^2.35.0",
|
"naive-ui": "^2.35.0",
|
||||||
"nanoid": "^5.0.9",
|
"nanoid": "^5.0.9",
|
||||||
"nanoid-dictionary": "^4.3.0",
|
"nanoid-dictionary": "^4.3.0",
|
||||||
|
"nestify-anything": "^3.0.3",
|
||||||
"netmask": "^2.0.2",
|
"netmask": "^2.0.2",
|
||||||
"netstack.js": "^2.1.1",
|
"netstack.js": "^2.1.1",
|
||||||
"nginx-config-formatter": "^1.4.5",
|
"nginx-config-formatter": "^1.4.5",
|
||||||
|
|||||||
Generated
+23
-1
@@ -530,6 +530,9 @@ dependencies:
|
|||||||
nanoid-dictionary:
|
nanoid-dictionary:
|
||||||
specifier: ^4.3.0
|
specifier: ^4.3.0
|
||||||
version: 4.3.0
|
version: 4.3.0
|
||||||
|
nestify-anything:
|
||||||
|
specifier: ^3.0.3
|
||||||
|
version: 3.0.3
|
||||||
netmask:
|
netmask:
|
||||||
specifier: ^2.0.2
|
specifier: ^2.0.2
|
||||||
version: 2.0.2
|
version: 2.0.2
|
||||||
@@ -2793,7 +2796,7 @@ packages:
|
|||||||
'@babel/helper-split-export-declaration': 7.22.6
|
'@babel/helper-split-export-declaration': 7.22.6
|
||||||
'@babel/parser': 7.22.10
|
'@babel/parser': 7.22.10
|
||||||
'@babel/types': 7.22.10
|
'@babel/types': 7.22.10
|
||||||
debug: 4.3.4
|
debug: 4.4.0
|
||||||
globals: 11.12.0
|
globals: 11.12.0
|
||||||
transitivePeerDependencies:
|
transitivePeerDependencies:
|
||||||
- supports-color
|
- supports-color
|
||||||
@@ -12692,6 +12695,11 @@ packages:
|
|||||||
engines: {node: '>=18'}
|
engines: {node: '>=18'}
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
/is-what@5.2.1:
|
||||||
|
resolution: {integrity: sha512-FLNNgur29o+0/G6RcG3B6KRDCT6SvMfb7MlfjdydTZWPgBLfiemceChDhY0DHu50O35BDNbNp4rJLQXMt4fG0g==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/is-windows@1.0.2:
|
/is-windows@1.0.2:
|
||||||
resolution: {integrity: sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==}
|
resolution: {integrity: sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==}
|
||||||
engines: {node: '>=0.10.0'}
|
engines: {node: '>=0.10.0'}
|
||||||
@@ -13729,6 +13737,13 @@ packages:
|
|||||||
engines: {node: '>= 0.10.0'}
|
engines: {node: '>= 0.10.0'}
|
||||||
dev: false
|
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:
|
/merge-stream@2.0.0:
|
||||||
resolution: {integrity: sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==}
|
resolution: {integrity: sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==}
|
||||||
dev: true
|
dev: true
|
||||||
@@ -14085,6 +14100,13 @@ packages:
|
|||||||
dev: true
|
dev: true
|
||||||
optional: 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:
|
/netmask@2.0.2:
|
||||||
resolution: {integrity: sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg==}
|
resolution: {integrity: sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg==}
|
||||||
engines: {node: '>= 0.4.0'}
|
engines: {node: '>= 0.4.0'}
|
||||||
|
|||||||
@@ -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('');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -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 '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import * as XLSX from 'xlsx';
|
import * as XLSX from 'xlsx';
|
||||||
import { objectArrayToData } from './excel-to-data.service';
|
import { objectArrayToData } from '@/utils/objectarray.export';
|
||||||
import type { ExportFormat } from './excel-to-data.service';
|
import type { ExportFormat } from '@/utils/objectarray.export';
|
||||||
|
|
||||||
const data = ref<any[]>([]);
|
const workbook = ref<XLSX.WorkBook | null>(null);
|
||||||
const convertedData = ref<string>('');
|
const convertedData = ref<string>('');
|
||||||
const selectedFormat = ref<string>('json');
|
const selectedFormat = ref<string>('json');
|
||||||
const tableName = ref<string>('TableName');
|
const tableName = ref<string>('TableName');
|
||||||
|
const nestify = ref(false);
|
||||||
|
const sheetName = ref('');
|
||||||
|
const error = ref('');
|
||||||
|
|
||||||
const formats = [
|
const formats = [
|
||||||
{ label: 'JSON', value: 'json' },
|
{ label: 'JSON', value: 'json' },
|
||||||
@@ -16,20 +19,35 @@ const formats = [
|
|||||||
{ label: 'CSV (semicolon)', value: 'csv_semicolon' },
|
{ label: 'CSV (semicolon)', value: 'csv_semicolon' },
|
||||||
{ label: 'CSV (tab)', value: 'tsv' },
|
{ label: 'CSV (tab)', value: 'tsv' },
|
||||||
{ label: 'Markdown', value: 'markdown' },
|
{ label: 'Markdown', value: 'markdown' },
|
||||||
|
{ label: 'XML', value: 'xml' },
|
||||||
];
|
];
|
||||||
|
|
||||||
async function handleFileUpload(file: File) {
|
async function handleFileUpload(file: File) {
|
||||||
const workbook = XLSX.read(await file.arrayBuffer(), { type: 'binary' });
|
error.value = '';
|
||||||
const sheetName = workbook.SheetNames[0];
|
workbook.value = null;
|
||||||
data.value = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
|
sheetName.value = '';
|
||||||
|
try {
|
||||||
|
workbook.value = XLSX.read(await file.arrayBuffer(), { type: 'binary' });
|
||||||
|
sheetName.value = workbook.value.SheetNames[0];
|
||||||
|
}
|
||||||
|
catch (e: any) {
|
||||||
|
error.value = e.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertFile() {
|
function convertFile() {
|
||||||
if (!data.value.length || !selectedFormat.value) {
|
if (!workbook.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = XLSX.utils.sheet_to_json(workbook.value.Sheets[sheetName.value]);
|
||||||
|
if (!data.length || !selectedFormat.value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
convertedData.value = objectArrayToData(data.value, selectedFormat.value as ExportFormat, tableName.value);
|
convertedData.value = objectArrayToData(data, selectedFormat.value as ExportFormat, {
|
||||||
|
tableName: tableName.value,
|
||||||
|
nestify: nestify.value,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -42,17 +60,30 @@ function convertFile() {
|
|||||||
@file-upload="handleFileUpload"
|
@file-upload="handleFileUpload"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<NFormItem v-if="workbook" label="Select Sheet to use:" label-placement="left">
|
||||||
|
<NSelect v-model:value="sheetName" :options="workbook.SheetNames?.map((s) => ({ label: s, value: s }))" placeholder="Select sheet" />
|
||||||
|
</NFormItem>
|
||||||
|
|
||||||
<NFormItem label="Select output format:" label-placement="left">
|
<NFormItem label="Select output format:" label-placement="left">
|
||||||
<NSelect v-model:value="selectedFormat" :options="formats" placeholder="Select format" />
|
<NSelect v-model:value="selectedFormat" :options="formats" placeholder="Select format" />
|
||||||
</NFormItem>
|
</NFormItem>
|
||||||
|
|
||||||
|
<n-form-item label="Nestify ('a.b.c' to nested objects)" label-placement="left">
|
||||||
|
<n-checkbox v-model:checked="nestify" />
|
||||||
|
</n-form-item>
|
||||||
|
|
||||||
<c-input-text v-if="selectedFormat === 'sql'" v-model:value="tableName" label="Table Name:" label-placement="left" />
|
<c-input-text v-if="selectedFormat === 'sql'" v-model:value="tableName" label="Table Name:" label-placement="left" />
|
||||||
|
|
||||||
<div mt-3 flex justify-center>
|
<div mt-3 flex justify-center>
|
||||||
<NButton :disabled="!data.length" @click="convertFile">
|
<NButton :disabled="!workbook" @click="convertFile">
|
||||||
Convert
|
Convert
|
||||||
</NButton>
|
</NButton>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<c-alert v-if="error">
|
||||||
|
{{ error }}
|
||||||
|
</c-alert>
|
||||||
|
|
||||||
<c-card v-if="convertedData" title="Converted data">
|
<c-card v-if="convertedData" title="Converted data">
|
||||||
<textarea-copyable :value="convertedData" :language="selectedFormat" />
|
<textarea-copyable :value="convertedData" :language="selectedFormat" />
|
||||||
</c-card>
|
</c-card>
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import { defineTool } from '../tool';
|
|||||||
export const tool = defineTool({
|
export const tool = defineTool({
|
||||||
name: 'Excel to data',
|
name: 'Excel to data',
|
||||||
path: '/excel-to-data',
|
path: '/excel-to-data',
|
||||||
description: 'Convert XLSX file to JSON, YAML, CSV, SQL INSERT or Markdown',
|
description: 'Convert XLSX file to JSON, YAML, CSV, SQL INSERT, XML or Markdown',
|
||||||
keywords: ['excel', 'csv', 'xlsx', 'sql', 'json', 'yaml', 'insert', 'markdown', 'md', 'tsv'],
|
keywords: ['excel', 'csv', 'xlsx', 'sql', 'json', 'yaml', 'insert', 'markdown', 'md', 'tsv', 'xml'],
|
||||||
component: () => import('./excel-to-data.vue'),
|
component: () => import('./excel-to-data.vue'),
|
||||||
icon: TableExport,
|
icon: TableExport,
|
||||||
createdAt: new Date('2024-12-25'),
|
createdAt: new Date('2024-12-25'),
|
||||||
|
|||||||
Vendored
+4
@@ -0,0 +1,4 @@
|
|||||||
|
declare module "nestify-anything" {
|
||||||
|
export function nestifyObject(
|
||||||
|
payload: { [key in string]: unknown }): { [key in string]: unknown };
|
||||||
|
}
|
||||||
@@ -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('<root><id>1</id><name>Alice</name><age>25</age></root><root><id>2</id><name>Bob</name><age>30</age></root>');
|
||||||
|
});
|
||||||
|
|
||||||
|
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('');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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 '';
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user