-
Notifications
You must be signed in to change notification settings - Fork 8
Add decode function. #12
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
Conversation
decodePart = String.split (Pattern "=") >>> case _ of | ||
[k, v] -> Just $ Tuple (unsafeDecodeURIComponent k) (Just $ unsafeDecodeURIComponent v) | ||
[k] -> Just $ Tuple (unsafeDecodeURIComponent k) Nothing | ||
_ -> Nothing |
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'm not particularly familiar with decodeURIComponent
, but is it possible that a key or value could cause an exception? For example, the MDN documentation provides this pair of examples, the first of which ought to succeed and the second of which ought to fail:
decodeURIComponent('JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B');
// "JavaScript_шеллы"
try {
var a = decodeURIComponent('%E0%A4%A');
} catch(e) {
console.error(e);
}
// URIError: malformed URI sequence
neither of which contains the explicit pattern =
or &
, and I'm not sure which part of it causes the exception thrown in the second case.
Do you mind clarifying how the decode
function avoids exceptions, and handles the same cases that the JavaScript decodeURIComponent
function does? (Feel free to correct me if this question doesn't seem pertinent).
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.
Ideally the decode
function in this library would:
- Successfully decode the same cases that the
decodeURIComponent()
function does - Return
Nothing
for cases that would cause an exception indecodeURIComponent()
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.
Now that there are safe versions in -globals
, can we update this @nsaunders?
Thanks for the response @thomashoneyman. I should have given more consideration to the possibility of I think the main issue is that the type of Another option would be to resort to the FFI as in purescript-httpure, but, actually, is this modeled correctly? I thought that throwing an error is considered a side effect. (As a relative PureScript newbie, I would love clarification one way or the other!) Assuming that throwing an error is a side effect, I think this would ultimately need to be reflected in the return types of Yet another possibility would be to attempt to validate the input before calling Please let me know what you think, or if you had another solution in mind. Maybe @garyb would be willing to opine as well, since I believe he has worked on purescript-uri and can see that he is keeping an eye on this issue. Otherwise, thanks again for the feedback! |
I agree that ideally there should be non-unsafe version of And yeah, an exception/ |
Thanks for the comprehensive response! @garyb made some good points; here are a few thoughts on my end:
This sounds like a problem to me. If encoding and decoding can fail then their types should reflect that -- likely through a
@garyb noted that there isn't error handling because the author(s) felt confident that they were providing input which would always succeed. So that's a sensible use of the unsafe functions. But it doesn't mean other code should do the same thing.
The use of I think you've raised a good point -- if these are defined in the purescript-globals package, why not update that package's main namespace ( @garyb I don't see a reason why
Generally that's true -- throwing an error is a side effect -- but a |
My vote would be:
|
Updated test cases:
|
@garyb this is going to be a breaking change because of the type signature difference. Do you have an opinion on adding EDIT: Whoops, meant to write this on #13. |
@nsaunders actually, do you mind adding a https://github.com/purescript-contrib/purescript-js-date/blob/master/test/Test/Main.purs where each of your lines above would be an |
@thomashoneyman yeah, I'd release it with a new breaking version. The old one is "bad" since it can explode, so it'd be better if people updated to the new one and dealt with the fact that encoding can fail 🙂 👍 for adding some tests that run in CI |
I've added an issue, #14, for the test cases. I've also updated master to include a test directory and for CI to run those tests. |
What does this pull request do?
The purpose of this pull request is to extend the library with a function that decodes form data into the
FormURLEncoded
structure.Where should the reviewer start?
It's a pretty small change.
How should this be manually tested?
Here are my test cases:
Other Notes:
I already updated the README, and I don't think any follow-up steps are necessary other than cutting a release.