ibantools-germany
Version:
IBAN Validator and Generator for German Bank Accounts
69 lines (66 loc) • 2.58 kB
JavaScript
;
/**
* ibantools-germany
* Copyright (C) 2022-2024 Markus Baumer <markus@baumer.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.eserValidate = void 0;
const helper_1 = require("../helper");
const method20_1 = __importDefault(require("./method20"));
const eser8 = (number, blz) => {
if (number.length !== 8) {
return null;
}
const blz58 = blz.slice(4, 8);
const number12 = number.slice(0, 2);
const number3Rest = number.slice(2, 8).replace(/^0+/, "");
return `${blz58}${number12}${number3Rest}`;
};
const eserValidate = (eserAccount) => {
const digits = (0, helper_1.getDigits)(eserAccount);
const givenCheckDigit = digits.slice(5, 6)[0]; // Check digit is last digit
const weigths = [2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4].slice(0, digits.length);
weigths.reverse();
const checkDigitWeight = weigths.splice(5, 1, 0)[0];
const weightedDigits = (0, helper_1.weightDigits)(digits, weigths);
const sum = (0, helper_1.calculateSum)(weightedDigits);
const modulo11 = sum % 11;
for (let i = 0; i < 10; i++) {
const modulo = (modulo11 + i * checkDigitWeight) % 11;
if (modulo === 10) {
if (givenCheckDigit === i) {
return "VALID";
}
break;
}
}
return "INVALID";
};
exports.eserValidate = eserValidate;
exports.default = (number, blz) => {
if (number.length === 10 && number.match(/^9/)) {
return (0, method20_1.default)(number);
}
if (blz.length != 8 || !blz.match(/^\d{3}5/)) {
return "INVALID";
}
const eserAccount = eser8(number, blz);
if (!eserAccount) {
return "INVALID";
}
return (0, exports.eserValidate)(eserAccount);
};