# @zokugun/fs-path 中文文档

规范地址：https://npmcn.com/package/%40zokugun/fs-path/

版本：0\.2\.1
npm 元数据更新时间：2026-11-02T23:16:16.187Z
中文译文更新时间：2026-11-03T01:05:29.833Z
软件包许可：MIT

本文由上游 README 机器翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40zokugun/fs-path
原文对照：https://npmcn.com/package/%40zokugun/fs-path/?lang=original
项目主页：https://github.com/zokugun/node-fs-path
源码仓库：https://github.com/zokugun/node-fs-path

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：60.2 kB，75 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：3 个
- 首次发布：2026-05-29T22:11:37.351Z
- 最新版本发布：2026-11-02T23:16:15.848Z
- 已发布版本数：3（近 12 个月 3 个）

## 中文简介

路径实用程序补充了内置的路径模块

## 安装

```sh
npm install @zokugun/fs-path
```

也可以使用：`pnpm add @zokugun/fs-path`、`yarn add @zokugun/fs-path`、`bun add @zokugun/fs-path`

## README

# [@zokugun / fs-path](https://github.com/zokugun/node-fs-path)

[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://npmcn.com/package/%40zokugun/fs-path/LICENSE) [![NPM Version](https://img.shields.io/npm/v/@zokugun/fs-path.svg?colorB=green)](https://www.npmjs.com/package/@zokugun/fs-path) [![Donation](https://img.shields.io/badge/donate-ko--fi-green)](https://ko-fi.com/daiyam) [![Donation](https://img.shields.io/badge/donate-liberapay-green)](https://liberapay.com/daiyam/donate) [![Donation](https://img.shields.io/badge/donate-paypal-green)](https://paypal.me/daiyam99)

> 紧凑的路径实用程序集，补充了Node的内置功能 `path` 模块。

## 功能

-   波浪膨胀：膨胀 `~` 到当前用户的主目录 `untildify`.
-   安全连接和解析：在组合路径时避免常见陷阱的帮助程序。
-   有用的实用程序：检索父路径、绝对规范化和平台感知的帮助程序。
-   带有ESM和CJS构建的小包

## 安装

```bash
npm add @zokugun/fs-path
```

## 快速入门

```ts
import { isInDir, join, resolve } from '@zokugun/fs-path'

const configDir = resolve('~/.config/myapp')
const dataFile = join(configDir, 'data.json')

console.log('Config dir:', configDir)
console.log('Data file:', dataFile)

console.log('Is data file inside config dir?', isInDir(dataFile, configDir))
```

此示例展开当前用户的主目录，安全地连接路径，并输出解析后的绝对路径 `configDir`.

## API参考

-   `absolute(...paths: string[]): string`：返回标准化的绝对路径。
-   `isAbsolute(path: string): boolean`:返回 `true` 当 `path` 是当前平台的绝对值。
-   `isInDir(path: string, parent: string): boolean`:返回 `true` 当 `path` 等于还是包含在 `parent`.
-   `isInDirs(path: string, parents: string[]): boolean`:返回 `true` 当 `path` 等于或包含在其中一个 `parents`.
-   `isSafePath(path: string, options?): boolean`:返回 `true` 当 `path` 被认为是安全的使用(防止可疑的部分，如 `..` 当适合平台时)。
-   `isSafeSegment(segment: string, target?: Target | 'auto'): boolean`:返回 `true` 当单个路径段是安全的（不包含路径分隔符或遍历序列）。
-   `join(...paths: string[]): string`：连接路径段。
-   `leafExt(path: string, length?: number): string`：返回扩展名。
-   `leafName(path: string, extension?: number | string): string`：返回的基名 `path` 带/不带扩展。
-   `matchesGlob?(path: string, pattern: string): boolean`：可选的平台提供的全局匹配器（当可用时）。
-   `normalize(path: string): string`：将路径归一化(与 `path.normalize`）。
-   `parentName(path: string): string`的父名称 `path`.
-   `parentPath(path: string): string`：返回的父目录 `path`.
-   `relative(from: string, to: string): string`：计算相对路径 `from` 来 `to`.
-   `resolve(...paths: string[]): string`：将路径段解析为绝对路径。支持波浪扩展：
    -   `~` 展开到当前用户的主目录。
    -   `~username/...` 尝试在可用时扩展到该用户的主目录。
-   `sanitize(path: string, options?): Result<string>`：返回的净化形式 `path` 不安全段归一化。
-   `untildify(path: string): string`：扩大领先地位 `~` 或者 `~username` 到主目录，或者返回 `path` 不变。
-   `delimiter`：平台特定字符(`;`/`:`）。
-   `separator`：平台特定字符(`\`/`/`）。

```ts
export type PlatformPath = {
    readonly delimiter: ';' | ':';
    readonly separator: '\\' | '/';
    readonly posix: PlatformPath;
    readonly win32: PlatformPath;
    absolute: (...paths: string[]) => string;
    isAbsolute: (path: string) => boolean;
    isInDir(path: string, parent: string): boolean;
    isInDirs(path: string, parents: string[]): boolean;
    isSafePath: (path: string, options?: {
        parent?: string | null;
        parents?: string | string[] | null;
        target?: Target | 'auto';
    }) => boolean;
    isSafeSegment: (segment: string, target?: Target | 'auto') => boolean;
    join: (...paths: string[]) => string;
    leafExt: (path: string, length?: number) => string;
    leafName: (path: string, extension?: number | string) => string;
    matchesGlob?: (path: string, pattern: string) => boolean;
    normalize: (path: string) => string;
    parentName: (path: string) => string;
    parentPath: (path: string) => string;
    relative: (from: string, to: string) => string;
    resolve: (...paths: string[]) => string;
    sanitize(path: string, options?: {
        parent?: string | null;
        parents?: string | string[] | null;
        replacement?: string | null;
        target?: Target | 'auto'
    }) => Result<string>
    untildify: (path: string) => string;
};
```

## 与Node的比较 `path`

这个库有意地反映了Node的许多库 `path` api，但提供了微小的人体工程学差异和有用的额外功能：

-   `join(...paths)`:包装 `path.join`。返回 `''` 而不是 `.` 当联接结果为当前目录时。
-   `isAbsolute(path)`， `normalize(path)`， `relative(from, to)`：直接映射到 `path.isAbsolute`， `path.normalize`, `path.relative` 分别。
-   `parentPath(file)`:包装 `path.dirname`。返回 `''` 而不是 `.` 为了与 `join`.
-   `resolve(...paths)`:包装 `path.resolve` 但增加了波浪膨胀(`~` 和 `~username`)，然后再解决。
-   `delimiter`：与 `path.delimiter` 在当前的平台上。
-   `separator`：与 `path.sep` 在当前的平台上。

## 贡献

欢迎投稿。请:

-   在存储库讨论下打开问题和特性请求。
-   遵循 [`CONTRIBUTING.md`](https://npmcn.com/package/%40zokugun/fs-path/CONTRIBUTING.md).

## 捐款

通过成为财务贡献者来支持这个项目。

<table><tbody><tr><td><img alt="Ko-fi" height="80px" src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_kofi.png" width="80px" loading="lazy" decoding="async" referrerpolicy="no-referrer"></td><td><a href="https://ko-fi.com/daiyam" rel="nofollow noopener noreferrer">ko-fi.com/daiyam</a></td></tr><tr><td><img alt="Liberapay" height="80px" src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_liberapay.png" width="80px" loading="lazy" decoding="async" referrerpolicy="no-referrer"></td><td><a href="https://liberapay.com/daiyam/donate" rel="nofollow noopener noreferrer">liberapay.com/daiyam/donate</a></td></tr><tr><td><img alt="PayPal" height="80px" src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_paypal.png" width="80px" loading="lazy" decoding="async" referrerpolicy="no-referrer"></td><td><a href="https://paypal.me/daiyam99" rel="nofollow noopener noreferrer">paypal.me / daiyam99</a></td></tr></tbody></table>

## 许可证

版权所有©2026-present Baptiste Augrain

根据 [麻省理工学院的许可](https://opensource.org/licenses/MIT).

## 常见问题

### @zokugun/fs-path 是什么？

路径实用程序补充了内置的路径模块

### 如何安装 @zokugun/fs-path？

在项目目录运行 npm install @zokugun/fs-path，或使用 pnpm add / yarn add / bun add。

### @zokugun/fs-path 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/zokugun\_\_fs-path 是否存在。

### @zokugun/fs-path 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @zokugun/fs-path 可以商用吗？

@zokugun/fs-path 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @zokugun/fs-path 还在维护吗？

最新版本 0\.2\.1 发布于 2026/11/03（今天）；至今共发布 3 个版本，其中过去 12 个月发布了 3 个；首次发布于 2026/05/30。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

