Skip to content

add Scheme.toString #44

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 1 commit into from
Mar 20, 2018
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
10 changes: 10 additions & 0 deletions src/URI/Scheme.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module URI.Scheme
( Scheme
, fromString
, toString
, unsafeFromString
, parser
, print
Expand Down Expand Up @@ -42,6 +43,15 @@ instance showScheme ∷ Show Scheme where
fromString ∷ String → Maybe Scheme
fromString = map Scheme <<< hush <<< flip runParser (parseScheme <* eof)

-- | Returns the string value for a scheme.
-- |
-- | ``` purescript
-- | toString (unsafeFromString "http") == "http"
-- | toString (unsafeFromString "git+ssh") == "git+ssh"
-- | ```
toString ∷ Scheme → NonEmptyString
toString (Scheme s) = s

-- | Constructs a `Scheme` part unsafely: if the value is not an acceptable
-- | scheme a runtime error will be thrown.
-- |
Expand Down
15 changes: 12 additions & 3 deletions test/URI/Scheme.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ module Test.URI.Scheme where

import Prelude

import Test.Spec (Spec, describe)
import Test.Util (testIso)
import Data.Maybe (Maybe(..))
import Data.String.NonEmpty as NES
import Test.Spec (Spec, describe, it)
import Test.Util (testIso, equal)
import URI.Scheme as Scheme

spec ∷ ∀ eff. Spec eff Unit
spec =
spec = do
describe "Scheme parser/printer" do
testIso Scheme.parser Scheme.print "http:" (Scheme.unsafeFromString "http")
testIso Scheme.parser Scheme.print "git+ssh:" (Scheme.unsafeFromString "git+ssh")
describe "Scheme fromString/toString" do
it "http"
let http = Scheme.unsafeFromString "http"
in equal (Just http) $ Scheme.fromString $ NES.toString $ Scheme.toString http
it "git+ssh"
let git = Scheme.unsafeFromString "git+ssh"
in equal (Just git) $ Scheme.fromString $ NES.toString $ Scheme.toString git