filepath
Work with file paths in Gleam!
This package does not yet support Windows paths, but it will in the future.
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.
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"