|
| 1 | +module JSURI |
| 2 | + ( encodeURIComponent |
| 3 | + , encodeFormURLComponent |
| 4 | + , decodeURIComponent |
| 5 | + , decodeFormURLComponent |
| 6 | + ) where |
| 7 | + |
| 8 | +import Prelude |
| 9 | + |
| 10 | +import Data.Function.Uncurried (Fn3, runFn3) |
| 11 | +import Data.Maybe (Maybe(..)) |
| 12 | + |
| 13 | +foreign import _encodeURIComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) |
| 14 | + |
| 15 | +-- | URI-encode a string according to RFC3896. Implemented using JavaScript's |
| 16 | +-- | `encodeURIComponent`. |
| 17 | +-- | |
| 18 | +-- | ```purs |
| 19 | +-- | > encodeURIComponent "https://purescript.org" |
| 20 | +-- | Just "https%3A%2F%2Fpurescript.org" |
| 21 | +-- | ``` |
| 22 | +-- | |
| 23 | +-- | Encoding a URI can fail with a `URIError` if the string contains malformed |
| 24 | +-- | characters. If you are confident you are encoding a well-formed string then |
| 25 | +-- | you can run this function unsafely: |
| 26 | +-- | |
| 27 | +-- | ```purs |
| 28 | +-- | import Partial.Unsafe (unsafePartial) |
| 29 | +-- | import Data.Maybe (fromJust) |
| 30 | +-- | |
| 31 | +-- | unsafeEncode :: String -> String |
| 32 | +-- | unsafeEncode str = unsafePartial $ fromJust $ encodeURIComponent str |
| 33 | +-- | ``` |
| 34 | +encodeURIComponent :: String -> Maybe String |
| 35 | +encodeURIComponent = runFn3 _encodeURIComponent (const Nothing) Just |
| 36 | + |
| 37 | +foreign import _encodeFormURLComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) |
| 38 | + |
| 39 | +-- | URI-encode a string according to RFC3896, except with spaces encoded using |
| 40 | +-- | '+' instead of '%20' to comply with application/x-www-form-urlencoded. |
| 41 | +-- | |
| 42 | +-- | ```purs |
| 43 | +-- | > encodeURIComponent "abc ABC" |
| 44 | +-- | Just "abc%20ABC" |
| 45 | +-- | |
| 46 | +-- | > encodeFormURLComponent "abc ABC" |
| 47 | +-- | Just "abc+ABC" |
| 48 | +-- | ``` |
| 49 | +encodeFormURLComponent :: String -> Maybe String |
| 50 | +encodeFormURLComponent = runFn3 _encodeFormURLComponent (const Nothing) Just |
| 51 | + |
| 52 | +foreign import _decodeURIComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) |
| 53 | + |
| 54 | +-- | Decode a URI string according to RFC3896. Implemented using JavaScript's |
| 55 | +-- | `decodeURIComponent`. |
| 56 | +-- | |
| 57 | +-- | ```purs |
| 58 | +-- | > decodeURIComponent "https%3A%2F%2Fpurescript.org" |
| 59 | +-- | Just "https://purescript.org" |
| 60 | +-- | ``` |
| 61 | +-- | |
| 62 | +-- | Decoding a URI can fail with a `URIError` if the string contains malformed |
| 63 | +-- | characters. If you are confident you are encoding a well-formed string then |
| 64 | +-- | you can run this function unsafely: |
| 65 | +-- | |
| 66 | +-- | ```purs |
| 67 | +-- | import Partial.Unsafe (unsafePartial) |
| 68 | +-- | import Data.Maybe (fromJust) |
| 69 | +-- | |
| 70 | +-- | unsafeDecode :: String -> String |
| 71 | +-- | unsafeDecode str = unsafePartial $ fromJust $ decodeURIComponent str |
| 72 | +-- | ``` |
| 73 | +decodeURIComponent :: String -> Maybe String |
| 74 | +decodeURIComponent = runFn3 _decodeURIComponent (const Nothing) Just |
| 75 | + |
| 76 | +foreign import _decodeFormURLComponent :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String) |
| 77 | + |
| 78 | +-- | Decode a URI according to application/x-www-form-urlencoded (for example, |
| 79 | +-- | a string containing '+' for spaces or query parameters). |
| 80 | +-- | |
| 81 | +-- | ```purs |
| 82 | +-- | > decodeURIComponent "https%3A%2F%2Fpurescript.org?search+query" |
| 83 | +-- | Just "https://purescript.org?search+query" |
| 84 | +-- | |
| 85 | +-- | > decodeFormURLComponent "https%3A%2F%2Fpurescript.org?search+query" |
| 86 | +-- | Just "https://purescript.org?search query" |
| 87 | +-- | ``` |
| 88 | +decodeFormURLComponent :: String -> Maybe String |
| 89 | +decodeFormURLComponent = runFn3 _decodeFormURLComponent (const Nothing) Just |
0 commit comments