purescript-contrib / purescript-form-urlencoded Goto Github PK
View Code? Open in Web Editor NEWA data type, and encoding / decoding functions for application/x-www-form-urlencoded
License: BSD 3-Clause "New" or "Revised" License
A data type, and encoding / decoding functions for application/x-www-form-urlencoded
License: BSD 3-Clause "New" or "Revised" License
Currently, purescript-form-urlencoded
cannot be found via Pursuit.
Using purs
current master
branch (prints --version
as 0.11.6 dev build
), attempting to pulp build
a project with purescript-form-urlencoded
(got version 3.0.0 from Pursuit) in the deps results in:
* Building project in /home/rox/c/ps/test-pscorefn-src
Compiling Data.FormURLEncoded
Error found:
at bower_components/purescript-form-urlencoded/src/Data/FormURLEncoded.purs line 31, column 8 - line 31, column 64
Cannot derive a type class instance for
Data.Generic.Generic FormURLEncoded
since instances of this type class are not derivable.
The library currently doesn't have tests in the test suite. It does have a Travis file to run tests in continuous integration, and @nsaunders has provided a few test cases:
> decode "a=aa&b=bb"
(Just (FormURLEncoded [(Tuple "a" (Just "aa")),(Tuple "b" (Just "bb"))]))
> decode "this=this%3Dthis"
(Just (FormURLEncoded [(Tuple "this" (Just "this=this"))]))
> decode "&x=x&&y=y&z="
(Just (FormURLEncoded [(Tuple "" Nothing),(Tuple "x" (Just "x")),(Tuple "" Nothing),(Tuple "y" (Just "y")),(Tuple "z" (Just ""))]))
> decode "a=b&%8A=c"
Nothing
> encode =<< decode "a=aa&b=bb"
(Just "a=aa&b=bb")
> encode =<< decode "this=this%3Dthis"
(Just "this=this%3Dthis")
> encode =<< decode "&x=x&&y=y&z="
(Just "&x=x&&y=y&z=")
> encode =<< decode "a=b&%8A=c"
Nothing
These tests should be added to the tests/
directory so they are checked in CI when new pull requests are opened.
The encode
function encodes a space as %20
, not +
as required.
> encode $ fromArray
> [ Tuple "hey" Nothing
> , Tuple "Oh" (Just "Let's go!")
> ]
Just "hey&Oh=Let's%20go!"
Expected result:
Just "hey&Oh=Let's+go!"
https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
application/x-www-form-urlencoded Forms submitted with this content type must be encoded as follows: <...> Space characters are replaced by `+', and then reserved characters are escaped as described in [RFC1738] <...>
Instead of functions encodeURIComponent
and decodeURIComponent
, should use functions encodeFormURLComponent
and decodeFormURLComponent
.
33: -- | Encode `FormURLEncoded` as `application/x-www-form-urlencoded`.
34: encode :: FormURLEncoded -> Maybe String
35: encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
36: where
37: encodePart = case _ of
38: Tuple k Nothing -> encodeURIComponent k
^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.