Skip to content

Commit 0297a0b

Browse files
Merge pull request #13 from nsaunders/encode-safe
Make encode function safe.
2 parents 04ade12 + 8b0b6e7 commit 0297a0b

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A `FormURLEncoded` datatype represents an ordered list of key-value pairs
99
with possible duplicates. `encode` function allows to transform `FormURLEncoded`
10-
into a string according to `application/x-www-form-urlencoded`.
10+
into a `Maybe String` according to `application/x-www-form-urlencoded`.
1111

1212
Documentation is available on [Pursuit][Pursuit].
1313

@@ -23,7 +23,7 @@ Example:
2323
> encode (fromArray [ Tuple "hey" Nothing
2424
> , Tuple "Oh" (Just "Let's go!")
2525
> ])
26-
"hey&Oh=Let's%20go!"
26+
Just "hey&Oh=Let's%20go!"
2727
```
2828

2929
## Contributing

bower.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
"package.json"
1515
],
1616
"dependencies": {
17-
"purescript-globals": "^4.0.0",
17+
"purescript-globals": "^4.1.0",
1818
"purescript-maybe": "^4.0.0",
1919
"purescript-newtype": "^3.0.0",
2020
"purescript-prelude": "^4.0.0",
2121
"purescript-strings": "^4.0.0",
22-
"purescript-tuples": "^5.0.0"
22+
"purescript-tuples": "^5.0.0",
23+
"purescript-foldable-traversable": "^4.1.1"
2324
}
2425
}

src/Data/FormURLEncoded.purs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import Prelude
55
import Data.Maybe (Maybe(..))
66
import Data.Newtype (class Newtype)
77
import Data.String (joinWith) as String
8+
import Data.Traversable (traverse)
89
import Data.Tuple (Tuple(..))
9-
import Global.Unsafe (unsafeEncodeURIComponent)
10+
import Global (encodeURIComponent)
1011

1112
-- | `FormURLEncoded` is an ordered list of key-value pairs with possible duplicates.
1213
newtype FormURLEncoded = FormURLEncoded (Array (Tuple String (Maybe String)))
@@ -29,9 +30,9 @@ instance showFormUrlEncoded :: Show FormURLEncoded where
2930
show (FormURLEncoded kvs) = "(FormURLEncoded " <> show kvs <> ")"
3031

3132
-- | Encode `FormURLEncoded` as `application/x-www-form-urlencoded`.
32-
encode :: FormURLEncoded -> String
33-
encode = String.joinWith "&" <<< map encodePart <<< toArray
33+
encode :: FormURLEncoded -> Maybe String
34+
encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
3435
where
3536
encodePart = case _ of
36-
Tuple k Nothing -> unsafeEncodeURIComponent k
37-
Tuple k (Just v) -> unsafeEncodeURIComponent k <> "=" <> unsafeEncodeURIComponent v
37+
Tuple k Nothing -> encodeURIComponent k
38+
Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeURIComponent k <*> encodeURIComponent v

0 commit comments

Comments
 (0)