|
| 1 | +import { isNullOrWhitespace } from "./string"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Checks if the provided string is a valid swiss IBAN number |
| 5 | + * @param ibanNumber The provided IBAN number to check |
| 6 | + * Must be in one of the following formats: |
| 7 | + * - "CHXX XXXX XXXX XXXX XXXX X" with whitespaces |
| 8 | + * - "CHXXXXXXXXXXXXXXXXXXX" without whitespaces |
| 9 | + * @returns The result of the IBAN number check |
| 10 | + */ |
| 11 | +export function isValidSwissIbanNumber(ibanNumber: string): boolean { |
| 12 | + // 1. Reject null, undefined or whitespace-only strings |
| 13 | + if (isNullOrWhitespace(ibanNumber)) { |
| 14 | + return false; |
| 15 | + } |
| 16 | + |
| 17 | + // 2. Define allowed strict formats |
| 18 | + // - with spaces: "CHXX XXXX XXXX XXXX XXXX X" |
| 19 | + const compactIbanNumberWithWhiteSpaces = new RegExp(/^CH\d{2}(?: \d{4}){4} \d{1}$/); |
| 20 | + // - without spaces: "CHXXXXXXXXXXXXXXXXXXX" |
| 21 | + const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{19}$/); |
| 22 | + |
| 23 | + // 3. Check if input matches one of the allowed formats |
| 24 | + if (!compactIbanNumberWithWhiteSpaces.test(ibanNumber) && !compactIbanNumberWithoutWhiteSpaces.test(ibanNumber)) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + // 4. Remove all spaces to get a compact IBAN string |
| 29 | + const compactIbanNumber = ibanNumber.replaceAll(" ", ""); |
| 30 | + |
| 31 | + // 5. Rearrange IBAN for checksum calculation |
| 32 | + // - move first 4 characters (CH + 2 check digits) to the end |
| 33 | + const rearrangedIban = compactIbanNumber.slice(4) + compactIbanNumber.slice(0, 4); |
| 34 | + |
| 35 | + // 6. Replace letters with numbers (A=10, B=11, ..., Z=35) |
| 36 | + const numericStr = rearrangedIban.replaceAll(/[A-Z]/g, (ch) => (ch.codePointAt(0)! - 55).toString()); |
| 37 | + |
| 38 | + // 7. Perform modulo 97 calculation to validate IBAN |
| 39 | + let restOfCalculation = 0; |
| 40 | + for (const digit of numericStr) { |
| 41 | + restOfCalculation = (restOfCalculation * 10 + Number(digit)) % 97; |
| 42 | + } |
| 43 | + |
| 44 | + // 8. IBAN is valid only if the remainder equals 1 |
| 45 | + return restOfCalculation === 1; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Validation of social insurance number with checking the checksum |
| 50 | + * Validation according to https://www.sozialversicherungsnummer.ch/aufbau-neu.htm |
| 51 | + * @param socialInsuranceNumber The social insurance number to check |
| 52 | + * Must be in one of the following formats: |
| 53 | + * - "756.XXXX.XXXX.XX" with dots as separators |
| 54 | + * - "756XXXXXXXXXX" with digits only |
| 55 | + * @returns The result if the social insurance number is valid or not |
| 56 | + */ |
| 57 | +export function isValidSwissSocialSecurityNumber(socialInsuranceNumber: string): boolean { |
| 58 | + // 1. Check if input is empty or only whitespace |
| 59 | + if (isNullOrWhitespace(socialInsuranceNumber)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 2. Check if input matches accepted formats: |
| 65 | + * - With dots: 756.XXXX.XXXX.XX |
| 66 | + * - Without dots: 756XXXXXXXXXX |
| 67 | + */ |
| 68 | + const socialInsuranceNumberWithDots = new RegExp(/^756\.\d{4}\.\d{4}\.\d{2}$/); |
| 69 | + const socialInsuranceNumberWithoutDots = new RegExp(/^756\d{10}$/); |
| 70 | + |
| 71 | + if (!socialInsuranceNumberWithDots.test(socialInsuranceNumber) && !socialInsuranceNumberWithoutDots.test(socialInsuranceNumber)) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // 3. Remove all dots → get a string of 13 digits |
| 76 | + const compactNumber = socialInsuranceNumber.replaceAll(".", ""); |
| 77 | + |
| 78 | + /** |
| 79 | + * 4. Separate digits for checksum calculation |
| 80 | + * - first 12 digits: used to calculate checksum |
| 81 | + * - last digit: actual check digit |
| 82 | + */ |
| 83 | + const digits = compactNumber.slice(0, -1); |
| 84 | + const reversedDigits = [...digits].reverse().join(""); |
| 85 | + const reversedDigitsArray = [...reversedDigits]; |
| 86 | + |
| 87 | + /* |
| 88 | + * 5. Calculate weighted sum for checksum |
| 89 | + * - Even positions (after reversing) ×3 |
| 90 | + * - Odd positions ×1 |
| 91 | + */ |
| 92 | + let sum = 0; |
| 93 | + for (const [i, element] of reversedDigitsArray.entries()) { |
| 94 | + sum += i % 2 === 0 ? Number(element) * 3 : Number(element) * 1; |
| 95 | + } |
| 96 | + |
| 97 | + /* |
| 98 | + * 6. Calculate expected check digit |
| 99 | + * - Check digit = value to reach next multiple of 10 |
| 100 | + */ |
| 101 | + const checksum = (10 - (sum % 10)) % 10; |
| 102 | + const checknumber = Number.parseInt(compactNumber.slice(-1)); |
| 103 | + |
| 104 | + /* |
| 105 | + * 7. Compare calculated check digit with actual last digit |
| 106 | + * - If equal → valid AHV number |
| 107 | + */ |
| 108 | + return checksum === checknumber; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Formats a unformatted Swiss social insurance number to the standard format "756.XXXX.XXXX.XX" |
| 113 | + * @param unformattedSocialInsuranceNumber the unformatted Swiss social insurance number to format |
| 114 | + * @returns a object containing the formatted Swiss social insurance number and a boolean indicating if the number was valid or not |
| 115 | + */ |
| 116 | +export function formatSwissSocialInsuranceNumber(unformattedSocialInsuranceNumber: string): { |
| 117 | + /** |
| 118 | + * The formatted Swiss social insurance number or the original input if the Swiss social insurance number was invalid |
| 119 | + */ |
| 120 | + socialInsuranceNumber: string; |
| 121 | + /** |
| 122 | + * The result if the social insurance number is a valid Swiss social insurance number or not |
| 123 | + */ |
| 124 | + isValidSwissSocialInsuranceNumber: boolean; |
| 125 | +} { |
| 126 | + // 1. Check if the unformatted Swiss social insurance number is empty or only a whitespace |
| 127 | + if (isNullOrWhitespace(unformattedSocialInsuranceNumber)) { |
| 128 | + return { socialInsuranceNumber: unformattedSocialInsuranceNumber, isValidSwissSocialInsuranceNumber: false }; |
| 129 | + } |
| 130 | + |
| 131 | + // 2. Remove all non-digit characters, then format as Swiss social insurance number (XXX.XXXX.XXXX.XX) |
| 132 | + const cleaned = unformattedSocialInsuranceNumber.replaceAll(/\D+/g, ""); |
| 133 | + const formattedSwissSocialInsuranceNumber = cleaned.replaceAll(/(\d{3})(\d{4})(\d{4})(\d{2})/g, "$1.$2.$3.$4"); |
| 134 | + |
| 135 | + // 3. If the Swiss social insurance number is valid return the formatted number with the true status |
| 136 | + if (isValidSwissSocialSecurityNumber(formattedSwissSocialInsuranceNumber)) { |
| 137 | + return { socialInsuranceNumber: formattedSwissSocialInsuranceNumber, isValidSwissSocialInsuranceNumber: true }; |
| 138 | + } |
| 139 | + // 4. If the Swiss social insurance number is not valid return the formatted number with the false status |
| 140 | + return { socialInsuranceNumber: formattedSwissSocialInsuranceNumber, isValidSwissSocialInsuranceNumber: false }; |
| 141 | +} |
0 commit comments