Skip to content

Commit

Permalink
filter ext in ts (same as python)
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn committed Dec 25, 2024
1 parent 6bdcaa6 commit d5534fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,18 @@ async function downloadCellResults(
cellResults.map(async (res) => {
const formats = res.formats(); // available formats in the result
const formatResults = await Promise.all(
formats.map(async (ext) => {
if (ext === "chart") {
// save chart data as json file
const filename = `${crypto.randomUUID()}.json`;
formats
.filter((ext) => ["png", "svg", "jpeg", "pdf"].includes(ext))
.map(async (ext) => {
const filename = `${crypto.randomUUID()}.${ext}`;
const base64 = res[ext as keyof Result];
const buffer = Buffer.from(base64, "base64");
const fileurl = await saveDocument(
path.join(OUTPUT_DIR, filename),
JSON.stringify(res[ext as keyof Result]),
buffer,
);
return { url: fileurl, filename };
}

const filename = `${crypto.randomUUID()}.${ext}`;
const base64 = res[ext as keyof Result];
const buffer = Buffer.from(base64, "base64");
const fileurl = await saveDocument(
path.join(OUTPUT_DIR, filename),
buffer,
);
return { url: fileurl, filename };
}),
}),
);
return formatResults;
}),
Expand Down
24 changes: 8 additions & 16 deletions templates/types/streaming/nextjs/app/api/sandbox/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,18 @@ async function downloadCellResults(
cellResults.map(async (res) => {
const formats = res.formats(); // available formats in the result
const formatResults = await Promise.all(
formats.map(async (ext) => {
if (ext === "chart") {
// save chart data as json file
const filename = `${crypto.randomUUID()}.json`;
formats
.filter((ext) => ["png", "svg", "jpeg", "pdf"].includes(ext))
.map(async (ext) => {
const filename = `${crypto.randomUUID()}.${ext}`;
const base64 = res[ext as keyof Result];
const buffer = Buffer.from(base64, "base64");
const fileurl = await saveDocument(
path.join(OUTPUT_DIR, filename),
JSON.stringify(res[ext as keyof Result]),
buffer,
);
return { url: fileurl, filename };
}

const filename = `${crypto.randomUUID()}.${ext}`;
const base64 = res[ext as keyof Result];
const buffer = Buffer.from(base64, "base64");
const fileurl = await saveDocument(
path.join(OUTPUT_DIR, filename),
buffer,
);
return { url: fileurl, filename };
}),
}),
);
return formatResults;
}),
Expand Down

0 comments on commit d5534fc

Please sign in to comment.