mikaello / norwegian-national-id-validator

Validate Norwegian national identity numbers
https://mikaello.github.io/norwegian-national-id-validator
MIT License
24 stars 7 forks source link

Guard against potentially unsafe property get #45

Open kbrabrand opened 2 years ago

kbrabrand commented 2 years ago

The functions all require the param to be a string, but in real life people will probably pass inn undefined/null in error or edge cases. The lib should return a sane value without causing a runtime error.

This change adds optional chaining to potentially unsafe property gets (.length and .trim) on the elevenDigits value

mikaello commented 2 years ago

Hm, this feels like re-examining the type system, these kind of type errors should be handled by the type checker, IMHO.

kbrabrand commented 2 years ago

I beg to differ. When the consumer project is set up with TS/Flow the assumptions about the type system sorting this out holds water, but in cases where the project using this package is not typed, passing a bad value will end in a runtime error.

As a consumer it feels a bit unnecessary that a function that returns undefined for a string that is not 11 digits explode when a bad value is passed, instead of just returning undefined.