@@ -116,7 +116,20 @@ export function isValidSwissSocialInsuranceNumber(socialInsuranceNumber: string)
116116 * @property {string } iban - The cleaned IBAN, only present if valid
117117 * @property {string } ibanFormatted - The formatted IBAN, only present if valid
118118 */
119- export function tryParseSwissIbanNumber ( unformattedIbanNumber ?: string ) {
119+ export function tryParseSwissIbanNumber ( unformattedIbanNumber ?: string ) : {
120+ /**
121+ * Indicates whether the IBAN is valid or not
122+ */
123+ isValid : boolean ;
124+ /**
125+ * The cleaned IBAN, only present if valid
126+ */
127+ iban ?: string ;
128+ /**
129+ * The formatted IBAN, only present if valid
130+ */
131+ ibanFormatted ?: string ;
132+ } {
120133 if ( isNullOrWhitespace ( unformattedIbanNumber ) ) {
121134 return { isValid : false } ;
122135 }
@@ -130,3 +143,39 @@ export function tryParseSwissIbanNumber(unformattedIbanNumber?: string) {
130143 ibanFormatted : isValid ? iban . match ( / .{ 1 , 4 } / g) ?. join ( " " ) : undefined ,
131144 } ;
132145}
146+
147+ /**
148+ * Attempts to parse and validate a Swiss social insurance number.
149+ * @param unformattedInsuranceNumber - The unformatted Swiss social insurance number
150+ * @returns The result object with the following properties:
151+ * @property {boolean } isValid - Indicates whether the Swiss social insurance number is valid or not
152+ * @property {string } swissSocialInsuranceNumber - The cleaned Swiss social insurance number, only present if valid
153+ * @property {string } swissSocialInsuranceNumberFormatted - The formatted Swiss social insurance number, only present if valid
154+ */
155+ export function tryParseSwissSocialInsuranceNumber ( unformattedInsuranceNumber ?: string ) : {
156+ /**
157+ * Indicates whether the Swiss social insurance number is valid or not
158+ */
159+ isValid : boolean ;
160+ /**
161+ * The cleaned Swiss social insurance number, only present if valid
162+ */
163+ swissSocialInsuranceNumber ?: string ;
164+ /**
165+ * The formatted Swiss social insurance number, only present if valid
166+ */
167+ swissSocialInsuranceNumberFormatted ?: string ;
168+ } {
169+ if ( isNullOrWhitespace ( unformattedInsuranceNumber ) ) {
170+ return { isValid : false } ;
171+ }
172+
173+ const insuranceNumber = unformattedInsuranceNumber ! . replaceAll ( / \D / g, "" ) ;
174+ const isValid = isValidSwissSocialInsuranceNumber ( insuranceNumber ) ;
175+
176+ return {
177+ isValid : isValid ,
178+ swissSocialInsuranceNumber : isValid ? insuranceNumber : undefined ,
179+ swissSocialInsuranceNumberFormatted : isValid ? insuranceNumber . replace ( / ^ ( \d { 3 } ) ( \d { 4 } ) ( \d { 4 } ) ( \d { 2 } ) $ / , "$1.$2.$3.$4" ) : undefined ,
180+ } ;
181+ }
0 commit comments