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

    Class RelativePath

    Represents a relative path (i.e. a path that doesn't start at the root, i.e. doesn't have a leading separator), and provides methods for path manipulation and queries.

    RelativePath is similar to AbsolutePath but lacks methods that are only valid for absolute paths.

    const p1 = new Relative('demos')
    const p2 = p1.join('demo1/src', 'index.ts') // 'demos/demo1/src/index.ts'
    Index

    Constructors

    • Create a new RelativePath from a string or another RelativePath.

      The path is normalized and guaranteed to be relative. Any trailing separator is removed.

      Parameters

      Returns RelativePath

      Throws an error if the provided path is absolute

      new RelativePath('project/demos')
      new RelativePath('/project/demos') // ❌ Throws error: path must be relative
      new RelativePath('project//src/../demos/') // → AbsolutePath('project/demos')

    Accessors

    • get extension(): string

      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

      Returns string

    • get filename(): Filename

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

      Returns Filename

      new AbsolutePath('/a/b/c.txt').filename // Filename('c.txt')
      new RelativePath('a/b/c.txt').filename // Filename('c.txt')
    • get parent(): this

      The parent directory of this path.

      Returns this

      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')
    • get segments(): string[]

      The segments of this path as an array of strings.

      Returns string[]

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

      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

      Returns string

    Methods

    • 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')
    • 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 entire directory path through the parent, while keeping the current filename.

      Parameters

      • newParent: string | RelativePath

        Parent directory as string or another path

      Returns this

      A new path rooted at newParent with the same filename.

      new AbsolutePath('/old/file.txt').replaceParent('/new/dir') // '/new/dir/file.txt' (AbsolutePath)
      new RelativePath('old/file.txt').replaceParent('new/dir') // 'new/dir/file.txt' (RelativePath)
    • 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')
    • Returns string

      Returns the string representation of this path.

    • Transform the filename via a callback.

      Parameters

      Returns this

      A new instance with the transformed filename.

      p.transformFilename(f => String(f).toUpperCase())
      
    • Checks whether a string is a relative path. (I.e., if it would be acceptable to the RelativePath constructor.)

      Parameters

      • filepath: string

        The string to check.

      Returns boolean

      true if the string is an absolute path, otherwise false.