Skip to content

Commit 310d8b2

Browse files
Refactor exportToJson to use safer types (#83)
Replaced `any` with `CellValue` in `exportToJson` to improve type safety. Exported `exportToJson` to enable unit testing. Added unit tests for `exportToJson` covering basic types, binary data, and edge cases. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent ae83722 commit 310d8b2

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

src/tableExporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ function exportToCsv(columns: string[], rows: CellValue[][], includeHeader: bool
371371
* Convert data to JSON format.
372372
* Each row becomes an object with column names as keys.
373373
*/
374-
function exportToJson(columns: string[], rows: CellValue[][]): string {
374+
export function exportToJson(columns: string[], rows: CellValue[][]): string {
375375
const objects = rows.map(row => {
376-
const obj: Record<string, any> = {};
376+
const obj: Record<string, CellValue> = {};
377377
columns.forEach((col, idx) => {
378378
const value = row[idx];
379379
// Convert Uint8Array to base64 for JSON

tests/unit/tableExporter.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
import './vscode_mock_setup'; // Must be first
3+
import { describe, it } from 'node:test';
4+
import assert from 'node:assert';
5+
import { exportToJson } from '../../src/tableExporter';
6+
import { CellValue } from '../../src/core/types';
7+
8+
describe('exportToJson', () => {
9+
it('should export basic types correctly', () => {
10+
const columns = ['id', 'name', 'value', 'nullable'];
11+
const rows: CellValue[][] = [
12+
[1, 'test', 12.34, null],
13+
[2, 'another', 0, 'not null']
14+
];
15+
16+
const json = exportToJson(columns, rows);
17+
const parsed = JSON.parse(json);
18+
19+
assert.deepStrictEqual(parsed, [
20+
{ id: 1, name: 'test', value: 12.34, nullable: null },
21+
{ id: 2, name: 'another', value: 0, nullable: 'not null' }
22+
]);
23+
});
24+
25+
it('should convert Uint8Array to base64', () => {
26+
const columns = ['id', 'data'];
27+
const buffer = new Uint8Array([1, 2, 3]);
28+
const rows: CellValue[][] = [
29+
[1, buffer]
30+
];
31+
32+
const json = exportToJson(columns, rows);
33+
const parsed = JSON.parse(json);
34+
35+
assert.deepStrictEqual(parsed, [
36+
{ id: 1, data: 'AQID' } // AQID is base64 for [1, 2, 3]
37+
]);
38+
});
39+
40+
it('should handle empty rows', () => {
41+
const columns = ['id', 'name'];
42+
const rows: CellValue[][] = [];
43+
44+
const json = exportToJson(columns, rows);
45+
const parsed = JSON.parse(json);
46+
47+
assert.deepStrictEqual(parsed, []);
48+
});
49+
50+
it('should handle empty columns (though unlikely in practice)', () => {
51+
const columns: string[] = [];
52+
const rows: CellValue[][] = [[], []];
53+
54+
const json = exportToJson(columns, rows);
55+
const parsed = JSON.parse(json);
56+
57+
assert.deepStrictEqual(parsed, [{}, {}]);
58+
});
59+
});

0 commit comments

Comments
 (0)