Skip to content

Commit ac624da

Browse files
authored
Add Swiss standards function tryParseSwissIbanNumber (#81)
Add Swiss standards function tryParseSwissIbanNumber for issue #77
1 parent bc3dd52 commit ac624da

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- `splitLines` string utility function
13+
- `tryParseSwissIbanNumber` swiss standard function
1314
- `trimStart`, `trimEnd` and `trim` string type utility functions
1415

1516
### Changed

src/lib/swissStandards.spec.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isValidSwissIbanNumber, isValidSwissSocialInsuranceNumber } from "./swissStandards";
1+
import { isValidSwissIbanNumber, isValidSwissSocialInsuranceNumber, tryParseSwissIbanNumber } from "./swissStandards";
22

33
describe("Swiss standards test", () => {
44
test.each([
@@ -10,8 +10,9 @@ describe("Swiss standards test", () => {
1010
["CH93-0076-2011-6238-5295-7", false],
1111
["CH93 0000 0000 0000 0000 1", false],
1212
["ch93 0076 2011 6238 5295 7", false],
13+
["c93 0076 2011 6238 5295 7", false],
1314
["DE93 0076 2011 6238 5295 7", false],
14-
])("check if the swiss IBAN number is valid or not", (iBanNumberToCheck, expected) => {
15+
])("check if the Swiss IBAN number is valid or not", (iBanNumberToCheck, expected) => {
1516
expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected);
1617
});
1718

@@ -24,7 +25,7 @@ describe("Swiss standards test", () => {
2425
["DE34 0076 2ABC 123D EF45 3", false],
2526
["CH34 0076 2ABC 123D EF45 \n6", false],
2627
["CH34 0076 2ABC 123D EF45 !", false],
27-
])("check if the siwss IBAN number with letters is valid or not", (iBanNumberToCheck, expected) => {
28+
])("check if the Swiss IBAN number with letters is valid or not", (iBanNumberToCheck, expected) => {
2829
expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected);
2930
});
3031

@@ -39,7 +40,22 @@ describe("Swiss standards test", () => {
3940
["756.1234.5678.91", false],
4041
["test756.9217.0769.85", false],
4142
["7.56..9217...0769.85", false],
42-
])("check if the social insurance number is valid or not", (socialInsuranceNumberToCheck, expected) => {
43-
expect(isValidSwissSocialInsuranceNumber(socialInsuranceNumberToCheck)).toBe(expected);
43+
])("check if the social insurance number is valid or not", (swissSocialInsuranceNumber, expected) => {
44+
expect(isValidSwissSocialInsuranceNumber(swissSocialInsuranceNumber)).toBe(expected);
45+
});
46+
47+
test.each([
48+
[null as unknown as string, { isValid: false, iban: undefined, ibanFormatted: undefined }],
49+
[undefined as unknown as string, { isValid: false, iban: undefined, ibanFormatted: undefined }],
50+
["", { isValid: false, iban: undefined, ibanFormatted: undefined }],
51+
["CH94 0076 2011 6238 5295 7", { isValid: false, iban: undefined, ibanFormatted: undefined }],
52+
["CH1000000ABC123DEF456", { isValid: false, iban: undefined, ibanFormatted: undefined }],
53+
["CH93-0076,2011.6238\n5295\n7", { isValid: true, iban: "CH9300762011623852957", ibanFormatted: "CH93 0076 2011 6238 5295 7" }],
54+
["CH9300762011623852957", { isValid: true, iban: "CH9300762011623852957", ibanFormatted: "CH93 0076 2011 6238 5295 7" }],
55+
["CH3400762ABC123DEF456", { isValid: true, iban: "CH3400762ABC123DEF456", ibanFormatted: "CH34 0076 2ABC 123D EF45 6" }],
56+
["CH93 0076 2011 6238 5295 7", { isValid: true, iban: "CH9300762011623852957", ibanFormatted: "CH93 0076 2011 6238 5295 7" }],
57+
["CH34 0076 2ABC 123D EF45 6", { isValid: true, iban: "CH3400762ABC123DEF456", ibanFormatted: "CH34 0076 2ABC 123D EF45 6" }],
58+
])("check if the Swiss IBAN number gets parsed correctly", (unformattedSwissIbanNumber, expected) => {
59+
expect(tryParseSwissIbanNumber(unformattedSwissIbanNumber)).toEqual(expected);
4460
});
4561
});

src/lib/swissStandards.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,26 @@ export function isValidSwissSocialInsuranceNumber(socialInsuranceNumber: string)
107107
*/
108108
return checksum === checknumber;
109109
}
110+
111+
/**
112+
* Attempts to parse and validate a Swiss IBAN.
113+
* @param unformattedIbanNumber - The unformatted IBAN as a string
114+
* @returns The result object with the following properties:
115+
* @property {boolean} isValid - Indicates whether the IBAN is valid or not
116+
* @property {string} iban - The cleaned IBAN, only present if valid
117+
* @property {string} ibanFormatted - The formatted IBAN, only present if valid
118+
*/
119+
export function tryParseSwissIbanNumber(unformattedIbanNumber?: string) {
120+
if (isNullOrWhitespace(unformattedIbanNumber)) {
121+
return { isValid: false };
122+
}
123+
124+
const iban = unformattedIbanNumber!.replaceAll(/[^A-Z0-9]/gi, "").toUpperCase();
125+
const isValid = isValidSwissIbanNumber(iban);
126+
127+
return {
128+
isValid: isValid,
129+
iban: isValid ? iban : undefined,
130+
ibanFormatted: isValid ? iban.match(/.{1,4}/g)?.join(" ") : undefined,
131+
};
132+
}

0 commit comments

Comments
 (0)