mui-table-2026 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.js ADDED
@@ -0,0 +1,150 @@
1
+ function buildMap(rows) {
2
+ return rows.reduce((accum, { dataIndex }) => {
3
+ accum[dataIndex] = true;
4
+ return accum;
5
+ }, {});
6
+ }
7
+
8
+ function escapeDangerousCSVCharacters(data) {
9
+ if (typeof data === 'string') {
10
+ // Places single quote before the appearance of dangerous characters if they
11
+ // are the first in the data string.
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;
20
+ if (consoleWarnings) {
21
+ consoleWarn(`Deprecation Notice: ${warning}`);
22
+ }
23
+ }
24
+
25
+ function warnInfo(warning, consoleWarnings = true) {
26
+ let consoleWarn = typeof consoleWarnings === 'function' ? consoleWarnings : console.warn;
27
+ if (consoleWarnings) {
28
+ consoleWarn(`${warning}`);
29
+ }
30
+ }
31
+
32
+ function getPageValue(count, rowsPerPage, page) {
33
+ const totalPages = count <= rowsPerPage ? 1 : Math.ceil(count / rowsPerPage);
34
+
35
+ // `page` is 0-indexed
36
+ return page >= totalPages ? totalPages - 1 : page;
37
+ }
38
+
39
+ function getCollatorComparator() {
40
+ if (!!Intl) {
41
+ const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
42
+ return collator.compare;
43
+ }
44
+
45
+ const fallbackComparator = (a, b) => a.localeCompare(b);
46
+ return fallbackComparator;
47
+ }
48
+
49
+ function sortCompare(order) {
50
+ return (a, b) => {
51
+ var aData = a.data === null || typeof a.data === 'undefined' ? '' : a.data;
52
+ var bData = b.data === null || typeof b.data === 'undefined' ? '' : b.data;
53
+ return (
54
+ (typeof aData.localeCompare === 'function' ? aData.localeCompare(bData) : aData - bData) *
55
+ (order === 'asc' ? 1 : -1)
56
+ );
57
+ };
58
+ }
59
+
60
+ function buildCSV(columns, data, options) {
61
+ const replaceDoubleQuoteInString = columnData =>
62
+ typeof columnData === 'string' ? columnData.replace(/\"/g, '""') : columnData;
63
+
64
+ const buildHead = columns => {
65
+ return (
66
+ columns
67
+ .reduce(
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
+ };