@chayns-components/core
Version:
A set of beautiful React components for developing your own applications with chayns.
58 lines • 1.85 kB
JavaScript
export const hasDuplicate = (items, newItem, comparator) => items.some(item => comparator(item, newItem));
export const filterDuplicateFile = _ref => {
let {
files,
newFile
} = _ref;
return hasDuplicate(files, newFile, (a, b) => a.name === b.name && a.size === b.size);
};
export const filterDuplicateFileUrls = _ref2 => {
let {
files,
newFile
} = _ref2;
return hasDuplicate(files, newFile, (a, b) => a === b);
};
export const getHumanSize = bytes => {
const FILE_SIZE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
let size = bytes || 0;
let unitIndex = 0;
while (size >= 1024) {
size /= 1024;
unitIndex += 1;
}
size = Math.round(size * 10) / 10;
return `${size.toString().replace('.', ',')} ${FILE_SIZE_UNITS[unitIndex] ?? ''}`;
};
export const isValidFileType = _ref3 => {
let {
types,
file
} = _ref3;
const allowedTypesArray = types.split(',').map(type => type.trim());
const fileType = file.type;
return allowedTypesArray.some(type => {
if (type.endsWith('/*')) {
const baseType = type.slice(0, -2); // remove '/*'
return fileType.startsWith(baseType);
}
return fileType === type;
});
};
export const MIME_TYPE_MAPPING = {
// Haupttypen
'application/pdf': 'fa fa-file-pdf',
'application/zip': 'fa fa-file-archive',
'application/msword': 'fa fa-file-word',
'application/vnd.ms-excel': 'fa fa-file-excel',
// Typen mit Wildcards
'application/vnd.openxmlformats-officedocument.': 'fa fa-file-word',
'application/x-': 'fa fa-file-archive',
// Addiere nur spezifische Fälle
'text/pdf': 'fa fa-file-pdf'
};
export const getIconByMimeType = mimeType => {
const matchedType = Object.keys(MIME_TYPE_MAPPING).find(type => mimeType?.startsWith(type));
return MIME_TYPE_MAPPING[matchedType] || 'fa fa-file';
};
//# sourceMappingURL=file.js.map