add-name-affixes
Version:
Add prefix and suffix to the basename of file paths or URLs, zero dependencies.
31 lines (30 loc) • 1.43 kB
TypeScript
export interface AddNameOptions {
prefix?: string;
suffix?: string;
/**
* Whether to keep Windows-style backslash separators (`\`) in output.
* Default is false, output will use forward slashes (`/`).
*/
keepWindowsSeparator?: boolean;
}
/**
* Adds a prefix and/or suffix to the basename of a given file path or URL.
*
* Supports both POSIX-style (`/`) and Windows-style (`\`) path separators,
* and correctly handles URLs including query strings and hashes.
*
* Examples:
* - "/foo/bar/file.txt" + prefix="pre-", suffix="-post" => "/foo/bar/pre-file-post.txt"
* - "C:\foo\bar\file.txt" + prefix="pre-", suffix="-post" => "C:/foo/bar/pre-file-post.txt"
* - "https://example.com/path/file.js?query=1#hash" + suffix="_v2" => "https://example.com/path/file_v2.js?query=1#hash"
*
* @param input The input file path or URL string.
* @param options Optional settings:
* - prefix: string to add before the basename (default: "").
* - suffix: string to add after the basename but before extension (default: "").
* - keepWindowsSeparator: if true, preserves backslashes (`\`) in output (default: false).
*
* @returns The modified path or URL string with prefix and/or suffix added to the basename.
* Returns the original input if it cannot be parsed or if no prefix/suffix is provided.
*/
export declare function addNameAffixes(input: string, options?: AddNameOptions): string;