All files / src/utils swissUIDValidator.ts

100% Statements 13/13
100% Branches 12/12
100% Functions 3/3
100% Lines 11/11

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              1x               31x 27x                 10x 7x 7x                 10x 3x     7x 6x     1x    
/**
 * Swiss UID Validator
 *
 * Validates Swiss Company UID format (CHE-XXX.XXX.XXX)
 * Based on Swiss Federal Register requirements
 */
 
export const SWISS_UID_PATTERN = /^CHE-\d{3}\.\d{3}\.\d{3}$/;
 
/**
 * Validates Swiss UID format
 * @param uid - The UID string to validate
 * @returns true if valid format, false otherwise
 */
export function isValidSwissUID(uid: string | undefined | null): boolean {
  if (!uid) return false;
  return SWISS_UID_PATTERN.test(uid.trim());
}
 
/**
 * Formats a Swiss UID string (removes extra spaces, converts to uppercase)
 * @param uid - The raw UID input
 * @returns Formatted UID or empty string if invalid
 */
export function formatSwissUID(uid: string | undefined | null): string {
  if (!uid) return '';
  const cleaned = uid.trim().toUpperCase();
  return isValidSwissUID(cleaned) ? cleaned : '';
}
 
/**
 * Validates and provides error message for Swiss UID
 * @param uid - The UID to validate
 * @returns Error message if invalid, undefined if valid
 */
export function validateSwissUID(uid: string | undefined): string | undefined {
  if (!uid || uid.trim() === '') {
    return undefined; // Optional field
  }
 
  if (!isValidSwissUID(uid)) {
    return 'Invalid Swiss UID format. Expected format: CHE-XXX.XXX.XXX';
  }
 
  return undefined;
}