Skip to content

Introduce purs-tidy formatter #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2021
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
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:

- name: Set up a PureScript toolchain
uses: purescript-contrib/setup-purescript@main
with:
purs-tidy: "latest"

- name: Cache PureScript dependencies
uses: actions/cache@v2
Expand All @@ -31,3 +33,6 @@ jobs:

- name: Run tests
run: spago test --no-install

- name: Check formatting
run: purs-tidy check src test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
!.gitignore
!.github
!.editorconfig
!.tidyrc.json

output
generated-docs
Expand Down
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "source",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "never",
"width": null
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
Bugfixes:

Other improvements:
- Added `purs-tidy` formatter (#26 by @thomashoneyman)

## [v6.0.2](https://github.com/purescript-contrib/purescript-form-urlencoded/releases/tag/v6.0.2) - 2021-06-16

Expand Down
14 changes: 7 additions & 7 deletions src/Data/FormURLEncoded.purs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ instance showFormUrlEncoded :: Show FormURLEncoded where
encode :: FormURLEncoded -> Maybe String
encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
where
encodePart = case _ of
Tuple k Nothing -> encodeFormURLComponent k
Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeFormURLComponent k <*> encodeFormURLComponent v
encodePart = case _ of
Tuple k Nothing -> encodeFormURLComponent k
Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeFormURLComponent k <*> encodeFormURLComponent v

-- | Decode `FormURLEncoded` from `application/x-www-form-urlencoded`.
decode :: String -> Maybe FormURLEncoded
decode = map FormURLEncoded <<< traverse decodePart <<< String.split (Pattern "&")
where
decodePart = String.split (Pattern "=") >>> case _ of
[k, v] -> (\key val -> Tuple key $ Just val) <$> decodeFormURLComponent k <*> decodeFormURLComponent v
[k] -> Tuple <$> decodeFormURLComponent k <*> pure Nothing
_ -> Nothing
decodePart = String.split (Pattern "=") >>> case _ of
[ k, v ] -> (\key val -> Tuple key $ Just val) <$> decodeFormURLComponent k <*> decodeFormURLComponent v
[ k ] -> Tuple <$> decodeFormURLComponent k <*> pure Nothing
_ -> Nothing
45 changes: 23 additions & 22 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ main = do

testDecode "this=this%3Dthis" (Just $ FormURLEncoded [ Tuple "this" $ Just "this=this" ])

testDecode "&x=x&&y=y&z=" $
Just $ FormURLEncoded
[ Tuple "" Nothing
, Tuple "x" $ Just "x"
, Tuple "" Nothing
, Tuple "y" $ Just "y"
, Tuple "z" $ Just ""
]
testDecode "&x=x&&y=y&z="
$ Just
$ FormURLEncoded
[ Tuple "" Nothing
, Tuple "x" $ Just "x"
, Tuple "" Nothing
, Tuple "y" $ Just "y"
, Tuple "z" $ Just ""
]

testDecode "a=b&%8A=c" Nothing

Expand All @@ -34,23 +35,23 @@ main = do
testEncode (FormURLEncoded [ Tuple "this" $ Just "this=this" ]) $ Just "this=this%3Dthis"

testEncode
(FormURLEncoded
[ Tuple "" Nothing
, Tuple "x" $ Just "x"
, Tuple "" Nothing
, Tuple "y" $ Just "y"
, Tuple "z" $ Just ""
])
( FormURLEncoded
[ Tuple "" Nothing
, Tuple "x" $ Just "x"
, Tuple "" Nothing
, Tuple "y" $ Just "y"
, Tuple "z" $ Just ""
]
)
(Just "&x=x&&y=y&z=")

testEncode (FormURLEncoded [ Tuple "a b" $ Just "aa bb" ]) $ Just "a+b=aa+bb"

where
testDecode :: String -> Maybe FormURLEncoded -> Effect Unit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the blank line between the where and this line could be removed.

testDecode input expected =
(log $ "decode \"" <> input <> "\" == " <> show expected) *> assert (decode input == expected)

testDecode :: String -> Maybe FormURLEncoded -> Effect Unit
testDecode input expected =
(log $ "decode \"" <> input <> "\" == " <> show expected) *> assert (decode input == expected)

testEncode :: FormURLEncoded -> Maybe String -> Effect Unit
testEncode input expected =
(log $ "encode " <> show input <> " == \"" <> show expected <> "\"") *> assert (encode input == expected)
testEncode :: FormURLEncoded -> Maybe String -> Effect Unit
testEncode input expected =
(log $ "encode " <> show input <> " == \"" <> show expected <> "\"") *> assert (encode input == expected)