-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests, put IBAN in a separate class.
- Loading branch information
1 parent
643605a
commit 810a1f2
Showing
6 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export abstract class IBAN{ | ||
|
||
static checkIBAN(iban: string): boolean { | ||
// Tested with: | ||
// GB33BUKB20201555555555 (correct) | ||
// GB94BARC10201530093459 (correct) | ||
// gB94BARC10201530093459 (correct, evaluates to its uppercase equivalent) | ||
// GB94BARC20201530093459 (incorrect checksum) | ||
iban = iban.toUpperCase(); | ||
const ibanRegex = /^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}$/; | ||
if (!ibanRegex.test(iban)) { | ||
return false; | ||
} | ||
|
||
// ISO/IEC 7064:2003 | ||
// Convert IBAN to numeric representation (rearrange and replace letter with corresponding numeric value) | ||
const numericIban = (iban.slice(4) + iban.slice(0,4)) | ||
.replace(/[A-Z]/g, char => (parseInt(char, 36)).toString()); | ||
// Match only sequences of 1-7 digits, convert each to number and add, then take result mod 97. | ||
const remainder = numericIban | ||
.match(/\d{1,7}/g)?.reduce((acc, block) => Number(acc + block) % 97, 0); | ||
return remainder===1; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { mount } from '@vue/test-utils'; | ||
import { checkIBAN } from '../views/HomeView.vue'; | ||
import {IBAN} from "../scripts/iban"; | ||
|
||
|
||
describe('checkIBAN', () => { | ||
it('should return true for a valid IBAN', () => { | ||
const validIBAN = 'DE89370400440532013000'; | ||
expect(IBAN.checkIBAN(validIBAN)).toBe(true); | ||
}); | ||
|
||
it('should return false for an invalid IBAN', () => { | ||
const invalidIBAN = 'DE89370400440532013001'; | ||
expect(IBAN.checkIBAN(invalidIBAN)).toBe(false); | ||
}); | ||
|
||
it('should return false for an IBAN with incorrect length', () => { | ||
const shortIBAN = 'DE8937040044053201300'; | ||
expect(IBAN.checkIBAN(shortIBAN)).toBe(false); | ||
}); | ||
|
||
it('should return false for an IBAN with invalid characters', () => { | ||
const invalidCharIBAN = 'DE89$70400440532013000'; | ||
expect(IBAN.checkIBAN(invalidCharIBAN)).toBe(false); | ||
}); | ||
|
||
it('should return false for an empty IBAN', () => { | ||
const emptyIBAN = ''; | ||
expect(IBAN.checkIBAN(emptyIBAN)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
import vue from "@vitejs/plugin-vue"; | ||
import viteTsconfigPaths from 'vite-tsconfig-paths'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, | ||
environment: 'jsdom', | ||
coverage: { | ||
reporter: ['text', 'json', 'html'], | ||
}, | ||
include: ["src/tests/*.test.js"], | ||
}, | ||
plugins: [vue(),viteTsconfigPaths()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters