All files / use-memory-monitor/src/utils formatting.ts

100% Statements 30/30
100% Branches 33/33
100% Functions 6/6
100% Lines 30/30

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                      300x 119x     181x 1x     180x 2x     178x               178x 178x     178x 178x   178x                           8x 2x     6x                   10x 2x     8x                               92x                                       104x 47x     57x 57x                   8x 2x     6x 2x     4x 2x     2x    
import { BYTE_UNITS, BYTES_PER_UNIT } from "../constants";
import type { FormattedMemory, MemoryInfo } from "../types";
 
/**
 * Format bytes to human-readable string (e.g., "45.2 MB")
 *
 * @param bytes - Number of bytes to format
 * @param decimals - Number of decimal places (default: 2)
 * @returns Formatted string with unit
 */
export function formatBytes(bytes: number | null | undefined, decimals: number = 2): string {
  if (bytes === null || bytes === undefined) {
    return "N/A";
  }
 
  if (bytes === 0) {
    return "0 B";
  }
 
  if (bytes < 0) {
    return `-${formatBytes(Math.abs(bytes), decimals)}`;
  }
 
  const unitIndex = Math.max(
    0,
    Math.min(
      Math.floor(Math.log(bytes) / Math.log(BYTES_PER_UNIT)),
      BYTE_UNITS.length - 1
    )
  );
 
  const value = bytes / Math.pow(BYTES_PER_UNIT, unitIndex);
  const unit = BYTE_UNITS[unitIndex];
 
  // Remove unnecessary trailing zeros
  const formatted = value.toFixed(decimals);
  const trimmed = parseFloat(formatted).toString();
 
  return `${trimmed} ${unit}`;
}
 
/**
 * Format a percentage value
 *
 * @param percentage - Percentage value (0-100)
 * @param decimals - Number of decimal places (default: 1)
 * @returns Formatted percentage string
 */
export function formatPercentage(
  percentage: number | null | undefined,
  decimals: number = 1
): string {
  if (percentage === null || percentage === undefined) {
    return "N/A";
  }
 
  return `${percentage.toFixed(decimals)}%`;
}
 
/**
 * Format a number with thousand separators
 *
 * @param value - Number to format
 * @returns Formatted string with thousand separators
 */
export function formatNumber(value: number | null | undefined): string {
  if (value === null || value === undefined) {
    return "N/A";
  }
 
  return value.toLocaleString();
}
 
/**
 * Create formatted memory object for display
 *
 * @param memory - Memory info object
 * @param domNodes - DOM node count (optional)
 * @param eventListeners - Event listener count (optional)
 * @returns FormattedMemory object with human-readable strings
 */
export function createFormattedMemory(
  memory: MemoryInfo | null,
  domNodes?: number | null,
  eventListeners?: number | null
): FormattedMemory {
  return {
    heapUsed: formatBytes(memory?.heapUsed),
    heapTotal: formatBytes(memory?.heapTotal),
    heapLimit: formatBytes(memory?.heapLimit),
    domNodes: domNodes != null ? formatNumber(domNodes) : undefined,
    eventListeners: eventListeners != null ? formatNumber(eventListeners) : undefined,
  };
}
 
/**
 * Calculate usage percentage from memory info
 *
 * @param heapUsed - Used heap size in bytes
 * @param heapLimit - Heap limit in bytes
 * @returns Usage percentage (0-100) or null if invalid
 */
export function calculateUsagePercentage(
  heapUsed: number | null | undefined,
  heapLimit: number | null | undefined
): number | null {
  if (heapUsed == null || heapLimit == null || heapLimit <= 0) {
    return null;
  }
 
  const percentage = (heapUsed / heapLimit) * 100;
  return Math.min(100, Math.max(0, percentage));
}
 
/**
 * Format time duration in milliseconds to human-readable string
 *
 * @param ms - Duration in milliseconds
 * @returns Formatted duration string
 */
export function formatDuration(ms: number): string {
  if (ms < 1000) {
    return `${ms}ms`;
  }
 
  if (ms < 60000) {
    return `${(ms / 1000).toFixed(1)}s`;
  }
 
  if (ms < 3600000) {
    return `${(ms / 60000).toFixed(1)}m`;
  }
 
  return `${(ms / 3600000).toFixed(1)}h`;
}