forked from purescript-contrib/purescript-js-uri
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement encoding and decoding functions (#1)
- Loading branch information
1 parent
00e6dfc
commit 5edccd3
Showing
5 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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 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