All files / src/utils helpers.js

100% Statements 14/14
100% Branches 6/6
100% Functions 6/6
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        4x     139x 139x 2780x       49x       6x       5x       22x 8x 8x   14x               14x     4x  
/**
 * Shared utility functions — pure helpers with no external dependencies.
 */
 
const crypto = require('node:crypto');
 
function generateId() {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  const bytes = crypto.randomBytes(20);
  return Array.from(bytes, (b) => chars[b % chars.length]).join('');
}
 
function now() {
  return Date.now();
}
 
function todayStr() {
  return new Date().toISOString().split('T')[0];
}
 
function yesterdayStr() {
  return new Date(Date.now() - 86400000).toISOString().split('T')[0];
}
 
function getExtension(contentType) {
  if (contentType.startsWith('video/')) {
    const sub = contentType.slice(6);
    return sub === 'quicktime' ? 'mov' : sub;
  }
  const map = {
    'image/jpeg': 'jpg',
    'image/png': 'png',
    'image/webp': 'webp',
    'image/gif': 'gif',
    'image/heic': 'heic',
    'image/heif': 'heif',
  };
  return map[contentType] ?? 'jpg';
}
 
module.exports = { generateId, now, todayStr, yesterdayStr, getExtension };