@spaced-out/ui-design-system
Version:
Sense UI components library
54 lines (44 loc) • 1.49 kB
Flow
// @flow strict
export const uuid = (): string => {
let dt = new Date().getTime();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (dt + Math.random() * 16) % 16 | 0;
dt = Math.floor(dt / 16);
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
});
};
export const range = (start: number, end: number): Array<number> => {
const length = end - start + 1;
return Array.from({length}, (_, i) => start + i);
};
export const convertFileSize = (fileSize: number = 0): string => {
let sizeInBytes = fileSize;
// Check if the file size is less than 1024
if (sizeInBytes < 0) {
sizeInBytes = Math.abs(sizeInBytes);
}
// Handle the case where the file size is 0 or negative
if (sizeInBytes <= 0) {
return '0 B';
}
// Check if the file size is less than 1024
if (sizeInBytes < 1024) {
// Return the file size in bytes
return sizeInBytes.toString() + ' B';
}
// Check if the file size is less than 1048576
else if (sizeInBytes < 1048576) {
// Return the file size in KB
return (sizeInBytes / 1024).toFixed(1) + ' KB';
}
// Check if the file size is less than 1073741824
else if (sizeInBytes < 1073741824) {
// Return the file size in MB
return (sizeInBytes / 1048576).toFixed(1) + ' MB';
}
// Otherwise, the file size is greater than or equal to 1073741824
else {
// Return the file size in GB
return (sizeInBytes / 1073741824).toFixed(1) + ' GB';
}
};