aboutsummaryrefslogtreecommitdiff
path: root/src/countries.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/countries.ts')
-rw-r--r--src/countries.ts52
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");