diff options
| author | pridon <[email protected]> | 2022-06-23 23:29:34 +0400 |
|---|---|---|
| committer | pridon <[email protected]> | 2022-06-23 23:29:34 +0400 |
| commit | 001a25e6761f832dfc6241b7d97f54210ab39381 (patch) | |
| tree | 340c8cf4993332332eb9f9534466d48af55427ef | |
| parent | eca87f071fc1f4289ef32ba0e5e5aa03a7982902 (diff) | |
| download | countryfetch-001a25e6761f832dfc6241b7d97f54210ab39381.tar.xz countryfetch-001a25e6761f832dfc6241b7d97f54210ab39381.zip | |
fix country search prioritizing fuzzy match instead of exact match
| -rw-r--r-- | src/countries.ts | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/countries.ts b/src/countries.ts index 432fcd1..8b81972 100644 --- a/src/countries.ts +++ b/src/countries.ts @@ -48,15 +48,25 @@ export class Countries { public find(name: string) { name = name.toLowerCase(); - // Find exact match first, then fall back to fuzzy match - const country = this.list.find((c) => { + + // Find exact match first + let country = this.list.find((c) => { const countryName = c.name.common.toLowerCase(); - return countryName === name || countryName.includes(name); + return countryName === name; }); + // Find fuzzy match if exact was not found + if (!country) { + country = this.list.find((c) => { + const countryName = c.name.common.toLowerCase(); + return countryName.includes(name); + }); + } + if (!country) { throw `Cannot find country named ${name}`; } + return country; } |
