ysn-rfd's picture
Upload 302 files
057576a verified
const formatFileSize = (bytes) => {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};
const generateInviteCode = () => {
return Math.random().toString(36).substring(2, 8).toUpperCase();
};
const sanitizeUser = (user) => {
const userObj = user.toObject ? user.toObject() : user;
const { password, __v, ...sanitized } = userObj;
return sanitized;
};
const paginate = (query, page = 1, limit = 20) => {
const skip = (page - 1) * limit;
return query.skip(skip).limit(limit);
};
module.exports = {
formatFileSize,
generateInviteCode,
sanitizeUser,
paginate
};