File tree 2 files changed +16
-2
lines changed
2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change 9
9
A ` FormURLEncoded ` datatype represents an ordered list of key-value pairs
10
10
with possible duplicates. ` encode ` function allows to transform ` FormURLEncoded `
11
11
into a ` Maybe String ` according to ` application/x-www-form-urlencoded ` .
12
+ ` decode ` function transforms a string into a ` Maybe FormURLEncoded ` structure.
12
13
13
14
Documentation is available on [ Pursuit] [ Pursuit ] .
14
15
@@ -25,6 +26,9 @@ Example:
25
26
> , Tuple " Oh" (Just " Let's go!" )
26
27
> ])
27
28
Just " hey&Oh=Let's%20go!"
29
+
30
+ > decode " a=aa&b=bb"
31
+ Just (FormURLEncoded [(Tuple " a" (Just " aa" )),(Tuple " b" (Just " bb" ))])
28
32
```
29
33
30
34
## Contributing
Original file line number Diff line number Diff line change @@ -4,10 +4,11 @@ import Prelude
4
4
5
5
import Data.Maybe (Maybe (..))
6
6
import 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 (..))
8
9
import Data.Traversable (traverse )
9
10
import Data.Tuple (Tuple (..))
10
- import Global (encodeURIComponent )
11
+ import Global (decodeURIComponent , encodeURIComponent )
11
12
12
13
-- | `FormURLEncoded` is an ordered list of key-value pairs with possible duplicates.
13
14
newtype FormURLEncoded = FormURLEncoded (Array (Tuple String (Maybe String )))
@@ -36,3 +37,12 @@ encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
36
37
encodePart = case _ of
37
38
Tuple k Nothing -> encodeURIComponent k
38
39
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