Skip to content

Commit 7da0f6d

Browse files
committed
Add explanation
1 parent 5054683 commit 7da0f6d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/lib/string.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean {
8484
return false;
8585
}
8686

87+
/**
88+
* Validates a Swiss IBAN number.
89+
*
90+
* Steps:
91+
* - The number must start with `CH`, be 21 digits long and follow one of the accepted formats:
92+
* - `CHXX XXXX XXXX XXXX XXXX X`or `CHXXXXXXXXXXXXXXXXXXX`.
93+
* - Remove all whitespaces.
94+
* - Rearrange by moving the first 4 characters (CH + 2 check digits) to the end.
95+
* - Replace letters with numbers: A=10, B=11, ..., Z=35.
96+
* - Convert the resulting string to a large integer and calculate modulo 97.
97+
* - The number is valid if the resto of the calculation equals 1.
98+
*/
99+
87100
const compactIbanNumber = ibanNumber.replaceAll(" ", "");
88101
const rearrangedIban = compactIbanNumber.slice(4) + compactIbanNumber.slice(0, 4);
89102
const numericStr = rearrangedIban.replaceAll(/[A-Z]/g, (ch) => (ch.codePointAt(0)! - 55).toString());

0 commit comments

Comments
 (0)