@tokens-studio/dtcg-convert
Version:
Conversion tool that converts Style Dictionary JSON formatted file(s) to DTCG
37 lines • 1.39 kB
JavaScript
import { ZipWriter, BlobWriter, TextReader } from '@zip.js/zip.js';
function downloadBlob(blob, filename) {
const url = URL.createObjectURL(blob);
// Auto-download the ZIP through anchor
const anchor = document.createElement('a');
anchor.href = url;
anchor.download = filename;
anchor.click();
URL.revokeObjectURL(url);
}
export function downloadJSON(stringOrBlob, filename = 'output.json') {
/** @type {Blob} */
let jsonBlob;
// check if it's a Blob.., instanceof is too strict e.g. Blob polyfills
if (stringOrBlob.constructor.name === 'Blob') {
jsonBlob = stringOrBlob;
}
else {
jsonBlob = new Blob([stringOrBlob], { type: 'application/json' });
}
downloadBlob(jsonBlob, filename);
}
export async function downloadZIP(filesOrBlob, filename = 'output.zip') {
let zipBlob;
// check if it's a Blob.., instanceof is too strict e.g. Blob polyfills
if (filesOrBlob.constructor.name === 'Blob') {
zipBlob = filesOrBlob;
}
else {
const zipWriter = new ZipWriter(new BlobWriter('application/zip'));
await Promise.all(Object.entries(filesOrBlob).map(([key, value]) => zipWriter.add(key, new TextReader(value))));
// Close zip and make into URL
zipBlob = await zipWriter.close();
}
downloadBlob(zipBlob, filename);
}
//# sourceMappingURL=downloadFile.js.map