Coder Social home page Coder Social logo

purescript-contrib / purescript-form-urlencoded Goto Github PK

View Code? Open in Web Editor NEW
8.0 8.0 7.0 104 KB

A data type, and encoding / decoding functions for application/x-www-form-urlencoded

License: BSD 3-Clause "New" or "Revised" License

PureScript 90.45% Dhall 9.55%

purescript-form-urlencoded's People

Contributors

dederer avatar garyb avatar hdgarrood avatar jordanmartinez avatar justinwoo avatar kl0tl avatar kritzcreek avatar menelaos avatar nsaunders avatar th-awake avatar themoritz avatar thomashoneyman avatar zudov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

purescript-form-urlencoded's Issues

Not compatible yet with purs 0.11.6-dev it seems?

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.

Add test cases

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.

Spaces must be encoded as "+"

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!"

W3C

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] <...>

FormURLEncoded.purs

https://github.com/purescript-contrib/purescript-form-urlencoded/blob/v6.0.1/src/Data/FormURLEncoded.purs

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
                              ^^^^^^^^^^^^^^^^^^

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.