ibantools-germany
Version:
IBAN Validator and Generator for German Bank Accounts
51 lines (50 loc) • 1.77 kB
JavaScript
;
/**
* ibantools-germany
* Copyright (c) 2022-2026 Markus Baumer <markus@baumer.dev>
* SPDX-License-Identifier: MIT OR MPL-2.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateIBAN = exports.generateBBAN = void 0;
const helper_1 = require("./helper");
/**
* Generate and return BBAN from account number and BLZ
*
* There is no check digit validation. If you want to get a valid BBAN you have
* to to pass the result to isValidBBAN() or validate the account number and
* BLZ to isValidAccountNumberBLZ() beforehand.
*
* @param accountNumber Account number with up to 10 digits
* @param blz German BLZ with 8 digits
* @returns BBAN or null if invalid
*/
const generateBBAN = (accountNumber, blz) => {
if (!accountNumber ||
!accountNumber.match(/^\d{1,10}$/) ||
!blz ||
!blz.match(/^[1-9]\d{7}$/)) {
return null;
}
return `${blz}${(0, helper_1.paddedAccountNumber)(accountNumber)}`;
};
exports.generateBBAN = generateBBAN;
/**
* Generate IBAN from BBAN und country
*
* Returns null if IBAN can't be generated, i.e. provided values are faulty
* or BBAN+country is invalid (if param 'validate' is true)
*
* @param accountNumber Account number with up to 10 digits
* @param blz German BLZ with 8 digits
* @returns IBAN or null if invalid
*/
const generateIBAN = (accountNumber, blz) => {
const bban = (0, exports.generateBBAN)(accountNumber, blz);
if (!bban) {
return null;
}
const dataForCalculation = (0, helper_1.lettersToDigits)(`${bban}DE00`);
const checkDigit = 98 - (0, helper_1.modulo97)(dataForCalculation);
return `DE${checkDigit < 10 ? `0${String(checkDigit)}` : checkDigit}${bban}`;
};
exports.generateIBAN = generateIBAN;