Files
it-tools/src/utils/objectarray.export.test.ts
T
Sebastian DanielssonandClaude Opus 5.5 3872432a07 fix(markdown): escape backslashes in generated table cells
Pipes were escaped but backslashes were not, so a cell ending in `\` turned
the added `\|` into `\\|` and the pipe split the cell. The object-array
Markdown export also left newlines in cells, which broke the row; they now
become `<br>` like in the Markdown table generator.

Fixes CodeQL js/incomplete-sanitization alerts in the Markdown table
generator and the object-array export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 21:11:50 +02:00

98 lines
3.0 KiB
TypeScript

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 escape Markdown table cells', () => {
const result = objectArrayToData([{ path: 'C:\\dir\\', note: 'a|`b`\nc' }], 'markdown');
expect(result).toContain('| C:\\\\dir\\\\ | a\\|\\`b\\`<br>c |');
});
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('');
});
});