@@ -66,27 +66,28 @@ export function truncate(value: string | undefined, maxLength: number, suffix =
6666}
6767
6868/**
69- * Checks if the provided string is a valid Swiss IBAN number
69+ * Checks if the provided string is a valid swiss IBAN number
7070 * @param ibanNumber The provided IBAN number to check
71+ * Must be in one of the following formats:
72+ * - "CHXX XXXX XXXX XXXX XXXX X" with whitespaces
73+ * - "CHXXXXXXXXXXXXXXXXXXX" without whitespaces
7174 * @returns The result of the IBAN number check
7275 */
7376export function isValidSwissIbanNumber ( ibanNumber : string ) : boolean {
7477 if ( isNullOrWhitespace ( ibanNumber ) ) {
7578 return false ;
7679 }
77- const compactIban = ibanNumber . replaceAll ( / \s + / g, "" ) ;
78- if ( ! / ^ C H \d { 19 } $ / . test ( compactIban ) ) {
80+
81+ const compactIbanNumberWithWhiteSpaces = new RegExp ( / ^ C H \d { 2 } (?: \s ? \d { 4 } ) { 4 } \s ? \d { 1 } $ / ) ;
82+ const compactIbanNumberWithoutWhiteSpaces = new RegExp ( / ^ C H \d { 19 } $ / ) ;
83+
84+ if ( ! compactIbanNumberWithWhiteSpaces . test ( ibanNumber ) && ! compactIbanNumberWithoutWhiteSpaces . test ( ibanNumber ) ) {
7985 return false ;
8086 }
81- const rearrangedIban = compactIban . slice ( 4 ) + compactIban . slice ( 0 , 4 ) ;
82- const numericStr = Array . from ( rearrangedIban , ( ch ) => {
83- if ( / [ A - Z ] / . test ( ch ) ) {
84- const code = ch . codePointAt ( 0 ) ;
85- // code is never undefined!
86- return ( code ! - 55 ) . toString ( ) ;
87- }
88- return ch ;
89- } ) . join ( "" ) ;
87+
88+ const compactInsuranceNumber = ibanNumber . replaceAll ( / [ \s . ] + / g, "" ) ;
89+ const rearrangedIban = compactInsuranceNumber . slice ( 4 ) + compactInsuranceNumber . slice ( 0 , 4 ) ;
90+ const numericStr = rearrangedIban . replaceAll ( / [ A - Z ] / g, ( ch ) => ( ch . codePointAt ( 0 ) ! - 55 ) . toString ( ) ) ;
9091
9192 let restOfCalculation = 0 ;
9293 for ( const digit of numericStr ) {
0 commit comments