Skip to content

@utilslib/core/getFileName


获取文件名(不包含扩展名)。


getFileName

函数签名

typescript
function getFileName(fileName: string): string | ""

描述

获取文件名(不包含扩展名)。

参数

参数名类型可选默认值描述
fileNamestring--

返回值

string \| ""

点击查看源码
js
/**
 * 从文件路径中提取文件名,可选择去除扩展名。
 *
 * @param {string} path - 包含文件名的路径。
 * @param {string} [ext] - 可选的扩展名,如果提供且文件名以该扩展名结尾,则会从结果中移除。
 * @returns {string} 提取出的文件名。
 * @example
 * \`\`\`ts
 * basename('/path/to/file.txt') // => "file.txt"
 * basename('/path/to/file.txt', '.txt') // => "file"
 * \`\`\`
 */
function getBasename(path, ext) {
  if (!path || typeof path !== "string") return "";
  const filename =
    path
      .replace(/[\/\\]+$/, "")
      .split(/[\/\\]/)
      .pop() || "";
  return ext && filename.endsWith(ext)
    ? filename.slice(0, -ext.length)
    : filename;
}
/**
 * 获取文件名(不包含扩展名)。
 *
 * @param {string} fileName - 文件名。
 * @returns {string | ""} 提取的文件名。
 */
export function getFileName(fileName) {
  const name = getBasename(fileName);
  const lastDotIndex = name.lastIndexOf(".");
  if (lastDotIndex === -1) {
    return name;
  }
  return name.slice(0, lastDotIndex);
}
ts
/**
 * 从文件路径中提取文件名,可选择去除扩展名。
 *
 * @param {string} path - 包含文件名的路径。
 * @param {string} [ext] - 可选的扩展名,如果提供且文件名以该扩展名结尾,则会从结果中移除。
 * @returns {string} 提取出的文件名。
 * @example
 * \`\`\`ts
 * basename('/path/to/file.txt') // => "file.txt"
 * basename('/path/to/file.txt', '.txt') // => "file"
 * \`\`\`
 */
function getBasename(path: string, ext?: string): string {
  if (!path || typeof path !== "string") return "";
  const filename =
    path
      .replace(/[\/\\]+$/, "")
      .split(/[\/\\]/)
      .pop() || "";
  return ext && filename.endsWith(ext)
    ? filename.slice(0, -ext.length)
    : filename;
}

/**
 * 获取文件名(不包含扩展名)。
 *
 * @param {string} fileName - 文件名。
 * @returns {string | ""} 提取的文件名。
 */
export function getFileName(fileName: string): string | "" {
  const name = getBasename(fileName);
  const lastDotIndex = name.lastIndexOf(".");
  if (lastDotIndex === -1) {
    return name;
  }
  return name.slice(0, lastDotIndex);
}

如有错误,请提交issue :::