Skip to content

PurePath.dropSegments() method

pathlib-ts > PurePath > dropSegments

Removes trailing segments from the path while preserving the anchor.

Signature:

dropSegments<T extends PurePath>(this: T, drop: number): T;

Parameter

Type

Description

this

T

drop

number

Number of segments to discard from the right-hand side. Values greater than the path length clamp to zero.

Returns:

T

A new path of the same type with the requested segments removed.

This is primarily used internally to synthesise parent anchors, but it can be useful when you need to truncate an arbitrary number of lexical components without re-parsing the string form.

Trimming nested directories

import { PurePath } from "pathlib-ts";
const nested = new PurePath("/srv/app/templates/email/welcome.html");
const section = nested.dropSegments(2);
console.log(section.toString()); // '/srv/app/templates'