bankcode-bic
Version:
Convert bank codes from IBAN to BICs, Name of bank. Currently supports only some selected EU countries.
63 lines (61 loc) • 2.18 kB
JavaScript
import { scrapeDownloadUrl } from "../libs/scrape-dowload-url.js";
import { ungzip } from "../libs/ungzip.js";
//#region src/download/fr.ts
const getCacheKey = (country) => {
return `bankcode-bic-${country}`;
};
const getDownloadUrl = async (fetchFn = globalThis.fetch) => {
return {
...await scrapeDownloadUrl("https://www.ecb.europa.eu/stats/financial_corporations/list_of_financial_institutions/html/monthly_list-MID.en.html", /href="(?<url>.+\/fi_mrr_csv_(?<version>\d{6})\.csv\.gz)"/, fetchFn),
dataFormat: "csv",
notes: "European Central Bank Financial Institutions"
};
};
const downloadCSV = async (url, fetchFn = globalThis.fetch, ungzipFn = ungzip) => {
const res = await fetchFn(url);
if (!res.ok) throw new Error(`Failed to fetch CSV from ${url}: ${res.statusText}`);
const contentEncoding = res.headers?.get?.("content-encoding");
const contentType = res.headers?.get?.("content-type");
if (contentEncoding === "gzip" || url.endsWith(".gz") || contentType?.includes("gzip")) return await ungzipFn(res);
else return await res.text();
};
const parseCSV = (cvs, countryCode) => {
const separator = " ";
const lines = cvs.split(/\r?\n/);
const header = lines[0].split(separator);
const colMap = /* @__PURE__ */ new Map();
header.forEach((col, idx) => {
colMap.set(col.trim(), idx);
});
lines.shift();
const wantedCols = [
"RIAD_CODE",
"BIC",
"NAME",
"ADDRESS",
"POSTAL",
"CITY",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN",
"UNKNOWN"
];
const BICs = /* @__PURE__ */ new Set();
const filterCountry = countryCode.toUpperCase();
const result = [];
for (const line_ of lines) {
const line = line_.trim();
if (line === "") continue;
const cols = line.split(separator);
if (cols[colMap.get("COUNTRY_OF_REGISTRATION") ?? -1] !== filterCountry) 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());
row[0] = row[0].slice(2);
result.push(row);
}
return result;
};
//#endregion
export { downloadCSV, getCacheKey, getDownloadUrl, parseCSV };