olahol / iso-3166-2.js

Lookup information about ISO-3166-2 subdivisions
MIT License
108 stars 41 forks source link

return falsey on failure #21

Closed Sicilica closed 7 years ago

Sicilica commented 7 years ago

Both subdivision() and country() return an empty object on failure (ie, when they aren't able to find a country / subdivision based on the input).

In normal JavaScript, you could just test any field to know whether your request worked:

let sub = iso3166.subdivision("US-IN");
if (sub.code) {
  // ...
}

In TypeScript, this is nearly impossible to do cleanly (since the compiler doesn't want you touching "code", which may or may not be set), and it might cause problems for other JavaScript variants as well.

It would be much cleaner if these functions returned a falsey value (false, null, undefined, ...) on failure. Testing for safety would then look like this:

let sub = iso3166.subdivision("SE-O");
if (sub) {
  // ...
}

This change would cause existing code to throw TypeError, but be far cleaner in the long run.

olahol commented 7 years ago

Thank you for your suggestion @Sicilica. This is a good API change which is now in version 1.0.0 of this package.