@thingts/path - v1.0.5
    Preparing search index...

    Class Filename

    Represents a filename, without any path components, an 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'

    Hierarchy

    • FilenameBase
      • Filename
    Index

    Constructors

    • Create a Filename instance from a string or another Filename

      Throws an error if the provided name contains path separators

      Parameters

      Returns Filename

      new Filename('index.ts') // OK
      new Filename('demo/index.ts') // Throws Error

    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

    • Returns true if this filename equals the other filename or string.

      Parameters

      Returns boolean

    • Replace the filename extensions, keeping the stem the same

      Parameters

      • newExt: string

      Returns this

      A new Filename instance

      new Filename('index.ts').replaceExtension('.js') // 'index.js' (Filename)
      
    • Replace the filename stem, keeping the extension the same

      Parameters

      • newStem: string

      Returns this

      A new Filename instance

      new Filename('index.ts').replaceStem('main') // 'main.ts' (Filename)
      
    • Returns the filename as a string.

      Returns string

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

      A new Filename instance

    • Returns true if the provided string is a valid filename (i.e. does not contain any path separators)

      Parameters

      • filename: string

      Returns boolean