-
Notifications
You must be signed in to change notification settings - Fork 28
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
feat!: hex string length support #213
Conversation
|
||
return true | ||
throw e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this a bit strange that a type guard function may throw an exception. Can you describe what is the rationale here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, generally speaking, you always want to catch only those errors that you know you can handle, which in this case is TypeError
coming from makeHexString
as it signals non valid hex string. Because if you will just "catch all" you can catch an error that is unrelated and the problem causing it might be silenced and then very hard to find.
If I would want to be more thorough I would actually check that it is coming from makeHexString
(for example with error codes) as there might be more points on the way which could throw TypeError
, but I found that overkill.
I agree that it might not be that much necessary in this case, but I find it generally a best practice to follow this style to prevent possible future problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general this is a good practice, I agree.
However my assumption would be that a type guard is a pure function that returns true or false and it's written in a way that it can positively decide if the input is of a certain type. Every other cases would be false (including potential errors) because we already checked the positive cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIce work!
I made some comments which is related to ticket #143 where type problems has been stated and as I see this PR also brings in this flaw. We must work on that refactoring after this PR merge.
Co-authored-by: nugaon <50576770+nugaon@users.noreply.github.com>
Supersedes #201 PR.
Breaking changes:
HexString
type is considered as non-prefixed hex string and all functions using it are expecting to have it like that. If used on public API, then it should be sanitized withmakeHexString
functionstripHexPrefix
in favor ofmakeHexString
withPrefix: boolean
flag as they always output non-prefixed hex and instead takelen?: number
to assert expected length of the non-prefixed hex stringTopic
type was changed fromBytes
intoHexString