UNPKG

country-currency-phone

Version:

Provides countries, currencies and phone codes base on ISO names, symbols and codes.

147 lines 5.16 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CountryCurrencyPhone = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); class CountryCurrencyPhone { constructor() { this.countryNames = new Map(); this.countryAlpha2s = new Map(); this.countryAlpha3s = new Map(); this.currencyNames = new Map(); this.currencyAlpha3s = new Map(); this.currencySymbols = new Map(); this.phoneCodes = new Map(); this.loadDb(); } getByCountryName(name) { return this.countryNames.get(name); } getByCountryAlpha2(alpha2) { return this.countryAlpha2s.get(alpha2); } getByCountryAlpha3(alpha3) { return this.countryAlpha3s.get(alpha3); } getByCurrencyName(name) { var _a; const items = []; (_a = this.currencyNames.get(name)) === null || _a === void 0 ? void 0 : _a.forEach((item) => { items.push(item); }); return items; } getByCurrencyAlpha3(alpha3) { var _a; const items = []; (_a = this.currencyAlpha3s.get(alpha3)) === null || _a === void 0 ? void 0 : _a.forEach((item) => { items.push(item); }); return items; } getByCurrencySymbol(symbol) { var _a; const items = []; (_a = this.currencySymbols.get(symbol)) === null || _a === void 0 ? void 0 : _a.forEach((item) => { items.push(item); }); return items; } getByPhoneCode(phoneCode) { var _a; const items = []; (_a = this.phoneCodes.get(phoneCode)) === null || _a === void 0 ? void 0 : _a.forEach((item) => { items.push(item); }); return items; } getAll() { const items = []; this.countryAlpha2s.forEach((item) => { items.push(item); }); return items; } addToDB(item) { let existingRecord = this.getByCountryAlpha2(item.country.alpha2); if (existingRecord && existingRecord.country.alpha3 !== item.country.alpha3) { throw new Error('Country alpha2 already exists!'); } existingRecord = this.getByCountryAlpha3(item.country.alpha3); if (existingRecord && existingRecord.country.alpha2 !== item.country.alpha2) { throw new Error('Country alpha3 already exists!'); } for (const name of item.country.names) { this.countryNames.set(name, item); } this.countryAlpha2s.set(item.country.alpha2, item); this.countryAlpha3s.set(item.country.alpha3, item); let itemSet = this.currencyNames.get(item.currency.name); if (itemSet) { itemSet.add(item); } else { itemSet = new Set().add(item); this.currencyNames.set(item.currency.name, itemSet); } itemSet = this.currencyAlpha3s.get(item.currency.alpha3); if (itemSet) { itemSet.add(item); } else { itemSet = new Set().add(item); this.currencyAlpha3s.set(item.currency.alpha3, itemSet); } itemSet = this.currencySymbols.get(item.currency.symbol); if (itemSet) { itemSet.add(item); } else { itemSet = new Set().add(item); this.currencySymbols.set(item.currency.symbol, itemSet); } item.phoneCodes.forEach((phoneCode) => { itemSet = this.phoneCodes.get(phoneCode); if (itemSet) { itemSet.add(item); } else { itemSet = new Set().add(item); this.phoneCodes.set(phoneCode, itemSet); } }); } loadDb() { const db = JSON.parse(fs.readFileSync(path.join(__dirname, './db.json'), 'utf-8')); for (const item of db) { this.addToDB(item); } } } exports.CountryCurrencyPhone = CountryCurrencyPhone; //# sourceMappingURL=country-currency-phone.js.map