File tree Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change 99A ` FormURLEncoded ` datatype represents an ordered list of key-value pairs
1010with possible duplicates. ` encode ` function allows to transform ` FormURLEncoded `
1111into a ` Maybe String ` according to ` application/x-www-form-urlencoded ` .
12+ ` decode ` function transforms a string into a ` Maybe FormURLEncoded ` structure.
1213
1314Documentation is available on [ Pursuit] [ Pursuit ] .
1415
@@ -25,6 +26,9 @@ Example:
2526> , Tuple " Oh" (Just " Let's go!" )
2627> ])
2728Just " hey&Oh=Let's%20go!"
29+
30+ > decode " a=aa&b=bb"
31+ Just (FormURLEncoded [(Tuple " a" (Just " aa" )),(Tuple " b" (Just " bb" ))])
2832```
2933
3034## Contributing
Original file line number Diff line number Diff line change @@ -4,10 +4,11 @@ import Prelude
44
55import Data.Maybe (Maybe (..))
66import Data.Newtype (class Newtype )
7- import Data.String (joinWith ) as String
7+ import Data.String (joinWith , split ) as String
8+ import Data.String.Pattern (Pattern (..))
89import Data.Traversable (traverse )
910import Data.Tuple (Tuple (..))
10- import Global (encodeURIComponent )
11+ import Global (decodeURIComponent , encodeURIComponent )
1112
1213-- | `FormURLEncoded` is an ordered list of key-value pairs with possible duplicates.
1314newtype FormURLEncoded = FormURLEncoded (Array (Tuple String (Maybe String )))
@@ -36,3 +37,12 @@ encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
3637 encodePart = case _ of
3738 Tuple k Nothing -> encodeURIComponent k
3839 Tuple k (Just v) -> (\key val -> key <> " =" <> val) <$> encodeURIComponent k <*> encodeURIComponent v
40+
41+ -- | Decode `FormURLEncoded` from `application/x-www-form-urlencoded`.
42+ decode :: String -> Maybe FormURLEncoded
43+ decode = map FormURLEncoded <<< traverse decodePart <<< String .split (Pattern " &" )
44+ where
45+ decodePart = String .split (Pattern " =" ) >>> case _ of
46+ [k, v] -> (\key val -> Tuple key $ Just val) <$> decodeURIComponent k <*> decodeURIComponent v
47+ [k] -> Tuple <$> decodeURIComponent k <*> pure Nothing
48+ _ -> Nothing
You can’t perform that action at this time.
0 commit comments