@thingts/path - v2.0.4
    Preparing search index...

    Interface AbsolutePathOps<TRelative, TResolvable, TJoinable>

    Operations available on path-like objects.

    interface AbsolutePathOps<
        TRelative extends PathOps,
        TResolvable = never,
        TJoinable = never,
    > {
        extension: string | undefined;
        filename: Filename | undefined;
        parent: AbsolutePathOps<TRelative, TResolvable, TJoinable>;
        segments: string[];
        stem: string | undefined;
        descendsFrom(
            ancestor: string | AbsolutePathOps<TRelative, TResolvable, TJoinable>,
            opts?: { includeSelf?: boolean },
        ): boolean;
        equals(
            other: string | AbsolutePathOps<TRelative, TResolvable, TJoinable>,
        ): boolean;
        join(
            ...segments: readonly (
                undefined | null | string | Filename
                | TJoinable
            )[],
        ): this;
        relativeTo(base: this): TRelative;
        replaceExtension(newExt: string): this;
        replaceFilename(newFilename: string | Filename): this;
        replaceParent(
            newParent: string | AbsolutePathOps<TRelative, TResolvable, TJoinable>,
        ): this;
        replaceStem(newStem: string): this;
        resolve(
            ...args: readonly (
                string
                | Filename
                | TResolvable
                | TJoinable
                | null
                | undefined
            )[],
        ): this;
        toString(): string;
        transformFilename(fn: (filename: Filename) => string | Filename): this;
    }

    Type Parameters

    • TRelative extends PathOps
    • TResolvable = never
    • TJoinable = never

    Hierarchy (View Summary)

    Implemented by

    Index

    Properties

    extension: string | undefined

    Returns the extension of the filename including the leading dot, as a string. If the filename has no extension, returns an empty string.

    Note that if the filename starts with a dot (e.g. .gitignore), that dot is considered part of the stem. So .gitignore has extension '' and .gitignore.bak has extension .bak

    filename: Filename | undefined

    The filename component (last path segment) as a Filename.

    new AbsolutePath('/a/b/c.txt').filename // Filename('c.txt')
    new RelativePath('a/b/c.txt').filename // Filename('c.txt')

    The parent directory of this path.

    A new instance pointing to the parent.

    new AbsolutePath('/a/b/c.txt').parent // AbsolutePath('/a/b')
    new RelativePath('a/b/c.txt').parent // RelativePath('a/b')
    segments: string[]

    The segments of this path as an array of strings.

    new AbsolutePath('/a/b/c.txt').segments // ['a', 'b', 'c.txt']
    new RelativePath('a/b/c.txt').segments // ['a', 'b', 'c.txt']
    stem: string | undefined

    Returns the stem of the filename, i.e. the part before the extension. If the filename has no extension, returns the entire filename

    Note that if the filename starts with a dot (e.g. .gitignore), that dot is considered part of the stem. So .gitignore and .gitignore.bak both have stem .gitignore

    Methods

    • Test whether this path is a descendant of the given ancestor path.

      Parameters

      • ancestor: string | AbsolutePathOps<TRelative, TResolvable, TJoinable>

        An absolute path or string to check against.

      • Optionalopts: { includeSelf?: boolean }
        • OptionalincludeSelf?: boolean

          If true, return true when the paths are identical.

      Returns boolean

      True if this path descends from the ancestor, otherwise false.

      const p1 = new AbsolutePath('/project')
      const p2 = new AbsolutePath('/project/src/index.ts')
      p2.descendsFrom(p1) // → true
      p1.descendsFrom(p1) // → false
      p1.descendsFrom(p1, { includeSelf: true }) // → true
    • Join additional path segments to this path.

      Accepts relative path objects and Filename segment arguments for type safety, but you can also directly pass strings for convenience. All args are stringified and interpreted as segments to be joined, regardless of whether they start with a path separator.

      Any null, undefined, or empty segments are ignored.

      Parameters

      Returns this

      A new instance with the segments appended and resulting path normalized.

      const a1 = new AbsolutePath('/project/demo')
      const a2 = a1.join('demo1/src', 'index.js') // → AbsolutePath('/project/demo/demo1/src/index.js')

      const r1 = new RelativePath('demo')
      const r2 = r1.join('demo1/src', 'index.js') // → RelativePath('demo/demo1/src/index.js')
    • Compute the relative path from the given base path to this path.

      Parameters

      • base: this

        The base absolute path.

      Returns TRelative

      A relative path that goes from base to this.

      const p1 = new AbsolutePath('/project')
      const p2 = new AbsolutePath('/project/src/index.ts')
      const rel = p2.relativeTo(p1) // → RelativePath('src/index.ts')
      p1.join(rel).equals(p2) // → true
    • Replace the filename extension, keeping the stem the same. The passed string can include or omit the leading dot; if omitted, it will be added.

      Parameters

      • newExt: string

        New extension, e.g. json or .json

      Returns this

      A new instance with the filename extension replaced.

      new AbsolutePath('/a/b/c.txt').replaceExtension('json') // '/a/b/c.json' (AbsolutePath)
      new RelativePath('a/b/c.txt').replaceExtension('.json') // '/a/b/c.json' (RelativePath)
    • Replace the filename (last segment).

      Parameters

      Returns this

      A new instance with the filename replaced.

      new AbsolutePath('/a/b/c.txt').replaceFilename('d.json') // → AbsolutePath('/a/b/d.json')
      new RelativePath('a/b/c.txt').replaceFilename('d.json') // → RelativePath('a/b/d.json')
    • Replace the filename stem, keeping the extension the same

      Parameters

      • newStem: string

      Returns this

      A new instance with the filename stem replaced.

      new AbsolutePath('/a/b/c.txt').replaceStem('d') // → AbsolutePath('/a/b/d.txt')
      new RelativePath('a/b/c.txt').replaceStem('d') // → RelativePath('a/b/d.txt')
    • Resolve additional paths or components against this absolute path.

      Similar to join(), except that if any argument is absolute, the current path is discarded and resolution starts from that argument.

      Parameters

      Returns this

      A new instance of this type, representing the resolved path.

      join