Browse API packages

API · v0.1.0

path

Portable path utilities

Member signatures use schematic object.member(...) API notation. Exported type declarations are valid TypeScript.

Browser-safe path operations with no runtime-native dependencies.

POSIX-specific operations are grouped under path.posix. The portable path.relative operation accepts POSIX, Windows drive, and UNC paths and always returns / separators.

ts
import path from "@rcompat/path";

path.posix#

Browser-safe POSIX path operations.

ts
path.posix: PosixPathUtilities;

POSIX methods#

path.posix.isAbsolute()#

Checks whether a path starts at the POSIX root.

Parameters

  • value — Path to inspect.
ts
path.posix.isAbsolute(value: string): boolean;

path.posix.normalize()#

Resolves repeated separators and . or .. segments.

Preserves a trailing slash. An empty input becomes .; a fully collapsed relative path becomes . or ./ when the input had a trailing slash.

Parameters

  • value — POSIX path to normalize.

Returns: The normalized path.

ts
path.posix.normalize(value: string): string;

path.relative#

Computes a portable relative path from a directory to a target.

Windows drive and UNC comparisons are case-insensitive; POSIX comparisons are case-sensitive. When the inputs have unrelated roots or mix absolute and relative paths, the normalized target is returned instead. Equal paths return an empty string.

Parameters

  • directory — Starting directory.
  • target — Target file or directory.

Returns: A relative or normalized target using / separators.

ts
path.relative(directory: string, target: string): string;

Example

ts
path.relative("/project/src", "/project/test"); // "../test"

Supporting contracts#

PosixPathUtilities#

Browser-safe POSIX path operations.

ts
interface PosixPathUtilities {
  // Checks whether a path starts at the POSIX root.
  isAbsolute(value: string): boolean;
  // Resolves repeated separators and `.` or `..` segments.
  normalize(value: string): string;
}