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

gregnb / mui-datatables / #1878

17 Jul 2026 08:55AM UTC coverage: 73.256% (+0.8%) from 72.444%
#1878

push

267 of 399 branches covered (66.92%)

Branch coverage included in aggregate %.

426 of 547 relevant lines covered (77.88%)

41.29 hits per line

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

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

8
function escapeDangerousCSVCharacters(data) {
9
  if (typeof data === 'string') {
127!
10
    // Places single quote before the appearance of dangerous characters if they
127✔
11
    // are the first in the data string.
127✔
12
    return data.replace(/^\+|^\-|^\=|^\@/g, "'$&");
13
  }
14

×
15
  return data;
×
16
}
17

18
function warnDeprecated(warning, consoleWarnings = true) {
19
  let consoleWarn = typeof consoleWarnings === 'function' ? consoleWarnings : console.warn;
1✔
20
  if (consoleWarnings) {
6!
21
    consoleWarn(`Deprecation Notice:  ${warning}`);
6!
22
  }
6✔
23
}
6!
24

6!
25
function warnInfo(warning, consoleWarnings = true) {
26
  let consoleWarn = typeof consoleWarnings === 'function' ? consoleWarnings : console.warn;
27
  if (consoleWarnings) {
28
    consoleWarn(`${warning}`);
29
  }
30
}
31

1✔
32
function getPageValue(count, rowsPerPage, page) {
33
  const totalPages = count <= rowsPerPage ? 1 : Math.ceil(count / rowsPerPage);
34

4!
35
  // `page` is 0-indexed
36
  return page >= totalPages ? totalPages - 1 : page;
37
}
38

39
function getCollatorComparator() {
1✔
40
  if (!!Intl) {
41
    const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
42
    return collator.compare;
4✔
43
  }
44

16✔
45
  const fallbackComparator = (a, b) => a.localeCompare(b);
46
  return fallbackComparator;
47
}
48

49
function sortCompare(order) {
50
  return (a, b) => {
1✔
51
    var aData = a.data === null || typeof a.data === 'undefined' ? '' : a.data;
1✔
52
    var bData = b.data === null || typeof b.data === 'undefined' ? '' : b.data;
53
    return (
54
      (typeof aData.localeCompare === 'function' ? aData.localeCompare(bData) : aData - bData) *
1!
55
      (order === 'asc' ? 1 : -1)
×
56
    );
57
  };
1✔
58
}
59

1!
60
function buildCSV(columns, data, options) {
1!
61
  const replaceDoubleQuoteInString = columnData =>
62
    typeof columnData === 'string' ? columnData.replace(/\"/g, '""') : columnData;
1✔
63

1✔
64
  const buildHead = columns => {
1✔
65
    return (
1✔
66
      columns
1✔
67
        .reduce(
1✔
68
          (soFar, column) =>
69
            column.download
70
              ? soFar +
71
                '"' +
72
                escapeDangerousCSVCharacters(replaceDoubleQuoteInString(column.label || column.name)) +
73
                '"' +
74
                options.downloadOptions.separator
75
              : soFar,
76
          '',
77
        )
78
        .slice(0, -1) + '\r\n'
79
    );
80
  };
81
  const CSVHead = buildHead(columns);
82

83
  const buildBody = data => {
84
    if (!data.length) return '';
85
    return data
86
      .reduce(
87
        (soFar, row) =>
88
          soFar +
89
          '"' +
90
          row.data
91
            .filter((_, index) => columns[index].download)
92
            .map(columnData => escapeDangerousCSVCharacters(replaceDoubleQuoteInString(columnData)))
93
            .join('"' + options.downloadOptions.separator + '"') +
94
          '"\r\n',
95
        '',
96
      )
97
      .trim();
98
  };
99
  const CSVBody = buildBody(data);
100

101
  const csv = options.onDownload
102
    ? options.onDownload(buildHead, buildBody, columns, data)
103
    : `${CSVHead}${CSVBody}`.trim();
104

105
  return csv;
106
}
107

108
function downloadCSV(csv, filename) {
109
  const blob = new Blob([csv], { type: 'text/csv' });
110

111
  /* taken from react-csv */
112
  if (navigator && navigator.msSaveOrOpenBlob) {
113
    navigator.msSaveOrOpenBlob(blob, filename);
114
  } else {
115
    const dataURI = `data:text/csv;charset=utf-8,${csv}`;
116

117
    const URL = window.URL || window.webkitURL;
118
    const downloadURI = typeof URL.createObjectURL === 'undefined' ? dataURI : URL.createObjectURL(blob);
119

120
    let link = document.createElement('a');
121
    link.setAttribute('href', downloadURI);
122
    link.setAttribute('download', filename);
123
    document.body.appendChild(link);
124
    link.click();
125
    document.body.removeChild(link);
126
  }
127
}
128

129
function createCSVDownload(columns, data, options, downloadCSV) {
130
  const csv = buildCSV(columns, data, options);
131

132
  if (options.onDownload && csv === false) {
133
    return;
134
  }
135

136
  downloadCSV(csv, options.downloadOptions.filename);
137
}
138

139
export {
140
  buildMap,
141
  getPageValue,
142
  getCollatorComparator,
143
  sortCompare,
144
  createCSVDownload,
145
  buildCSV,
146
  downloadCSV,
147
  warnDeprecated,
148
  warnInfo,
149
  escapeDangerousCSVCharacters,
150
};
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