Skip to content

Guard against potentially unsafe property get #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kbrabrand
Copy link
Contributor

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

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
Copy link
Owner

mikaello commented Feb 7, 2022

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

@kbrabrand
Copy link
Contributor Author

kbrabrand commented Feb 7, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants