button = (data, filename = 'data.csv') => {
if (!data) throw new Error('Array of data required as first argument');
let downloadData;
if (filename.includes('.csv')) {
downloadData = new Blob([d3.csvFormat(data)], { type: "text/csv" });
} else if (filename.includes('.json')) {
downloadData = new Blob([JSON.stringify(data, null, 2)], {
type: "application/json"
});
} else {
downloadData = new Blob([data], { type: "application/octet-stream" });
}
const size = (downloadData.size / 1024).toFixed(0);
const button = DOM.download(
downloadData,
filename,
`Download ${filename} (~${size} KB)`
);
return button;
}