All files / src/types cia.utility.ts

93.87% Statements 46/49
91.89% Branches 34/37
100% Functions 8/8
93.33% Lines 42/45

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                  7x   5x   4x                   77x 18x 14x 18x 17x 9x 1x                     12x 1x 1x 5x 2x 1x 2x                                             22x 22x 22x   22x 12x     10x   22x 5x 15x   5x       5x 2x     9x 6x   3x 3x           5x   5x                               7x           7x 1x 2x 2x 1x 1x        
import { SecurityLevel } from "./cia";
 
/**
 * Format security level string for display
 * 
 * @param level - Security level to format
 * @returns Formatted security level string
 */
export function formatSecurityLevel(level?: string): string {
  if (!level) return "None";
  
  if (level.toUpperCase() === "VERY HIGH") return "Very High";
  
  return level.charAt(0).toUpperCase() + level.slice(1).toLowerCase();
}
 
/**
 * Convert security level to numeric value
 * 
 * @param level - Security level to convert
 * @returns Numeric value (0-4)
 */
export function getSecurityLevelValue(level: SecurityLevel): number {
  switch (level) {
    case "None": return 0;
    case "Low": return 1;
    case "Moderate": return 2;
    case "High": return 3;
    case "Very High": return 4;
    default: return 0;
  }
}
 
/**
 * Get security level based on numeric value
 * 
 * @param value - Numeric value to convert
 * @returns Corresponding security level
 */
export function getSecurityLevelFromValue(value: number): SecurityLevel {
  switch (value) {
    case 0: return "None";
    case 1: return "Low";
    case 2: return "Moderate";
    case 3: return "High";
    case 4: return "Very High";
    default: return "None";
  }
}
 
/**
 * Calculate overall security level based on component levels
 * 
 * ## Business Perspective
 * 
 * This function provides organizations with a consolidated view of their 
 * security posture across all three components of the CIA triad.
 * It helps in strategic decision-making and resource allocation. 💼
 * 
 * @param availabilityLevel - Availability security level
 * @param integrityLevel - Integrity security level
 * @param confidentialityLevel - Confidentiality security level
 * @returns Overall security level
 */
export function calculateOverallSecurityLevel(
  availabilityLevel: SecurityLevel,
  integrityLevel: SecurityLevel,
  confidentialityLevel: SecurityLevel
): SecurityLevel {
  const availValue = getSecurityLevelValue(availabilityLevel);
  const integValue = getSecurityLevelValue(integrityLevel);
  const confidValue = getSecurityLevelValue(confidentialityLevel);
  
  if (availabilityLevel === integrityLevel && integrityLevel === confidentialityLevel) {
    return availabilityLevel;
  }
  
  const hasNone = availabilityLevel === "None" || integrityLevel === "None" || confidentialityLevel === "None";
  
  if (hasNone) {
    const noneCount = [availabilityLevel, integrityLevel, confidentialityLevel]
      .filter(level => level === "None").length;
    
    Iif (noneCount === 3) {
      return "None";
    }
    
    if (noneCount >= 2) {
      return "None";
    }
    
    const nonNoneValues = [availValue, integValue, confidValue].filter(val => val > 0);
    const avgNonNoneValue = nonNoneValues.reduce((sum, val) => sum + val, 0) / nonNoneValues.length;
    
    Eif (avgNonNoneValue > 1) {
      return "Low";
    }
    
    return "None";
  }
  
  const avgValue = Math.round((availValue + integValue + confidValue) / 3);
  
  return getSecurityLevelFromValue(avgValue);
}
 
/**
 * Calculate risk level based on security levels
 * 
 * @param availabilityLevel - Availability security level
 * @param integrityLevel - Integrity security level
 * @param confidentialityLevel - Confidentiality security level
 * @returns Risk level (Critical, High, Medium, Low, Minimal)
 */
export function calculateRiskLevel(
  availabilityLevel: SecurityLevel,
  integrityLevel: SecurityLevel,
  confidentialityLevel: SecurityLevel
): string {
  const overallLevel = calculateOverallSecurityLevel(
    availabilityLevel,
    integrityLevel,
    confidentialityLevel
  );
  
  switch (overallLevel) {
    case "None": return "Critical";
    case "Low": return "High";
    case "Moderate": return "Medium";
    case "High": return "Low";
    case "Very High": return "Minimal";
    default: return "Unknown";
  }
}