UNPKG

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.42 kB
import { scrapeDownloadUrl } from "../libs/scrape-dowload-url.js"; import { xlsxUnzip } from "../libs/xlsx-unzip.js"; import { xlsxSharedStrings, xlsxWorksheetData, xlsxWorksheetXML } from "../libs/xlsx-worksheet.js"; //#region src/download/be.ts const getCacheKey = (country) => { return `bankcode-bic-${country}`; }; const getDownloadUrl = async (fetchFn = globalThis.fetch) => { return { ...await scrapeDownloadUrl("https://www.nbb.be/en/activities/payments-and-securities/payment-standards/bank-identification-codes", /href="(?<url>.*\/sites\/default\/files\/(?<version>\d{4}-\d{2})\/r_fulllist_of_codes_current_\d\.xlsx)"/, fetchFn), dataFormat: "xlsx", notes: "National Bank of Belgium Bank Identification Codes" }; }; 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 xlsContent = await xlsxUnzip(buffer); return xlsContent; }; const parseCSV = (xlsContent) => { const sharedStrings = xlsxSharedStrings(xlsContent); const worksheetXML = xlsxWorksheetXML(xlsContent, 1); const lines = xlsxWorksheetData(worksheetXML, sharedStrings); while (lines[0][0] !== "T_Identification_Number") lines.shift(); const header = lines[0]; const colMap = /* @__PURE__ */ new Map(); header.forEach((col, idx) => { colMap.set(col.trim(), idx); }); lines.shift(); const BICs = /* @__PURE__ */ new Set(); const removedBICs = new Set([ "VRIJ", "VRIJ-LIBRE", "NAV", "NAP", "NYA", "-" ]); const result = []; for (const line of lines) { const bic = line[colMap.get("Biccode") ?? -1]?.replaceAll(" ", "")?.toUpperCase() ?? ""; if (bic === "") continue; if (removedBICs.has(bic) || BICs.has(bic)) continue; BICs.add(bic); const bankCode = line[colMap.get("T_Identification_Number") ?? -1]?.trim() ?? ""; if (bankCode === "") continue; let name = line[colMap.get("T_Institutions_English") ?? -1]?.trim() ?? ""; if (name === "") name = line[colMap.get("T_Institutions_Dutch") ?? -1]?.trim() ?? ""; if (name === "") name = line[colMap.get("T_Institutions_French") ?? -1]?.trim() ?? ""; const row = Array.from({ length: 10 }).fill(""); row[0] = bankCode; row[1] = bic; row[2] = name; result.push(row); } return result; }; //#endregion export { downloadCSV, getCacheKey, getDownloadUrl, parseCSV };