All files / src/utils index.ts

93.75% Statements 30/32
91.66% Branches 22/24
66.66% Functions 4/6
92.85% Lines 26/28

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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                                                        11x       11x     11x   11x 10x             11x 5x                       11x                               11x                   11x                         11x                                 11x                             11x     11x           6x               6x           6x 5x 4x 2x 1x     11x         6x 6x                                                             11x     11x        
/**
 * Central export of utility functions for CIA Compliance Manager
 *
 * This index file provides organized exports of all utility functions
 * to ensure they're properly accessible throughout the application.
 *
 * @packageDocumentation
 */
 
// Import all utilities to help organize exports
import {
  BUSINESS_IMPACT_CATEGORIES,
  RISK_LEVELS,
} from "../constants/riskConstants";
import { calculateRiskLevel } from "../types/cia.utility";
import { SecurityLevel } from "../types/cia";
import * as colorUtils from "./colorUtils";
import * as costUtils from "./costCalculationUtils";
import * as errorUtils from "./errorUtils";
import * as formatUtils from "./formatUtils";
import * as levelUtils from "./levelValuesUtils";
import * as riskUtils from "./riskUtils";
import * as securityUtils from "./securityLevelUtils";
import * as typeGuards from "./typeGuards";
import * as widgetUtils from "./widgetHelpers";
 
// Export individual utilities with explicit names to avoid conflicts
// Color utilities
export const { getSecurityLevelColorClass } = colorUtils;
 
// Add missing color utilities with appropriate fallbacks
export const getColorForRiskLevel =
  colorUtils.getSecurityLevelColorClass ||
  ((_level: string) => `text-gray-600`);
export const getComplianceStatusColor =
  colorUtils.getSecurityLevelColorClass ||
  ((_status: string) => `text-gray-600`);
export const getProgressColor = (progress: number) =>
  progress > 75
    ? "text-green-500"
    : progress > 50
    ? "text-blue-500"
    : progress > 25
    ? "text-yellow-500"
    : "text-red-500";
export const getSeverityColor = (severity: string) =>
  severity === "high"
    ? "text-red-500"
    : severity === "medium"
    ? "text-yellow-500"
    : "text-green-500";
 
// Cost calculation utilities
export const {
  calculateImplementationCost,
  calculateTotalSecurityCost,
  calculateSecurityROI,
  getRecommendedBudgetAllocation,
} = costUtils;
 
// Formatting utilities
export const {
  formatBudgetPercentage,
  formatCurrency,
  formatCurrencyWithOptions,
  formatDate,
  formatLargeNumber,
  formatNumber,
  formatNumberWithDecimals,
  formatPercentage,
  formatRiskLevel,
  formatTimeframe,
  formatUptime,
  toTitleCase,
} = formatUtils;
 
// Level value utilities
export const {
  calculateOverallSecurityLevel: calculateOverallSecurityLevelFromValues,
  compareSecurityLevels,
  getNormalizedSecurityValue,
  getSecurityLevelValue: getNumericSecurityLevelValue,
  getSecurityLevelFromValue,
  SECURITY_LEVEL_VALUES,
} = levelUtils;
 
// Risk utilities
export const {
  calculateCombinedRiskLevel,
  calculateRiskScore,
  getFormattedRiskLevel,
  getRiskBadgeVariant,
  getRiskLevelFromSecurityLevel,
  getRiskScoreFromSecurityLevel,
  getRiskSeverityDescription,
  getStatusBadgeForRiskLevel,
  parseRiskLevel,
} = riskUtils;
 
// Security level utilities
export const {
  calculateOverallSecurityLevel,
  asSecurityLevel,
  formatSecurityLevel: formatSecurityLevelString,
  getRecommendedSecurityLevel,
  getSecurityIcon,
  getSecurityLevelBadgeVariant,
  getSecurityLevelClass,
  getSecurityLevelDescription,
  getSecurityLevelPercentage,
  getSecurityLevelValue,
  isSecurityLevel,
  meetsComplianceRequirements,
  normalizeSecurityLevel,
} = securityUtils;
 
// Widget helper utilities
export const {
  formatSecurityLevel: formatSecurityLevelFromWidget,
  getWidgetColumnSpan,
  getWidgetRowSpan,
  handleWidgetError,
  KeyValuePair,
  RiskLevelKeyValue,
  sanitizeWidgetId,
  SecurityLevelBadge,
  WidgetEmptyState,
  WidgetError,
  WidgetLoading,
} = widgetUtils;
 
// Add missing widget utilities with appropriate implementations
export const calculateWidgetRiskLevel = (
  availabilityLevel: SecurityLevel,
  integrityLevel: SecurityLevel,
  confidentialityLevel: SecurityLevel
) => {
  // Basic implementation based on average security level
  const levels: Record<string, number> = {
    None: 0,
    Low: 1,
    Moderate: 2,
    High: 3,
    "Very High": 4,
  };
 
  const avgLevel =
    (levels[availabilityLevel] +
      levels[integrityLevel] +
      levels[confidentialityLevel]) /
    3;
 
  if (avgLevel < 1) return "Critical";
  if (avgLevel < 2) return "High";
  if (avgLevel < 3) return "Medium";
  if (avgLevel < 4) return "Low";
  return "Minimal";
};
 
export const formatSecurityMetric = (
  value: number,
  prefix = "",
  suffix = ""
): string => {
  const formattedValue = new Intl.NumberFormat().format(value);
  return `${prefix}${formattedValue}${suffix}`;
};
 
// Type guards
export const {
  ensureArray,
  extractSecurityLevels,
  hasProperty,
  hasTagValue,
  isBusinessImpactCategory,
  isBusinessImpactDetails,
  isComplianceFramework,
  isComplianceFrameworkName,
  isComplianceFrameworkObject,
  isComplianceStatus,
  isNumber,
  isObject,
  isRiskLevel,
  isROIEstimate,
  isROIMetricDetails,
  isROIMetrics,
  isSecurityProfile,
  isSecurityResource,
  isString,
  isWidget,
  isWidgetConfig,
  isWidgetProps,
  isWidgetType,
  safeAccess,
  safeNumberConversion,
  hasWidgetProps,
} = typeGuards;
 
// Error utilities
export const { toErrorObject, formatError } = errorUtils;
 
// Other exports
export { BUSINESS_IMPACT_CATEGORIES, calculateRiskLevel, RISK_LEVELS };