# All files / src/lib utils.ts

0% Statements 0/47
0% Branches 0/1
0% Functions 0/1
0% Lines 0/47

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
    43
    44
    45
    46
    47
    48

     /**
     * Utility function to combine class names
     * Replaces tailwind-merge and clsx with a simple implementation
     */
    export function cn(...classes: (string | undefined | null | false)[]): string {
      return classes.filter(Boolean).join(' ')
    }

    /**
     * Format bytes to human readable string
     */
    export function formatBytes(bytes: number): string {
      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 Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
    }

    /**
     * Debounce function
     */
    export function debounce<T extends (...args: unknown[]) => unknown>(
      func: T,
      wait: number
    ): (...args: Parameters<T>) => void {
      let timeout: NodeJS.Timeout | null = null

      return function executedFunction(...args: Parameters<T>) {
        const later = () => {
          timeout = null
          func(...args)
        }

        if (timeout) {
          clearTimeout(timeout)
        }
        timeout = setTimeout(later, wait)
      }
    }

    /**
     * Sleep utility
     */
    export function sleep(ms: number): Promise<void> {
      return new Promise(resolve => setTimeout(resolve, ms))
    }