Add Path.fromString.

This commit is contained in:
Dillon Kearns 2021-05-23 10:07:39 -07:00
parent 8a982aa884
commit b147d17a1d
2 changed files with 37 additions and 2 deletions

View File

@ -1,4 +1,7 @@
module Path exposing (Path, join, toAbsolute)
module Path exposing
( Path, join, fromString
, toAbsolute
)
{-| Represents the path portion of a URL (not query parameters, fragment, protocol, port, etc.).
@ -14,7 +17,15 @@ second does not.
|> Path.toAbsolute
--> "/blog/post-1"
@docs Path, join, toAbsolute
## Creating Paths
@docs Path, join, fromString
## Turning Paths to String
@docs toAbsolute
-}
@ -31,6 +42,20 @@ join parts =
|> Path
{-| Create a Path from a path String.
Path.fromString "blog/post-1/"
|> Path.toAbsolute
|> Expect.equal "/blog/post-1"
-}
fromString : String -> Path
fromString path =
path
|> normalize
|> Path
toAbsolute : Path -> String
toAbsolute (Path path) =
"/" ++ path

View File

@ -22,4 +22,14 @@ all =
Path.join [ "a/", "/b/", "/c/d/e/" ]
|> Path.toAbsolute
|> Expect.equal "/a/b/c/d/e"
, test "fromString with trailing and leading" <|
\() ->
Path.fromString "/blog/post-1/"
|> Path.toAbsolute
|> Expect.equal "/blog/post-1"
, test "fromString without trailing and leading" <|
\() ->
Path.fromString "blog/post-1"
|> Path.toAbsolute
|> Expect.equal "/blog/post-1"
]