fission-codes / webnative-elm / Webnative.Path

Paths


type Path t

Path type.

This is used with the phantom 👻 types.

directoryPath : Path Directory

filePath : Path File

encapsulatedPath : Path Encapsulated


type Directory

👻 Directory


type File

👻 File


type Encapsulated

👻 Encapsulated


type Kind
    = Directory
    | File

Creation

directory : List String -> Path Directory

Create a directory path.

directory [ "Audio", "Playlists" ]

file : List String -> Path File

Create a file path.

file [ "Document", "invoice.pdf" ]

root : Path Directory

Root directory.

POSIX

fromPosix : String -> Path Encapsulated

Convert a POSIX formatted string to a path.

This will return a Encapsulated path. To get a path of the type Path Directory or Path File, use the functions in the Webnative.Path.Encapsulated module.

>>> import Webnative.Path.Encapsulated

>>> "foo/bar/"
..>   |> fromPosix
..>   |> Webnative.Path.Encapsulated.toDirectory
Just (directory [ "foo", "bar" ])

>>> "foo/bar"
..>   |> fromPosix
..>   |> Webnative.Path.Encapsulated.toFile
Just (file [ "foo", "bar" ])

toPosix : Path t -> String

Convert a path to the POSIX format.

>>> toPosix (directory [ "foo", "bar"])
"foo/bar/"

>>> toPosix (file [ "foo", "bar"])
"foo/bar"

Encapsulation

encapsulate : Path t -> Path Encapsulated

Encapsulate a path.

Functions

kind : Path t -> Kind

Get the path kind.

>>> kind (directory [])
Directory

>>> kind (file [])
File

Even if a path is encapsulated,
you can still check the kind of path it is.

>>> kind (encapsulate <| directory [])
Directory

>>> kind (encapsulate <| file [])
File

map : (List String -> List String) -> Path t -> Path t

Map.

unwrap : Path t -> List String

Get the path parts.

>>> unwrap (directory [ "foo", "bar" ])
[ "foo", "bar" ]

>>> unwrap (file [ "foo", "bar" ])
[ "foo", "bar" ]

Miscellaneous

encode : Path t -> Json.Encode.Value

Encode to JSON.

>>> import Json.Encode

>>> [ "foo" ]
..>   |> directory
..>   |> encode
..>   |> Json.Encode.encode 0
"{\"directory\":[\"foo\"]}"

>>> [ "bar" ]
..>   |> file
..>   |> encode
..>   |> Json.Encode.encode 0
"{\"file\":[\"bar\"]}"