PathAPI

interface PathAPI : NodeAPI

Node API: path

Describes the API provided by the Node API built-in path module, which supplies utilities for dealing with file and directory paths. Routines are provided for joining, parsing, and extracting bits of information from paths.

 

Summary

The default implementation of the path module supplies path logic for the operating system on which the program is currently running; specific implementations for POSIX and Windows are also provided. The module provides utilities for common path operations, including:

  • basename(): Extracts the last portion of a path.

  • dirname(): Extracts the directory portion of a path.

  • extname(): Extracts the file extension from a path.

  • isAbsolute(): Determines if a path is absolute.

  • join(): Joins multiple path segments together.

  • normalize(): Normalizes a path.

  • parse(): Parses a path into an object.

  • relative(): Determines the relative path between two paths.

  • resolve(): Resolves a sequence of paths or path segments into an absolute path.

  • toNamespacedPath(): Converts a path to a namespace-prefixed path.

  • format(): Converts a path object to a path string.

Several configuration properties are also provided, including:

  • sep: The path segment separator.

  • delimiter: The path delimiter.

 

Specification compliance

This formulation of the Node Path API follows the official definition of the path module for Node version 21.7.3, which is current at the time of this writing. The specification for this API can be found here.

Differences in behavior between Node's implementation of this API are usually considered bugs; where this behavior is intentional, it will be noted in this section.

 

Behavior in Elide

Elide doesn't provide host I/O access by default, and in the case of embedded VFS use, paths are considered under Unix-style rules unconditionally. Most path function implementations use the underlying JVM NIO path utilities.

 

Further reading

Properties

Link copied to clipboard
abstract val delimiter: String

Provides the platform-specific path delimiter:

Link copied to clipboard
abstract val posix: PathAPI

Provides utilities implementing the NodeAPI for POSIX-style paths, regardless of the current operating system. Paths passed to this instance will forcibly be considered as PathStyle.POSIX-style paths.

Link copied to clipboard
abstract val sep: String

Provides the platform-specific path segment separator:

Link copied to clipboard
abstract val win32: PathAPI

Provides utilities implementing the NodeAPI for Windows-style paths, regardless of the current operating system. Paths passed to this instance will forcibly be considered as PathStyle.WIN32-style paths.

Functions

Link copied to clipboard
abstract fun basename(path: Path, ext: String? = null): String
open fun basename(path: String, ext: String? = null): String
open fun basename(path: Path, ext: String? = null): String

Extracts the last portion of a path.

Link copied to clipboard
abstract fun dirname(path: Path): String?
open fun dirname(path: String): String
open fun dirname(path: Path): String?

Extracts the directory portion of a path.

Link copied to clipboard
abstract fun extname(path: Path): String
open fun extname(path: String): String
open fun extname(path: Path): String

Extracts the file extension from a path.

Link copied to clipboard
abstract fun format(pathObject: Any): String

Formats the provided pathObject as a string path; the path object may carry a complete set, or valid subset of, the suite of properties defined on a NodePath object; these include:

Link copied to clipboard
abstract fun isAbsolute(path: Path): Boolean
open fun isAbsolute(path: String): Boolean
open fun isAbsolute(path: Path): Boolean

Determines if a path is absolute.

Link copied to clipboard
open fun join(paths: Iterable<Path>): String
abstract fun join(sequence: Sequence<Path>): String
abstract fun join(first: Path, vararg rest: Path): String
open fun join(first: String, vararg rest: String): String
open fun join(first: Path, vararg rest: Path): String

Joins multiple path segments together.

Link copied to clipboard
abstract fun normalize(path: Path): String
open fun normalize(path: String): String
open fun normalize(path: Path): String

Normalizes a path.

Link copied to clipboard
open fun parse(path: String): Path
abstract fun parse(path: String, pathStyle: PathStyle?): Path

Parses a path into an object.

Link copied to clipboard
abstract fun relative(from: Path, to: Path): String
open fun relative(from: String, to: String): String
open fun relative(from: Path, to: Path): String

Determines the relative path between two paths.

Link copied to clipboard
open fun resolve(iterable: Iterable<Path>): String
abstract fun resolve(sequence: Sequence<Path>): String
abstract fun resolve(first: Path, vararg rest: Path): String
open fun resolve(first: String, vararg rest: String): String
open fun resolve(first: Path, vararg rest: Path): String

Resolves a sequence of paths or path segments into an absolute path.

Link copied to clipboard
abstract fun toNamespacedPath(path: Path): String
open fun toNamespacedPath(path: String): String
open fun toNamespacedPath(path: Path): String

Converts a path to a namespace-prefixed path.