Skip to content

Documentation / base / PathUtils

Class: PathUtils

Defined in: libs/base/src/vfs/common.ts:59

Path utilities.

Provides POSIX-like path manipulation helpers (pure string operations), including normalization, joining, dirname/basename/extname extraction, absolute-path detection, and relative path computation.

Notes:

  • Uses "/" as the separator (web/URL or POSIX-like paths).
  • All methods are pure and do not touch a real filesystem.
  • normalize collapses ".", "..", and redundant slashes.

Constructors

Constructor

new PathUtils(): PathUtils

Returns

PathUtils

Methods

normalize()

static normalize(path): string

Defined in: libs/base/src/vfs/common.ts:76

Normalizes a path by collapsing redundant slashes, removing "." segments, and resolving ".." segments.

Rules:

  • Multiple consecutive "/" are collapsed into a single "/".
  • "." segments are removed.
  • ".." removes the previous segment (no-op at root).
  • The result always starts with "/" (absolute form).

Example:

  • normalize('/a//b/./c/../d') -> '/a/b/d'

Parameters

path

string

Input path (relative or absolute).

Returns

string

The normalized absolute path (always starting with "/").


join()

static join(...paths): string

Defined in: libs/base/src/vfs/common.ts:104

Joins multiple path segments and normalizes the result.

Behavior:

  • Concatenates segments with "/" and then runs normalize.
  • The returned path is always absolute.

Example:

  • join('/a', 'b', '../c') -> '/a/c'

Parameters

paths

...string[]

Path segments in order.

Returns

string

Normalized absolute path.


dirname()

static dirname(path): string

Defined in: libs/base/src/vfs/common.ts:123

Returns the directory name (parent directory) of a path.

Behavior:

  • Applies normalize first.
  • If the path is root "/" or has no parent, returns "/".

Examples:

  • dirname('/a/b/c') -> '/a/b'
  • dirname('/a') -> '/'
  • dirname('/') -> '/'

Parameters

path

string

Input path.

Returns

string

Directory path of the input.


basename()

static basename(path, ext?): string

Defined in: libs/base/src/vfs/common.ts:145

Returns the last portion of a path (file name).

Behavior:

  • Applies normalize first.
  • If ext is provided and the name ends with it, the extension is stripped.

Examples:

  • basename('/a/b/c.txt') -> 'c.txt'
  • basename('/a/b/c.txt', '.txt') -> 'c'
  • basename('/') -> ''

Parameters

path

string

Input path.

ext?

string

Optional extension to strip (exact suffix match).

Returns

string

The base name of the path.


extname()

static extname(path): string

Defined in: libs/base/src/vfs/common.ts:171

Returns the extension of the path, including the leading dot.

Behavior:

  • Based on the result of basename.
  • If there is no dot, returns an empty string.

Examples:

  • extname('/a/b/c.txt') -> '.txt'
  • extname('/a/b/c') -> ''

Parameters

path

string

Input path.

Returns

string

The extension (e.g., ".txt") or an empty string if none.


sanitizeFilename()

static sanitizeFilename(filename, options?): string

Defined in: libs/base/src/vfs/common.ts:198

Sanitizes a file or directory name by replacing or removing invalid characters.

Behavior:

  • Removes or replaces characters that are invalid in common filesystems.
  • Trims leading/trailing spaces and dots.
  • Collapses multiple spaces into single spaces.
  • Replaces reserved names with safe alternatives.
  • Optionally limits the length of the result.

Examples:

  • sanitizeFilename('my file.txt') -> 'my file.txt'
  • sanitizeFilename('file:name*.txt') -> 'file_name_.txt'
  • sanitizeFilename(' .hidden ') -> 'hidden'
  • sanitizeFilename('CON') -> '_CON'
  • sanitizeFilename('a'.repeat(300)) -> (truncated to maxLength)

Parameters

filename

string

Input filename to sanitize.

options?

Optional configuration.

replacement?

string

maxLength?

number

asciiOnly?

boolean

Returns

string

Sanitized filename safe for use across platforms.


isAbsolute()

static isAbsolute(path): boolean

Defined in: libs/base/src/vfs/common.ts:253

Determines whether the path is absolute.

Definition here: absolute paths start with "/".

Parameters

path

string

Input path.

Returns

boolean

True if the path starts with "/", otherwise false.


relative()

static relative(from, to): string

Defined in: libs/base/src/vfs/common.ts:274

Computes a relative path from one path to another.

Behavior:

  • Both from and to are normalized first.
  • The returned path does not start with "/" (relative form).
  • If both resolve to the same path, returns ".".

Examples:

  • relative('/a/b/c', '/a/d/e') -> '../../d/e'
  • relative('/a/b', '/a/b/c') -> 'c'
  • relative('/a/b', '/a/b') -> '.'

Parameters

from

string

Base path to start from.

to

string

Target path to reach.

Returns

string

Relative path from from to to.

Released under the MIT License.