Skip to content

Commit

Permalink
Fix problem with friendlyFormatIBAN and invalid argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Simplify committed May 5, 2019
1 parent 82e503b commit 2b5f0cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ChangeLog
@@ -1,8 +1,9 @@
2019-05-05 Saša Jovanić <sasa@simplify.ba>
* Merged PR1 - Renamed `main:jsnext` to `modules` - Thanks @NeoLegends
* Merged PR1 - Renamed `main:jsnext` to `modules` - Thanks @NeoLegends ()PR1/GH9)
* Upraded various packages containing security vulnerabilities using `npm audit fix`
* Upgraded Gulp to version 4 and all gulp tasks
* Added Vatican City State
* `friendlyFormatIBAN` and `electronicFormatIBAN` will return `null` when non-string value is provided (GH15).

2018-03-11 Saša Jovanić <sasa@simplify.ba>
* Released version 2.0.0
Expand Down
7 changes: 4 additions & 3 deletions src/IBANTools.ts
Expand Up @@ -144,6 +144,7 @@ function checkFormatBBAN(bban: string, bformat: string): boolean {
/**
* Get IBAN in electronic format (no spaces)
* IBAN validation is not performed.
* When non-string value for IBAN is provided, returns null.
* @example
* // returns "NL91ABNA0417164300"
* ibantools.electronicFormatIBAN("NL91 ABNA 0417 1643 00");
Expand All @@ -152,15 +153,14 @@ function checkFormatBBAN(bban: string, bformat: string): boolean {
* @return {string} IBAN Electronic formated IBAN
*/
export function electronicFormatIBAN(iban: string) {
if (iban === undefined || iban === null) {
return null;
}
if (typeof iban !== "string") { return null; }
return iban.replace(/[-\ ]/g, "").toUpperCase();
}

/**
* Get IBAN in friendly format (separated after every 4 characters)
* IBAN validation is not performed.
* When non-string value for IBAN is provided, returns null.
* @example
* // returns "NL91 ABNA 0417 1643 00"
* ibantools.friendlyFormatIBAN("NL91ABNA0417164300");
Expand All @@ -173,6 +173,7 @@ export function electronicFormatIBAN(iban: string) {
* @return {string} IBAN Friendly formated IBAN
*/
export function friendlyFormatIBAN(iban: string, separator?: string) {
if (typeof iban !== "string") { return null; }
if (typeof separator === "undefined") { separator = " "; }
return electronicFormatIBAN(iban).replace(/(.{4})(?!$)/g, "$1" + separator);
}
Expand Down
9 changes: 9 additions & 0 deletions test/IBANToolsTest.ts
Expand Up @@ -281,6 +281,15 @@ describe("IBANTools", () => {
});
});

describe("When calling friendlyFormatIBAN() with invalid argument", () => {
it("returns null when undefined is provided", () => {
expect(iban.friendlyFormatIBAN(undefined)).to.be.null;
});
it("returns null when null is provided", () => {
expect(iban.friendlyFormatIBAN(null)).to.be.null;
});
});

describe("When calling getCountrySpecifications()", () => {
const ext = iban.getCountrySpecifications();
it("Country with code NL should return name Netherlands", () => {
Expand Down

0 comments on commit 2b5f0cd

Please sign in to comment.