Skip to content

pathExtname

从路径中获取扩展名

结果: .txt
快速示例:

函数签名

typescript
interface PathExtnameOptions {
  full?: boolean
}

function pathExtname(
  path: string,
  options?: PathExtnameOptions
): string

参数

参数名类型必填说明
pathstring文件路径
optionsPathExtnameOptions配置选项

PathExtnameOptions

属性类型说明
fullboolean是否返回完整扩展名(例如 .tar.gz

返回值

类型说明
string扩展名(包含点号),如果没有扩展名则返回空字符串

使用示例

typescript
import { pathExtname } from 'zcw-shared/functions/path/pathExtname'

// 基本用法
pathExtname('/home/user/document.txt')
// '.txt'

// 多扩展名(默认返回最后一段)
pathExtname('archive.tar.gz')
// '.gz'

// 完整扩展名
pathExtname('archive.tar.gz', { full: true })
// '.tar.gz'

// 无扩展名
pathExtname('README')
// ''

// 隐藏文件(只有点开头的文件名不算扩展名)
pathExtname('.gitignore')
// ''

// 相对路径
pathExtname('./src/main.ts')
// '.ts'