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.
import path from "@rcompat/path";path.posix#
Browser-safe POSIX path operations.
path.posix: PosixPathUtilities;POSIX methods#
path.posix.isAbsolute()#
Checks whether a path starts at the POSIX root.
Parameters
value— Path to inspect.
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.
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.
path.relative(directory: string, target: string): string;Example
path.relative("/project/src", "/project/test"); // "../test"Supporting contracts#
PosixPathUtilities#
Browser-safe POSIX path operations.
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;
}