• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gregnb / mui-datatables / #1880

01 Aug 2019 07:29AM UTC coverage: 74.963% (+3.7%) from 71.311%
#1880

push

gabrielliwerant
2.7.0

444 of 611 branches covered (72.67%)

Branch coverage included in aggregate %.

568 of 739 relevant lines covered (76.86%)

85.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.63
/src/utils.js
1
function buildMap(rows) {
2
  return rows.reduce((accum, { dataIndex }) => {
17✔
3
    accum[dataIndex] = true;
26✔
4
    return accum;
26✔
5
  }, {});
6
}
7

8
function getCollatorComparator() {
9
  if (!!Intl) {
276!
10
    const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
276✔
11
    return collator.compare;
276✔
12
  }
13

14
  const fallbackComparator = (a, b) => a.localeCompare(b);
×
15
  return fallbackComparator;
×
16
}
17

18
function sortCompare(order) {
19
  return (a, b) => {
3✔
20
    if (a.data === null) a.data = '';
13!
21
    if (b.data === null) b.data = '';
13!
22
    return (
13✔
23
      (typeof a.data.localeCompare === 'function' ? a.data.localeCompare(b.data) : a.data - b.data) *
13!
24
      (order === 'asc' ? 1 : -1)
13✔
25
    );
26
  };
27
}
28

29
function createCSVDownload(columns, data, options) {
30
  const replaceDoubleQuoteInString = columnData =>
2✔
31
    typeof columnData === 'string' ? columnData.replace(/\"/g, '""') : columnData;
×
32

33
  const buildHead = columns => {
2✔
34
    return (
2✔
35
      columns
36
        .reduce(
37
          (soFar, column) =>
38
            column.download
8!
39
              ? soFar + '"' + replaceDoubleQuoteInString(column.name) + '"' + options.downloadOptions.separator
40
              : soFar,
41
          '',
42
        )
43
        .slice(0, -1) + '\r\n'
44
    );
45
  };
46
  const CSVHead = buildHead(columns);
2✔
47

48
  const buildBody = data => {
2✔
49
    return data
2✔
50
      .reduce(
51
        (soFar, row) =>
52
          soFar +
8✔
53
          '"' +
54
          row.data
55
            .filter((_, index) => columns[index].download)
32✔
56
            .map(columnData => replaceDoubleQuoteInString(columnData))
×
57
            .join('"' + options.downloadOptions.separator + '"') +
58
          '"\r\n',
59
        [],
60
      )
61
      .trim();
62
  };
63
  const CSVBody = buildBody(data);
2✔
64

65
  const csv = options.onDownload
2✔
66
    ? options.onDownload(buildHead, buildBody, columns, data)
67
    : `${CSVHead}${CSVBody}`.trim();
68

69
  if (options.onDownload && csv === false) {
2!
70
    return;
×
71
  }
72

73
  const blob = new Blob([csv], { type: 'text/csv' });
2✔
74

75
  /* taken from react-csv */
76
  if (navigator && navigator.msSaveOrOpenBlob) {
2!
77
    navigator.msSaveOrOpenBlob(blob, options.downloadOptions.filename);
×
78
  } else {
79
    const dataURI = `data:text/csv;charset=utf-8,${csv}`;
2✔
80

81
    const URL = window.URL || window.webkitURL;
2!
82
    const downloadURI = typeof URL.createObjectURL === 'undefined' ? dataURI : URL.createObjectURL(blob);
2!
83

84
    let link = document.createElement('a');
2✔
85
    link.setAttribute('href', downloadURI);
2✔
86
    link.setAttribute('download', options.downloadOptions.filename);
2✔
87
    document.body.appendChild(link);
2✔
88
    link.click();
2✔
89
    document.body.removeChild(link);
2✔
90
  }
91
}
92

93
export { buildMap, getCollatorComparator, sortCompare, createCSVDownload };
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc