UNPKG

bankdata-germany

Version:
157 lines (154 loc) 5.19 kB
"use strict"; /** * bankdata-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.isBICInData = exports.bankDataByBIC = exports.bankDataByIBAN = exports.bankDataByBBAN = exports.bankDataByBLZ = exports.bankDataSet = exports.combineCurrentNext = exports.dateObject = void 0; const current_json_1 = __importDefault(require("../data/current.json")); const next_json_1 = __importDefault(require("../data/next.json")); const extract_1 = require("./extract"); /** * 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 nextUpsert * @param nextRemove */ const combineCurrentNext = (current, nextUpsert, nextRemove) => { const combinedData = Object.assign(Object.assign({}, current), nextUpsert); for (const nextRemoveBLZ of nextRemove) { delete combinedData[nextRemoveBLZ]; } 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 bankDataSet = (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.upsert, next_json_1.default.remove); } return current_json_1.default; }; exports.bankDataSet = bankDataSet; /** * Get name (and BIC if available) for bank with given BLZ * * @param blz German BLZ with 8 digits * @param date Bank data valid at this date (default: current date) * @returns Bank data or null if invalid */ const bankDataByBLZ = (blz, date) => { if (!blz.match(/^[1-9]\d{7}$/)) { return null; } const bankData = (0, exports.bankDataSet)(date)[blz]; if (!bankData) { return null; } return { bankName: bankData[0], bic: bankData[1], blz: blz, }; }; exports.bankDataByBLZ = bankDataByBLZ; /** * Get name (and BIC if available) for bank with given BBAN * * @param bban German BBAN with 18 digits * @param date Bank data valid at this date (default: current date) * @returns Bank data or null if invalid */ const bankDataByBBAN = (bban, date) => { const blz = (0, extract_1.extractBLZFromBBAN)(bban); if (!blz) { return null; } return (0, exports.bankDataByBLZ)(blz, date); }; exports.bankDataByBBAN = bankDataByBBAN; /** * Get name (and BIC if available) for bank with given IBAN * * @param bban German IBAN with 22 digits * @param date Bank data valid at this date (default: current date) * @returns Bank data or null if invalid */ const bankDataByIBAN = (iban, date) => { if (!iban || !iban.match(/^DE\d{20}$/i)) { return null; } return (0, exports.bankDataByBBAN)(iban.slice(4), date); }; exports.bankDataByIBAN = bankDataByIBAN; /** * Get bank data for bank with given BIC * * @param bic BIC to search for * @param date Bank data valid at this date (default: current date) * @returns Bank data or null */ const bankDataByBIC = (bic, date) => { if (!bic || !bic.match(/^[A-Z]{4}DE[A-Z0-9]{2}([A-Z0-9]{3})?$/i)) { return null; } const searchBIC = `${bic.toUpperCase()}${bic.length === 8 ? "XXX" : ""}`; const result = Object.entries((0, exports.bankDataSet)(date)).find(([, bank]) => bank[1] && bank[1] === searchBIC); if (!result) { return null; } return { bankName: result[1][0], bic: result[1][1], blz: result[0], }; }; exports.bankDataByBIC = bankDataByBIC; /** * Search all bank data and check if any contains the BIC * * @param bic BIC to search for * @param date Bank data valid at this date (default: current date) * @returns Whether BIC exists in bank data */ const isBICInData = (bic, date) => { return (0, exports.bankDataByBIC)(bic, date) != null; }; exports.isBICInData = isBICInData;