README

moonbitlang/x/path does not have a README file

#
Path

pub(all) struct Path(String) derive(Eq,
Debug
)

A newtype wrapper provide path operation methods.
impl Show for Path

#
Path::basename

fn Path::basename(path : Path) -> StringView

Returns the last path component of the given path.

Trailing separators are ignored, following Node path.basename.

test {
let path : Path = "usr/local/bin"
inspect(path.basename(), content="bin")
}

#
Path::dirname

fn Path::dirname(path : Path) -> Path

Returns the directory portion of the path, following Node path.dirname.

test {
let path : Path = "usr/local/bin"
inspect(path.dirname(), content="usr/local")
}

#
Path::extname

fn Path::extname(path : Path) -> StringView

Returns the extension of the last path component, following Node path.extname dotfile rules.

test {
let path : Path = "archive.tar.gz"
inspect(path.extname(), content=".gz")
}

#
Path::is_absolute

fn Path::is_absolute(path : Path) -> Bool

Returns whether the given path is absolute.

edge cases: when path is empty, return false

test {
let path1 : Path = "/usr/local/bin"
let path2 : Path = "usr/local/bin"
json_inspect(path1.is_absolute(), content=true)
json_inspect(path2.is_absolute(), content=false)
}

#
Path::join

fn Path::join(lhs : Path, rhs : Path) -> Path

Concatenates two path strings with the active separator, then normalizes.

Absolute right-hand paths do not override lhs. When both inputs are empty, the result is ".".

#
Path::normalize

fn Path::normalize(path : Path) -> Path

  1. resolve . by directly removing it
  2. resolve .. by removing the preceding path component if exists, otherwise keep it.
  3. remove redundant slashes or backslashes
  4. preserve trailing slash or backslash

edge cases:
  1. when path is empty, return .
  2. POSIX leading // is normalized to /, following Node behavior.

See @path/posix and @path/win32 for separator-specific examples.

#
Path::relative

fn Path::relative(path : Path, base~ : Path) -> Path

Returns the relative path from base to path.

property: joining base with the returned relative path yields @path.resolve(path).

edge cases:
  1. when @path.resolve(base) == @path.resolve(path), return empty string
  2. on Windows, different roots return the resolved target path

Warning: cwd is already resolve symbolic link and normalized, but path doesn't.

See @path/posix and @path/win32 for separator-specific examples.

#
Path::resolve

fn Path::resolve(path : Path) -> Path

  1. if path is already absolute, normalize it and remove non-root trailing separators.
  2. if path is relative, join current working directory and path, normalize it, and remove non-root trailing separators.

Warning: cwd is already resolve symbolic link and normalized, but path doesn't.

#
delimiter

let delimiter : Char

OS platform specific path delimiter for environment variables. e.g., PATH

#
sep

let sep : Char

OS platform specific path component separator.

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io