mukeshsoni / country-telephone-data

Data related to a country's telephone dial code and number formatting
41 stars 38 forks source link

Fix iso2Lookup #17

Closed ghost closed 4 years ago

ghost commented 4 years ago

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.

mukeshsoni commented 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.

ghost commented 4 years ago

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
    },
    ...
}
mukeshsoni commented 4 years ago

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.