PurePath.dropSegments() method
pathlib-ts > PurePath > dropSegments
PurePath.dropSegments() method
Section titled “PurePath.dropSegments() method”Removes trailing segments from the path while preserving the anchor.
Signature:
dropSegments<T extends PurePath>(this: T, drop: number): T;Parameters
Section titled “Parameters”|
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.
Remarks
Section titled “Remarks”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.
Example
Section titled “Example”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'