Skip to content

Commit 14a9ea6

Browse files
authored
Merge pull request #44 from safareli/scheme-tostring
add Scheme.toString
2 parents 488afff + 3ed561b commit 14a9ea6

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/URI/Scheme.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module URI.Scheme
22
( Scheme
33
, fromString
4+
, toString
45
, unsafeFromString
56
, parser
67
, print
@@ -42,6 +43,15 @@ instance showScheme ∷ Show Scheme where
4243
fromString String Maybe Scheme
4344
fromString = map Scheme <<< hush <<< flip runParser (parseScheme <* eof)
4445

46+
-- | Returns the string value for a scheme.
47+
-- |
48+
-- | ``` purescript
49+
-- | toString (unsafeFromString "http") == "http"
50+
-- | toString (unsafeFromString "git+ssh") == "git+ssh"
51+
-- | ```
52+
toString Scheme NonEmptyString
53+
toString (Scheme s) = s
54+
4555
-- | Constructs a `Scheme` part unsafely: if the value is not an acceptable
4656
-- | scheme a runtime error will be thrown.
4757
-- |

test/URI/Scheme.purs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ module Test.URI.Scheme where
22

33
import Prelude
44

5-
import Test.Spec (Spec, describe)
6-
import Test.Util (testIso)
5+
import Data.Maybe (Maybe(..))
6+
import Data.String.NonEmpty as NES
7+
import Test.Spec (Spec, describe, it)
8+
import Test.Util (testIso, equal)
79
import URI.Scheme as Scheme
810

911
spec eff. Spec eff Unit
10-
spec =
12+
spec = do
1113
describe "Scheme parser/printer" do
1214
testIso Scheme.parser Scheme.print "http:" (Scheme.unsafeFromString "http")
1315
testIso Scheme.parser Scheme.print "git+ssh:" (Scheme.unsafeFromString "git+ssh")
16+
describe "Scheme fromString/toString" do
17+
it "http"
18+
let http = Scheme.unsafeFromString "http"
19+
in equal (Just http) $ Scheme.fromString $ NES.toString $ Scheme.toString http
20+
it "git+ssh"
21+
let git = Scheme.unsafeFromString "git+ssh"
22+
in equal (Just git) $ Scheme.fromString $ NES.toString $ Scheme.toString git

0 commit comments

Comments
 (0)