All files / lib/utils dataUrlToFile.ts

10% Statements 1/10
100% Branches 0/0
0% Functions 0/1
10% Lines 1/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 131x                        
export const dataURLtoFile = (dataUrl: string, filename: string) => {
  const arr = dataUrl.split(',');
  const mimeType = arr[0].match(/:(.*?);/)?.[1];
  const decodedData = atob(arr[1]);
  let lengthOfDecodedData = decodedData.length;
  const u8array = new Uint8Array(lengthOfDecodedData);
  while (lengthOfDecodedData > -1) {
    u8array[lengthOfDecodedData] = decodedData.charCodeAt(lengthOfDecodedData);
    lengthOfDecodedData -= 1;
  }
  return new File([u8array], filename, { type: mimeType });
};