Skip to content

PurePath class

pathlib-ts > PurePath

Immutable filesystem path that never performs I/O.

Signature:

export declare class PurePath

This mirrors CPython’s `pathlib.PurePath`. Separators are normalised according to the active parser (POSIX vs Windows), parts are exposed via flavour-aware casing, and joining semantics match the reference implementation. Prefer this class when you need deterministic, purely lexical path manipulation. Use Path to add filesystem-aware operations.

The constructor preserves the concrete subclass by default: instantiating PurePath chooses the flavour for the current runtime, whereas PurePosixPath and PureWindowsPath can be requested explicitly for cross-platform tooling.

Creating a nested PurePath without touching the filesystem

import { PurePath } from "pathlib-ts";
const project = new PurePath("/srv/app");
const config = project.joinpath("pyproject.toml");
console.log(config.toString()); // '/srv/app/pyproject.toml'
console.log(config.isAbsolute()); // true

Constructor

Modifiers

Description

(constructor)(segments)

Constructs a new instance of the PurePath class

Property

Modifiers

Type

Description

anchor

readonly

string

The concatenation of the drive and root, or ”.

caseSensitive

protected

readonly

boolean

drive

readonly

string

Gets the drive prefix for Windows-style paths, or an empty string on POSIX.

driveCache?

protected

string

(Optional)

name

readonly

string

Returns the final path component as a string, excluding any anchor, if any.

normCaseCache?

protected

string

(Optional)

normPartsCache?

protected

string[]

(Optional)

parent

readonly

PurePath

Returns the lexical (logical) parent of the current path.

parents

readonly

PathParents

Provides indexed access to the sequence of lexical (logical) ancestors of the path.

parser

protected

readonly

nodepath.PlatformPath

parser

static

nodepath.PlatformPath

parts

readonly

string[]

Returns the normalized path components as an array of strings.

rawPaths

protected

string[]

root

readonly

string

A string representing the (local or global) root, if any.

rootCache?

protected

string

(Optional)

stem

readonly

string

Returns the final component without its last suffix.

strCache?

protected

string

(Optional)

suffix

readonly

string

Returns the rightmost suffix (including the leading dot) for the last path segment.

suffixes

readonly

string[]

Lists all suffixes (extensions) attached to the final path component, if any.

tailCache?

protected

string[]

(Optional)

Method

Modifiers

Description

[Symbol.toPrimitive]()

anchorParts()

protected

asPosix()

Returns the string representation of the path with forward (/) slashes, regardless of platform.

asURI()

Converts the path to a file:// URI string.

cloneFromParts(drive, root, tail)

protected

dropSegments(this, drop)

Removes trailing segments from the path while preserving the anchor.

ensureParsed()

protected

fromURI(uri)

static

Creates a path from a file:// URI string.

fullMatch(pattern, options)

Tests whether the entire path matches a glob-style pattern.

isAbsolute()

Indicates whether the path is anchored (has a root and optional drive).

isRelativeTo(other)

Returns true when the path can be expressed relative to another path without leaving its subtree.

joinpath(this, segments)

Produces a new path by appending additional segments to the current instance.

match(pattern, options)

Checks whether the path matches a non-recursive glob pattern.

rawPath()

protected

relativeTo(other, options)

Return the relative path to another path identified by the passed arguments.

tailParts()

protected

toJSON()

toString()

valueOf()

withName(name)

Returns a new path with the final component replaced by the provided name.

withSegments(this, segments)

Builds a sibling path instance of the same type by combining additional segments.

withStem(stem)

Returns a new path with the stem component changed.

withSuffix(suffix)

Return a new path with the file suffix changed. If the path has no suffix, add the given suffix. If the given suffix is an empty string, remove the suffix from the path.