gleam/path

Types

Kind

pub type Kind {
  Relative
  Absolute
  Volumerelative
}

Constructors

  • Relative
  • Absolute
  • Volumerelative

Functions

absolute

pub external fn absolute(path: String) -> String

Converts a relative path and returns an absolute path. No attempt is made to create the shortest absolute path, as this can give incorrect results on file systems that allow links.

basename

pub external fn basename(path: String) -> String

Returns the filename portion of a path

Examples

> path.basename("foo")
"foo"
> path.basename("/usr/foo")
"foo"
> path.basename("/")

""

extension

pub external fn extension(path: String) -> String

Returns the extension portion of a path, including the period.

Examples

> path.extension("foo.gleam")

".gleam" > path.extension("beam.src/kalle") ""

join

pub external fn join(path: List(String)) -> String

Joins a list of path parts with directory separators. If one of the path parts includes an absolute path, such as "/xxx", the preceding elements, if any, are removed from the result.

The result is "normalized": Redundant directory separators are removed. In Windows, all directory separators are forward slashes and the drive letter is in lower case.

kind

pub fn kind(path: String) -> Kind

Returns the path type.

Examples

Unix-like operating systems

  path.kind("/")                #=> Absolute
  path.kind("/usr/local/bin")   #=> Absolute
  path.kind("usr/local/bin")    #=> Relative
  path.kind("../usr/local/bin") #=> Relative
  path.kind("~/file")           #=> Relative

Windows

  path.kind("D:/usr/local/bin") #=> Absolute
  path.kind("usr/local/bin")    #=> Relative
  path.kind("D:bar.ex")         #=> Volumerelative
  path.kind("/bar/foo.ex")      #=> Volumerelative

split

pub external fn split(path: String) -> List(String)

Splits the path into a list at the path separator. If an empty string is given, returns an empty list. On Windows, path is split on both "" and "/" separators and the driver letter, if there is one, is always returned in lowercase.

Examples

   > path.split("")
  []
  > path.split("foo")
  ["foo"]
  > path.split("/foo/bar")
  ["/", "foo", "bar"]