All files / extensions/tmtv/src/utils createAndDownloadTMTVReport.js

100% Statements 28/28
88.23% Branches 15/17
100% Functions 6/6
100% Lines 28/28

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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58  1x 1x 1x     22x         1x 1x 1x   22x                   1x     1x 1x 1x   1x 1x 1x 1x 3x 3x 3x 2x 2x     3x     1x       1x   1x 1x 1x 1x    
export default function createAndDownloadTMTVReport(segReport, additionalReportRows, options = {}) {
  const firstReport = segReport[Object.keys(segReport)[0]];
  const columns = Object.keys(firstReport);
  const csv = [
    columns
      .map(column =>
        column.toLowerCase().startsWith('namedstats_') ? column.substring(11) : column
      )
      .join(','),
  ];
 
  Object.values(segReport).forEach(segmentation => {
    const row = [];
    columns.forEach(column => {
      // if it is array then we need to replace , with space to avoid csv parsing error
      row.push(
        segmentation[column] && typeof segmentation[column] === 'object'
          ? Array.isArray(segmentation[column])
            ? segmentation[column].join(' ')
            : segmentation[column].value && Array.isArray(segmentation[column].value)
              ? segmentation[column].value.join(' ')
              : (segmentation[column].value ?? segmentation[column])
          : segmentation[column]
      );
    });
    csv.push(row.join(','));
  });
 
  csv.push('');
  csv.push('');
  csv.push('');
 
  csv.push(`Patient ID,${firstReport.PatientID}`);
  csv.push(`Study Date,${firstReport.StudyDate}`);
  csv.push('');
  additionalReportRows.forEach(({ key, value: values }) => {
    const temp = [];
    temp.push(`${key}`);
    Object.keys(values).forEach(k => {
      temp.push(`${k}`);
      temp.push(`${values[k]}`);
    });
 
    csv.push(temp.join(','));
  });
 
  const blob = new Blob([csv.join('\n')], {
    type: 'text/csv;charset=utf-8',
  });
 
  const url = URL.createObjectURL(blob);
 
  const a = document.createElement('a');
  a.href = url;
  a.download = options.filename ?? `${firstReport.PatientID}_tmtv.csv`;
  a.click();
}