Path

Standard library for working with filesystem paths.

Functions in this module operate on filesystem paths. Paths are separated into components by the Unix directory separator /. On Windows, even though \ is the directory separator, the functions in this module convert \ to / for consistency, and these converted paths can be used by the File module with no issues.

To import all names from this module, use:

import Path (*)

With no import, you can still access anything with the prefix Path., like Path.base.

Index

NameTypeDescription

base(path)

String -> String

Returns the last component of path.

dir(path)

String -> String

Returns all but the last component of path, representing the directory in which path resides.

ext(path)

String -> String

Returns the extension of path, or "" if there is no extension.

strip_ext(path)

String -> String

Returns path with the extension stripped.

normalize(path)

String -> String

Normalizes path by eliminating redundant `.

to_absolute(path)

String -> String

Returns an absolute version of path.

to_relative(path, from)

(String, String) -> String

Returns a relative path from from to path.

absolute?(path)

String -> Bool

Returns true if path is absolute.

relative?(path)

String -> Bool

Returns true if path is relative.

join(path1, path2)

(String, String) -> String

Joins path1 and path2 together with the directory separator.

join_all(paths)

[String] -> String

Joins all paths in paths with the directory separator.

split(path)

String -> [String]

Splits path on the directory separator, returning a list of components.

Functions

base : String -> String
base(path)

Returns the last component of path. If path doesn't have any slashes, returns path.

assert base("/foo/bar/baz.txt") == "baz.txt"
assert base("foo/bar.pdf") == "bar.pdf"
assert base("foo") == "foo"
assert base("/") == ""
dir : String -> String
dir(path)

Returns all but the last component of path, representing the directory in which path resides. The result doesn't end in a trailing slash, unless the directory is the root directory "/". If path contains no slashes, returns ".".

assert dir("/foo/bar/baz.txt") == "/foo/bar"
assert dir("foo/bar.pdf") == "foo"
assert dir("foo") == "."
assert dir("/") == "/"
ext : String -> String
ext(path)

Returns the extension of path, or "" if there is no extension.

assert ext("/foo/bar/baz.txt") == ".txt"
assert ext("foo/bar.pdf") == ".pdf"
assert ext("foo") == ""
assert ext("/") == ""
strip_ext : String -> String
strip_ext(path)

Returns path with the extension stripped.

assert strip_ext("/foo/bar/baz.txt") == "/foo/bar/baz"
assert strip_ext("foo/bar.pdf") == "foo/bar"
assert strip_ext("foo") == "foo"
assert strip_ext("/") == "/"
normalize : String -> String
normalize(path)

Normalizes path by eliminating redundant . and .. components and extra slashes, returning the shortest equivalent path. Note that, if foo is a symlink, a path like "foo/../bar" may not be equivalent to "bar", so this function can return a wrong result in this case.

assert normalize("foo//bar") == "foo/bar"
assert normalize("foo/../bar") == "bar"
assert normalize("../foo/./../bar/../..") == "../.."
assert normalize("/.././/..//foo///bar") == "/foo/bar"
to_absolute : String -> String
to_absolute(path)

Returns an absolute version of path. The result is normalized (see normalize()).

assert to_absolute("foo") == join(File.cwd(), "foo")
if OS.windows? then
  assert to_absolute("c:/foo/bar") == "c:/foo/bar"
else
  assert to_absolute("/foo/bar") == "/foo/bar"
assert to_absolute("foo/.//../bar//./baz") ==
  join_all([File.cwd(), "bar", "baz"])
to_relative : (String, String) -> String
to_relative(path, from)

Returns a relative path from from to path. from and path are normalized (see normalize()) before a relative path is computed, so the relative path is also normalized.

assert to_relative("foo", "foo") == ""
assert to_relative("bar", "foo") == "../bar"
assert to_relative("foo/a.txt", "foo/bar/baz") == "../../a.txt"
assert to_relative("foo/bar", "foo") == "bar"
absolute? : String -> Bool
absolute?(path)

Returns true if path is absolute.

assert !absolute?("foo")
assert !absolute?("foo/.//../bar//./baz")
if OS.windows? then
  assert absolute?("c:/foo/bar")
else
  assert absolute?("/foo/bar")
relative? : String -> Bool
relative?(path)

Returns true if path is relative.

assert relative?("foo")
assert relative?("foo/.//../bar//./baz")
if OS.windows? then
  assert !relative?("c:/foo/bar")
else
  assert !relative?("/foo/bar")
join : (String, String) -> String
join(path1, path2)

Joins path1 and path2 together with the directory separator.

assert join("foo", "bar") == "foo/bar"
assert join("/foo/bar", "baz/a.txt") == "/foo/bar/baz/a.txt"
assert join("", "bar") == "/bar"
assert join("foo", "") == "foo"
join_all : [String] -> String
join_all(paths)

Joins all paths in paths with the directory separator.

assert join_all(["foo", "bar", "/baz"]) == "foo/bar/baz"
assert join_all(["/foo/bar", "baz/a.txt"]) == "/foo/bar/baz/a.txt"
assert join_all(["", "bar"]) == "/bar"
assert join_all(["foo", "", "bar", ""]) == "foo/bar"
split : String -> [String]
split(path)

Splits path on the directory separator, returning a list of components.

assert split("foo/bar") == ["foo", "bar"]
assert split("/foo/bar/baz/a.txt") == ["/", "foo", "bar", "baz", "a.txt"]
assert split("foo") == ["foo"]