aboutsummaryrefslogtreecommitdiff
path: root/src/countries.ts
diff options
context:
space:
mode:
authorFridon <[email protected]>2022-06-22 00:01:18 +0400
committerFridon <[email protected]>2022-06-22 00:01:18 +0400
commitcab126423c647aeac1a2a692b4918772d4e56150 (patch)
treeb56c0a4f08af73f97d52af1b751a6409f20552c3 /src/countries.ts
parent336862f977962d3cbb88e9b7bcf288045c377ac5 (diff)
downloadcountryfetch-cab126423c647aeac1a2a692b4918772d4e56150.tar.xz
countryfetch-cab126423c647aeac1a2a692b4918772d4e56150.zip
add find by capital
Diffstat (limited to 'src/countries.ts')
-rw-r--r--src/countries.ts17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/countries.ts b/src/countries.ts
index dba4d1a..05d6a17 100644
--- a/src/countries.ts
+++ b/src/countries.ts
@@ -47,7 +47,7 @@ export class Countries {
return this.list;
}
- find(name: string): Country {
+ find(name: string) {
name = name.toLowerCase();
// Find exact match first, then fall back to fuzzy match
const country = this.list.find((c) => {
@@ -62,6 +62,21 @@ export class Countries {
return country;
}
+ findByCapital(capital: string) {
+ const country = this.list.find((c) => {
+ const capitalsLowercase = c.capital.map((capital) =>
+ capital.toLowerCase()
+ );
+ return capitalsLowercase.includes(capital);
+ });
+
+ if (!country) {
+ throw Error(`Could not find the country of capital: ${capital}`);
+ }
+
+ return country;
+ }
+
print(name: string) {
const country = this.find(name);
let currencies = [];