UNPKG

e118-iin-list

Version:

List of issuer identifier numbers for the international telecommunication charge card (ITU-T E.118)

82 lines (81 loc) 3.02 kB
var _a, _b, _c; import csv from 'csv-parser'; import * as fs from 'fs'; import * as path from 'path'; import prettier from 'prettier'; import { darkIssuersList } from './darkIssuers.js'; const results = []; const target = path.join(process.cwd(), 'src', 'list.ts'); const notBlank = (s) => (s.trim().length > 0 ? s.trim() : undefined); const removeBlanks = (o) => { const result = {}; for (const [k, v] of Object.entries(o)) { if (v !== undefined) { result[k] = v; } } return result; }; await new Promise((resolve) => fs .createReadStream('list.csv') .pipe(csv()) .on('data', (data) => results.push(data)) .on('end', () => resolve())); const issuerData = {}; let currentICCID; for (const [country, company, iccid, contact] of results.map((r) => Object.values(r))) { if (iccid.startsWith('89')) { currentICCID = notBlank(iccid); } if (currentICCID === undefined) continue; const current = (_a = issuerData[currentICCID]) !== null && _a !== void 0 ? _a : {}; issuerData[currentICCID] = { ...current, ...removeBlanks({ country: notBlank(country), company: notBlank((_b = current.company) !== null && _b !== void 0 ? _b : company), }), contact: [...((_c = current.contact) !== null && _c !== void 0 ? _c : []), notBlank(contact)].filter((s) => s !== undefined), }; } const list = Object.entries(issuerData).reduce((list, [iccid, { company, country, contact }]) => { const [, countryCode, issuerIdentifierNumber] = iccid.split(' '); const iin = parseInt(iccid.replace(/ /g, ''), 10); const key = `${countryCode}${issuerIdentifierNumber}`; const emailRegEx = /e-mail ?: ?(.+)/i; const companyURLs = contact.reduce((urls, s) => { const m = emailRegEx.exec(s); if (!m) return urls; return m[1] .replace(/ /g, '') .split(';') .map((email) => email.replace(/^.+@/, 'http://').toLowerCase()) .filter((url, k, urls) => urls.indexOf(url) === k); }, undefined); const cc = parseInt(countryCode, 10); const result = { ...list, [key]: [ iin, issuerIdentifierNumber, cc, [881, 882, 883].includes(cc) ? 'Global' : country, company, companyURLs !== null && companyURLs !== void 0 ? companyURLs : [], ], }; if (cc === 1) { result[`0${key}`] = result[key]; } return result; }, darkIssuersList); fs.writeFileSync(target, await prettier.format([ `/* Auto-generated file. Do not change! */`, `/* Generated: ${new Date().toISOString()} */`, `import type { IssuerList } from './types.js';`, `export const iinRegEx = /^89(${Object.keys(list).join('|')})/;`, `export const e118IINList: IssuerList = ${JSON.stringify(list, null, 2)} as const;`, ].join('\n\n'), { parser: 'typescript' }), 'utf-8'); console.log(`${target} written.`);