Open kbrabrand opened 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.
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
.
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