Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 6 additions & 5 deletions src/Data/FormURLEncoded.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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