ibantools-germany
Version:
IBAN Validator and Generator for German Bank Accounts
104 lines (101 loc) • 3.7 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.methodForBLZ = exports.checkDigitData = exports.combineCurrentNext = exports.dateObject = void 0;
const current_json_1 = __importDefault(require("../data/current.json"));
const next_json_1 = __importDefault(require("../data/next.json"));
/**
* Returns date object
*
* @param date Date from string or current date if undefined
* @returns
*/
const dateObject = (date) => {
if (date === undefined) {
return new Date();
}
if (typeof date === "string") {
return new Date(date);
}
return date;
};
exports.dateObject = dateObject;
/**
* Combines current data by adding or removing from data provided in next
* @param current
* @param nextAdd
* @param nextRemove
*/
const combineCurrentNext = (current, nextAdd, nextRemove) => {
const combinedData = {};
const currentMethods = Object.keys(current);
for (const currentMethod of currentMethods) {
if (!nextAdd[currentMethod] && !nextRemove[currentMethod]) {
combinedData[currentMethod] = current[currentMethod];
continue;
}
let methodBLZs = current[currentMethod];
if (nextRemove[currentMethod]) {
methodBLZs = methodBLZs.filter((methodBLZ) => !nextRemove[currentMethod].includes(methodBLZ));
}
if (nextAdd[currentMethod]) {
methodBLZs = [...methodBLZs, ...nextAdd[currentMethod]];
}
if (methodBLZs.length > 0) {
combinedData[currentMethod] = methodBLZs;
}
}
return combinedData;
};
exports.combineCurrentNext = combineCurrentNext;
/**
* Get data, either current or combined with next by comparing it to valid-to
* date from next data
* @param date
* @returns
*/
const checkDigitData = (date) => {
const nextValidFrom = new Date(next_json_1.default.valid);
const currentDate = (0, exports.dateObject)(date);
if (currentDate >= nextValidFrom) {
return (0, exports.combineCurrentNext)(current_json_1.default, next_json_1.default.add, next_json_1.default.remove);
}
return current_json_1.default;
};
exports.checkDigitData = checkDigitData;
/**
* Get the check digit method assigned for a BLZ
*
* @param blz German BLZ with 8 digits
* @param date Method valid at this date (default: current date)
* @returns Check digit method or null if invalid
*/
const methodForBLZ = (blz, date) => {
var _a;
if (!blz.match(/^[1-9]\d{7}$/)) {
return null;
}
const numbericBlz = Number(blz);
const data = (0, exports.checkDigitData)(date);
return ((_a = Object.keys(data).find((method) => {
return data[method].includes(numbericBlz);
})) !== null && _a !== void 0 ? _a : null);
};
exports.methodForBLZ = methodForBLZ;