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

    Class Filename

    Represents a filename (without any path components), and provides methods to query and manipulate it.

    The class is immutable; all methods that modify the filename return a new instance.

    The constructor ensures that the filename does not contain any directory separators. If it does, an error is thrown.

    const file = new Filename('example.txt')
    console.log(file.stem) // 'example'

    Implements

    Index

    Constructors

    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 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

    • 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 Filename('file.txt').replaceExtension('json') // → Filename('file.json')
      
    • Replace the filename stem, keeping the extension the same

      Parameters

      • newStem: string

      Returns this

      A new instance with the filename stem replaced.

      new Filename('index.ts').replaceStem('main') // → Filename('main.ts')
      
    • Creates a new Filename by calling a callback function with the old filename as a string, and using the returned string as the new filename

      Parameters

      • fn: (filename: string) => string

      Returns Filename

      A new Filename instance