bankcode-bic
Version:
Convert bank codes from IBAN to BICs, Name of bank. Currently supports only some selected EU countries.
64 lines (62 loc) • 2.12 kB
JavaScript
import { scrapeDownloadUrl } from "../libs/scrape-dowload-url.js";
//#region src/download/de.ts
const getCacheKey = (country) => {
return `bankcode-bic-${country}`;
};
const getDownloadUrl = async (fetchFn = globalThis.fetch) => {
return {
...await scrapeDownloadUrl("https://www.bundesbank.de/de/aufgaben/unbarer-zahlungsverkehr/serviceangebot/bankleitzahlen/download-bankleitzahlen-602592", /href="(?<url>.*\/resource\/blob\/(?<version>\d+)\/.*\/blz-aktuell-csv-data.csv)"/, fetchFn),
dataFormat: "csv",
notes: "Deutsche Bundesbank Bankleitzahlendatei"
};
};
const downloadCSV = async (url, fetchFn = globalThis.fetch) => {
const res = await fetchFn(url);
if (!res.ok) throw new Error(`Failed to fetch CSV from ${url}: ${res.statusText}`);
const buffer = await res.arrayBuffer();
const decoder = new TextDecoder("iso-8859-1");
const cvs = decoder.decode(new Uint8Array(buffer));
return cvs;
};
const parseCSV = (cvs) => {
const lines = cvs.split(/\r?\n/);
const header = lines[0].split(";");
const colMap = /* @__PURE__ */ new Map();
header.forEach((col, idx) => {
colMap.set(col.trim(), idx);
});
lines.shift();
const wantedCols = [
"Bankleitzahl",
"BIC",
"Bezeichnung",
"UNKNOWN",
"PLZ",
"Ort",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN"
];
const BICs = /* @__PURE__ */ new Set();
const result = [];
for (const line_ of lines) {
const line = line_.trim();
if (line === "") continue;
const cols = line.split(";");
for (let i = 0; i < cols.length; i++) {
const val = cols[i];
if (val.startsWith("\"") && val.endsWith("\"")) cols[i] = val.slice(1, -1).replace(/""/g, "\"");
}
if (cols[colMap.get("Änderungskennzeichen") ?? -1] === "D") continue;
if (cols[colMap.get("Merkmal") ?? -1] !== "1") continue;
if (cols[colMap.get("BIC") ?? -1] === "") continue;
if (BICs.has(cols[colMap.get("BIC") ?? -1])) continue;
BICs.add(cols[colMap.get("BIC") ?? -1]);
const row = wantedCols.map((col) => cols[colMap.get(col) ?? -1]?.trim());
result.push(row);
}
return result;
};
//#endregion
export { downloadCSV, getCacheKey, getDownloadUrl, parseCSV };