diff --git a/README.md b/README.md index b441a08..bebf28e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ 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`. Documentation is available on [Pursuit][Pursuit]. @@ -23,7 +23,7 @@ Example: > encode (fromArray [ Tuple "hey" Nothing > , Tuple "Oh" (Just "Let's go!") > ]) -"hey&Oh=Let's%20go!" +Just "hey&Oh=Let's%20go!" ``` ## 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..4ed716b 100644 --- a/src/Data/FormURLEncoded.purs +++ b/src/Data/FormURLEncoded.purs @@ -5,8 +5,9 @@ import Prelude import Data.Maybe (Maybe(..)) import Data.Newtype (class Newtype) import Data.String (joinWith) as String +import Data.Traversable (traverse) import Data.Tuple (Tuple(..)) -import Global.Unsafe (unsafeEncodeURIComponent) +import Global (encodeURIComponent) -- | `FormURLEncoded` is an ordered list of key-value pairs with possible duplicates. newtype FormURLEncoded = FormURLEncoded (Array (Tuple String (Maybe String))) @@ -29,9 +30,9 @@ 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