filepath

Work with file paths in Gleam!

This library expects paths to be valid unicode. If you need to work with non-unicode paths you will need to convert them to unicode before using this library.

Functions

pub fn base_name(path: String) -> String

Get the base name of a path, that is the name of the file without the containing directory.

Examples

base_name("/usr/local/bin")
// -> "bin"
pub fn directory_name(path: String) -> String

Get the directory name of a path, that is the path without the file name.

Examples

directory_name("/usr/local/bin")
// -> "/usr/local"
pub fn expand(path: String) -> Result(String, Nil)

Expand .. and . segments in a path.

If the path has a .. segment that would go up past the root of the path then an error is returned. This may be useful to example to ensure that a path specified by a user does not go outside of a directory.

If the path is absolute then the result will always be absolute.

Examples

expand("/usr/local/../bin")
// -> Ok("/usr/bin")
expand("/tmp/../..")
// -> Error(Nil)
expand("src/../..")
// -> Error("..")
pub fn extension(path: String) -> Result(String, Nil)

Get the file extension of a path.

Examples

extension("src/main.gleam")
// -> Ok("gleam")
extension("package.tar.gz")
// -> Ok("gz")
pub fn is_absolute(path: String) -> Bool

Check if a path is absolute.

Examples

is_absolute("/usr/local/bin")
// -> True
is_absolute("usr/local/bin")
// -> False
pub fn join(left: String, right: String) -> String

Join two paths together.

This function does not expand .. or . segments, use the expand function to do this.

Examples

join("/usr/local", "bin")
// -> "/usr/local/bin"
pub fn split(path: String) -> List(String)

Split a path into its segments.

When running on Windows both / and \ are treated as path separators, and if the path starts with a drive letter then the drive letter then it is lowercased.

Examples

split("/usr/local/bin", "bin")
// -> ["/", "usr", "local", "bin"]
pub fn split_unix(path: String) -> List(String)

Split a path into its segments, using / as the path separator.

Typically you would want to use split instead of this function, but if you want non-Windows path behaviour on a Windows system then you can use this function.

Examples

split("/usr/local/bin", "bin")
// -> ["/", "usr", "local", "bin"]
pub fn split_windows(path: String) -> List(String)

Split a path into its segments, using / and \ as the path separators. If there is a drive letter at the start of the path then it is lowercased.

Typically you would want to use split instead of this function, but if you want Windows path behaviour on a non-Windows system then you can use this function.

Examples

split("/usr/local/bin", "bin")
// -> ["/", "usr", "local", "bin"]
pub fn strip_extension(path: String) -> String

Remove the extension from a file, if it has any.

Examples

strip_extension("src/main.gleam")
// -> "src/main"
strip_extension("package.tar.gz")
// -> "package.tar"
strip_extension("src/gleam")
// -> "src/gleam"
Search Document