-
Notifications
You must be signed in to change notification settings - Fork 7
Implement encoding and decoding functions #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b443d4
Initial implementation of encoders and decoders
thomashoneyman 2160b4d
Add documentation comments
thomashoneyman 8d1083b
Use strict in FFI
thomashoneyman 0904091
Move to FFI-only implementation and drop strings dependency.
thomashoneyman 11a541d
Fix FFI definitions
thomashoneyman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { name = "js-uri" | ||
| , dependencies = [ "console", "effect", "psci-support" ] | ||
| , dependencies = [ "assert", "effect", "functions", "maybe", "psci-support" ] | ||
| , packages = ./packages.dhall | ||
| , sources = [ "src/**/*.purs", "test/**/*.purs" ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| "use strict"; | ||
|
|
||
| // A helper which transforms the result ofencodeURIComponent to be compliant | ||
| // with RFC3896, as described in the MDN documentation here: | ||
| // | ||
| // https://web.archive.org/web/20201206001047/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent | ||
| function toRFC3896(input) { | ||
| return input.replace(/[!'()*]/g, function (c) { | ||
| return "%" + c.charCodeAt(0).toString(16); | ||
| }); | ||
| } | ||
|
|
||
| exports._encodeURIComponent = function encode(fail, succeed, input) { | ||
| try { | ||
| return succeed(toRFC3896(encodeURIComponent(input))); | ||
| } catch (err) { | ||
| return fail(err); | ||
| } | ||
| }; | ||
|
|
||
| exports._encodeFormURLComponent = function encode(fail, succeed, input) { | ||
| try { | ||
| return succeed(toRFC3896(encodeURIComponent(input)).replace(/%20/g, "+")); | ||
| } catch (err) { | ||
| return fail(err); | ||
| } | ||
| }; | ||
|
|
||
| function _decodeURIComponent(fail, succeed, input) { | ||
| try { | ||
| return succeed(decodeURIComponent(input)); | ||
| } catch (err) { | ||
| return fail(err); | ||
| } | ||
| } | ||
|
|
||
| exports._decodeURIComponent = _decodeURIComponent; | ||
|
|
||
| exports._decodeFormURLComponent = function encode(fail, succeed, input) { | ||
| return _decodeURIComponent(fail, succeed, input.replace(/\+/g, " ")); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| module JSURI | ||
| ( encodeURIComponent | ||
| , encodeFormURLComponent | ||
| , decodeURIComponent | ||
| , decodeFormURLComponent | ||
| ) where | ||
|
|
||
| import Prelude | ||
|
|
||
| import Data.Function.Uncurried (Fn3, runFn3) | ||
| import Data.Maybe (Maybe(..)) | ||
|
|
||
| foreign import _encodeURIComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) | ||
|
|
||
| -- | URI-encode a string according to RFC3896. Implemented using JavaScript's | ||
| -- | `encodeURIComponent`. | ||
| -- | | ||
| -- | ```purs | ||
| -- | > encodeURIComponent "https://purescript.org" | ||
| -- | Just "https%3A%2F%2Fpurescript.org" | ||
| -- | ``` | ||
| -- | | ||
| -- | Encoding a URI can fail with a `URIError` if the string contains malformed | ||
| -- | characters. If you are confident you are encoding a well-formed string then | ||
| -- | you can run this function unsafely: | ||
| -- | | ||
| -- | ```purs | ||
| -- | import Partial.Unsafe (unsafePartial) | ||
| -- | import Data.Maybe (fromJust) | ||
| -- | | ||
| -- | unsafeEncode :: String -> String | ||
| -- | unsafeEncode str = unsafePartial $ fromJust $ encodeURIComponent str | ||
| -- | ``` | ||
| encodeURIComponent :: String -> Maybe String | ||
| encodeURIComponent = runFn3 _encodeURIComponent (const Nothing) Just | ||
|
|
||
| foreign import _encodeFormURLComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) | ||
|
|
||
| -- | URI-encode a string according to RFC3896, except with spaces encoded using | ||
| -- | '+' instead of '%20' to comply with application/x-www-form-urlencoded. | ||
| -- | | ||
| -- | ```purs | ||
| -- | > encodeURIComponent "abc ABC" | ||
| -- | Just "abc%20ABC" | ||
| -- | | ||
| -- | > encodeFormURLComponent "abc ABC" | ||
| -- | Just "abc+ABC" | ||
| -- | ``` | ||
| encodeFormURLComponent :: String -> Maybe String | ||
| encodeFormURLComponent = runFn3 _encodeFormURLComponent (const Nothing) Just | ||
|
|
||
| foreign import _decodeURIComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) | ||
|
|
||
| -- | Decode a URI string according to RFC3896. Implemented using JavaScript's | ||
| -- | `decodeURIComponent`. | ||
| -- | | ||
| -- | ```purs | ||
| -- | > decodeURIComponent "https%3A%2F%2Fpurescript.org" | ||
| -- | Just "https://purescript.org" | ||
| -- | ``` | ||
| -- | | ||
| -- | Decoding a URI can fail with a `URIError` if the string contains malformed | ||
| -- | characters. If you are confident you are encoding a well-formed string then | ||
| -- | you can run this function unsafely: | ||
| -- | | ||
| -- | ```purs | ||
| -- | import Partial.Unsafe (unsafePartial) | ||
| -- | import Data.Maybe (fromJust) | ||
| -- | | ||
| -- | unsafeDecode :: String -> String | ||
| -- | unsafeDecode str = unsafePartial $ fromJust $ decodeURIComponent str | ||
| -- | ``` | ||
| decodeURIComponent :: String -> Maybe String | ||
| decodeURIComponent = runFn3 _decodeURIComponent (const Nothing) Just | ||
|
|
||
| foreign import _decodeFormURLComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) | ||
|
|
||
| -- | Decode a URI according to application/x-www-form-urlencoded (for example, | ||
| -- | a string containing '+' for spaces or query parameters). | ||
| -- | | ||
| -- | ```purs | ||
| -- | > decodeURIComponent "https%3A%2F%2Fpurescript.org?search+query" | ||
| -- | Just "https://purescript.org?search+query" | ||
| -- | | ||
| -- | > decodeFormURLComponent "https%3A%2F%2Fpurescript.org?search+query" | ||
| -- | Just "https://purescript.org?search query" | ||
| -- | ``` | ||
| decodeFormURLComponent :: String -> Maybe String | ||
| decodeFormURLComponent = runFn3 _decodeFormURLComponent (const Nothing) Just | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.