diff --git a/README.md b/README.md index b441a08..1f7150d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ A `FormURLEncoded` datatype represents an ordered list of key-value pairs with possible duplicates. `encode` function allows to transform `FormURLEncoded` -into a string according to `application/x-www-form-urlencoded`. +into a `Maybe String` according to `application/x-www-form-urlencoded`. +`decode` function transforms a string into a `Maybe FormURLEncoded` structure. Documentation is available on [Pursuit][Pursuit]. @@ -23,7 +24,10 @@ Example: > encode (fromArray [ Tuple "hey" Nothing > , Tuple "Oh" (Just "Let's go!") > ]) -"hey&Oh=Let's%20go!" +Just "hey&Oh=Let's%20go!" + +> decode "a=aa&b=bb" +Just (FormURLEncoded [(Tuple "a" (Just "aa")),(Tuple "b" (Just "bb"))]) ``` ## Contributing diff --git a/bower.json b/bower.json index 65722ca..e042952 100644 --- a/bower.json +++ b/bower.json @@ -14,11 +14,12 @@ "package.json" ], "dependencies": { - "purescript-globals": "^4.0.0", + "purescript-globals": "^4.1.0", "purescript-maybe": "^4.0.0", "purescript-newtype": "^3.0.0", "purescript-prelude": "^4.0.0", "purescript-strings": "^4.0.0", - "purescript-tuples": "^5.0.0" + "purescript-tuples": "^5.0.0", + "purescript-foldable-traversable": "^4.1.1" } } diff --git a/src/Data/FormURLEncoded.purs b/src/Data/FormURLEncoded.purs index eae359e..4b3ea54 100644 --- a/src/Data/FormURLEncoded.purs +++ b/src/Data/FormURLEncoded.purs @@ -4,9 +4,11 @@ import Prelude import Data.Maybe (Maybe(..)) import Data.Newtype (class Newtype) -import Data.String (joinWith) as String +import Data.String (joinWith, split) as String +import Data.String.Pattern (Pattern(..)) +import Data.Traversable (traverse) import Data.Tuple (Tuple(..)) -import Global.Unsafe (unsafeEncodeURIComponent) +import Global (decodeURIComponent, encodeURIComponent) -- | `FormURLEncoded` is an ordered list of key-value pairs with possible duplicates. newtype FormURLEncoded = FormURLEncoded (Array (Tuple String (Maybe String))) @@ -29,9 +31,18 @@ instance showFormUrlEncoded :: Show FormURLEncoded where show (FormURLEncoded kvs) = "(FormURLEncoded " <> show kvs <> ")" -- | Encode `FormURLEncoded` as `application/x-www-form-urlencoded`. -encode :: FormURLEncoded -> String -encode = String.joinWith "&" <<< map encodePart <<< toArray +encode :: FormURLEncoded -> Maybe String +encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray where encodePart = case _ of - Tuple k Nothing -> unsafeEncodeURIComponent k - Tuple k (Just v) -> unsafeEncodeURIComponent k <> "=" <> unsafeEncodeURIComponent v + Tuple k Nothing -> encodeURIComponent k + Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeURIComponent k <*> encodeURIComponent v + +-- | Decode `FormURLEncoded` from `application/x-www-form-urlencoded`. +decode :: String -> Maybe FormURLEncoded +decode = map FormURLEncoded <<< traverse decodePart <<< String.split (Pattern "&") + where + decodePart = String.split (Pattern "=") >>> case _ of + [k, v] -> (\key val -> Tuple key $ Just val) <$> decodeURIComponent k <*> decodeURIComponent v + [k] -> Tuple <$> decodeURIComponent k <*> pure Nothing + _ -> Nothing