diff options
| author | Pridon Tetradze <[email protected]> | 2022-12-20 19:32:07 +0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-20 19:32:07 +0400 |
| commit | 244b46e1cf5b2b4645c4bbf6193f8dbbf7502c2c (patch) | |
| tree | 54426947ca4c6f3bc6d99d03c7c1f85368191a97 /src/countries.ts | |
| parent | cb43f40f8f3c9e495f025359eed6e24a6542f47f (diff) | |
| download | countryfetch-244b46e1cf5b2b4645c4bbf6193f8dbbf7502c2c.tar.xz countryfetch-244b46e1cf5b2b4645c4bbf6193f8dbbf7502c2c.zip | |
linting, type definitions & cleanup (#12)
Diffstat (limited to 'src/countries.ts')
| -rw-r--r-- | src/countries.ts | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/src/countries.ts b/src/countries.ts index e2230dc..27e765e 100644 --- a/src/countries.ts +++ b/src/countries.ts @@ -61,11 +61,7 @@ export class Countries { return this.list; } - getAll() { - return this.list; - } - - public find(name: string) { + public find(name: string): Country { name = name.toLowerCase(); // Find exact match first @@ -89,31 +85,11 @@ export class Countries { return country; } - public findByCapital(capital: string) { - const country = this.list.find((c) => { - const capitalsLowercase = c.capital.map((capital) => - capital.toLowerCase() - ); - return capitalsLowercase.includes(capital); - }); - - if (!country) { - throw `Could not find the country of capital: ${capital}`; - } - - return country; - } - - capitalOf(capital: string) { - const country = this.findByCapital(capital); - this.logger.capitalOf(capital, country.name.common); - } - public filterByRegion(region: Region) { return this.list.filter((country) => country.region === region); } - public async print(name: string, flag?: boolean) { + public print(name: string) { const country = this.find(name); const currencies = this.extractCurrencies(country.currencies); const languages = this.extractLanguages(country.languages); @@ -146,6 +122,30 @@ export class Countries { return this.names[randomNum]; } + public capitalOf(capital: string): void { + if (!capital) { + this.logger.error("Must provide a capital name."); + return; + } + const country = this.findByCapital(capital); + this.logger.capitalOf(capital, country.name.common); + } + + private findByCapital(capital: string): Country { + const country = this.list.find((c) => { + const capitalsLowercase = c.capital.map((capital) => + capital.toLowerCase() + ); + return capitalsLowercase.includes(capital); + }); + + if (!country) { + throw `Could not find the country of capital: ${capital}`; + } + + return country; + } + private shouldSync() { const lastSynced = this.cache.readTxt("last-synced"); const cacheExists = this.cache.exists("countries", ".json"); |
