File size: 4,110 Bytes
1e92f2d |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
function buildMap(rows) {
return rows.reduce((accum, { dataIndex }) => {
accum[dataIndex] = true;
return accum;
}, {});
}
function escapeDangerousCSVCharacters(data) {
if (typeof data === 'string') {
// Places single quote before the appearance of dangerous characters if they
// are the first in the data string.
return data.replace(/^\+|^\-|^\=|^\@/g, "'$&");
}
return data;
}
function warnDeprecated(warning, consoleWarnings = true) {
let consoleWarn = typeof consoleWarnings === 'function' ? consoleWarnings : console.warn;
if (consoleWarnings) {
consoleWarn(`Deprecation Notice: ${warning}`);
}
}
function warnInfo(warning, consoleWarnings = true) {
let consoleWarn = typeof consoleWarnings === 'function' ? consoleWarnings : console.warn;
if (consoleWarnings) {
consoleWarn(`${warning}`);
}
}
function getPageValue(count, rowsPerPage, page) {
const totalPages = count <= rowsPerPage ? 1 : Math.ceil(count / rowsPerPage);
// `page` is 0-indexed
return page >= totalPages ? totalPages - 1 : page;
}
function getCollatorComparator() {
if (!!Intl) {
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
return collator.compare;
}
const fallbackComparator = (a, b) => a.localeCompare(b);
return fallbackComparator;
}
function sortCompare(order) {
return (a, b) => {
var aData = a.data === null || typeof a.data === 'undefined' ? '' : a.data;
var bData = b.data === null || typeof b.data === 'undefined' ? '' : b.data;
return (
(typeof aData.localeCompare === 'function' ? aData.localeCompare(bData) : aData - bData) *
(order === 'asc' ? 1 : -1)
);
};
}
function buildCSV(columns, data, options) {
const replaceDoubleQuoteInString = columnData =>
typeof columnData === 'string' ? columnData.replace(/\"/g, '""') : columnData;
const buildHead = columns => {
return (
columns
.reduce(
(soFar, column) =>
column.download
? soFar +
'"' +
escapeDangerousCSVCharacters(replaceDoubleQuoteInString(column.label || column.name)) +
'"' +
options.downloadOptions.separator
: soFar,
'',
)
.slice(0, -1) + '\r\n'
);
};
const CSVHead = buildHead(columns);
const buildBody = data => {
if (!data.length) return '';
return data
.reduce(
(soFar, row) =>
soFar +
'"' +
row.data
.filter((_, index) => columns[index].download)
.map(columnData => escapeDangerousCSVCharacters(replaceDoubleQuoteInString(columnData)))
.join('"' + options.downloadOptions.separator + '"') +
'"\r\n',
'',
)
.trim();
};
const CSVBody = buildBody(data);
const csv = options.onDownload
? options.onDownload(buildHead, buildBody, columns, data)
: `${CSVHead}${CSVBody}`.trim();
return csv;
}
function downloadCSV(csv, filename) {
const blob = new Blob([csv], { type: 'text/csv' });
/* taken from react-csv */
if (navigator && navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(blob, filename);
} else {
const dataURI = `data:text/csv;charset=utf-8,${csv}`;
const URL = window.URL || window.webkitURL;
const downloadURI = typeof URL.createObjectURL === 'undefined' ? dataURI : URL.createObjectURL(blob);
let link = document.createElement('a');
link.setAttribute('href', downloadURI);
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
function createCSVDownload(columns, data, options, downloadCSV) {
const csv = buildCSV(columns, data, options);
if (options.onDownload && csv === false) {
return;
}
downloadCSV(csv, options.downloadOptions.filename);
}
export {
buildMap,
getPageValue,
getCollatorComparator,
sortCompare,
createCSVDownload,
buildCSV,
downloadCSV,
warnDeprecated,
warnInfo,
escapeDangerousCSVCharacters,
};
|