Closed ghost closed 4 years ago
@nbcnc Sorry, i can't accept this change as it will break all libraries which depend on this library and use the iso2Lookup function.
No problem, I understand. Then I would just recommend updating your README because the instructions indicate the iso2Lookup returns an object with all country data but it appears to only return the dialCode. Let me know if I've misunderstood something.
iso2Lookup is the same data as alLCountries, but as a map, indexed by the iso2 name of the country -
{
...
zw: {
name: "Zimbabwe",
iso2: "zw",
dialCode: "263",
format: "+...-.-......",
hasAreaCodes: false
},
...
}
yeah, the description is not correct. What it's doing is this -
allCountries
is an array. If i want to find a country in allCountries
using it's iso2
, it will be a O(n) operation.
So i maintain another map from iso2 to an array index within allCountries. This allows you to get hold of the country within allCoutries in O(1) time.
E.g. if you want to find the country with iso2
of tg
, you can do it like this
const iso2ToFind = 'tg';
allCoutries.find(country => country.iso2 === iso2ToFind);
But that is a O(n) operation.
Or you can do it like this
const iso2ToFind = 'tg';
const indexForTheCountry = iso2Lookup[iso2ToFind];
allCoutries[indexForTheCountry];
That is O(1).
That whole thing is super confusing as a whole though. And the description in the README does not help. PR on description change is welcome.
Your documentation says iso2Lookup returns an object but I noticed it only returns the dialCode. I modified the code to return the object instead of just the dialCode.