Skip to content

Commit

Permalink
Enable support for exporting 1D and 2D dataset slices.
Browse files Browse the repository at this point in the history
  • Loading branch information
mandrew9 authored and axelboc committed Sep 7, 2023
1 parent 67e6538 commit 9851f90
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { vscode } from './vscode-api.js';
// 2 GB = 2 * 1024 * 1024 * 1024 B
export const MAX_SIZE_IN_BYTES = 2147483648;

export const getExportURL: GetExportURL = (format, dataset, __, value) => {
export const getExportURL: GetExportURL = (
format,
dataset,
selection,
value
) => {
if (format === 'json') {
return async () => {
vscode.postMessage({
Expand All @@ -20,5 +25,62 @@ export const getExportURL: GetExportURL = (format, dataset, __, value) => {
};
}

if (format === 'csv') {
// Async function that will be called when the user clicks on a `CSV` export menu entry
return async () => {
// Generate CSV string from `value` array
let csv = '';
let dims = [];

// Record all dataset dimensions with cardinality greater than one,
// accounting for subset selection if applicable
if (selection) {
let subsets = selection.split(',');
for (let i = 0; i < subsets.length; i++) {
// Because of the user interface, all slices will either be
// the full array length or a single value
if (subsets[i] === ':' && dataset.shape[i] > 1) {
dims.push(dataset.shape[i]);
}
}
} else {
dataset.shape.forEach((val) => {
if (val > 1) dims.push(val);
});
}

// Only provide special handling for 2D dataset outputs
if (dims.length == 2) {
let k = 0;
for (let i = 0; i < dims[0]; i++) {
for (let j = 0; j < dims[1] - 1; j++) {
csv += `${value[k].toString()}, `;
k++;
}

csv += `${value[k].toString()}\n`;
k++;
}
} else {
value.forEach((val: any) => {
csv += `${val.toString()}\n`;
});
}

const finalCsv = csv.slice(0, -1);

vscode.postMessage({
type: MessageType.Export,
data: {
format,
name: dataset.name,
payload: finalCsv,
},
});

return new Blob(); // doesn't matter as long as it's not falsy
};
}

return undefined;
};

0 comments on commit 9851f90

Please sign in to comment.