@zokugun/fs-path

中文文档
0.2.1发布于 2026/11/03MITESM + CJS构建来源已验证

Path utilities complementing the builtin path module

原始使用文档
文档目录8 节
  1. Features
  2. Installation
  3. Quick Start
  4. API reference
  5. Comparison with Node's path
  6. Contributions
  7. Donations
  8. License

@zokugun/fs-path

MIT licensed NPM Version Donation Donation Donation

Compact set of path utilities that complement Node's builtin path module.

Features

  • Tilde expansion: expand ~ to the current user's home directory with untildify.
  • Safe joining and resolving: helpers that avoid common pitfalls when composing paths.
  • Useful utilities: retrieve parent paths, absolute normalization, and platform-aware helpers.
  • Small package with ESM and CJS builds

Installation

npm add @zokugun/fs-path

Quick Start

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))

This example expands the current user's home directory, joins paths safely, and prints the resolved absolute path for configDir.

API reference

  • absolute(...paths: string[]): string: Returns a normalized absolute path.
  • isAbsolute(path: string): boolean: Return true when path is absolute for the current platform.
  • isInDir(path: string, parent: string): boolean: Return true when path is equal to or contained within parent.
  • isInDirs(path: string, parents: string[]): boolean: Return true when path is equal to or contained within in one of the parents.
  • isSafePath(path: string, options?): boolean: Return true when path is considered safe for use (guards against suspicious segments such as .. when appropriate for the platform).
  • isSafeSegment(segment: string, target?: Target | 'auto'): boolean: Return true when a single path segment is safe (doesn't contain path separators or traversal sequences).
  • join(...paths: string[]): string: Join path segments.
  • leafExt(path: string, length?: number): string: Return the extensions.
  • leafName(path: string, extension?: number | string): string: Return the basename of path with/without the extensions.
  • matchesGlob?(path: string, pattern: string): boolean: Optional platform-provided glob matcher when available.
  • normalize(path: string): string: Normalize the path (same as path.normalize).
  • parentName(path: string): string: Return the parent name of path.
  • parentPath(path: string): string: Return the parent directory of path.
  • relative(from: string, to: string): string: Compute the relative path from from to to.
  • resolve(...paths: string[]): string: Resolve path segments to an absolute path. Supports tilde expansion:
    • ~ expands to the current user's home directory.
    • ~username/... attempts to expand to that user's home directory when available.
  • sanitize(path: string, options?): Result<string>: Return a sanitized form of path with unsafe segments normalized.
  • untildify(path: string): string: Expand a leading ~ or ~username to the home directory, or return path unchanged.
  • delimiter: Platform-specific character (;/:).
  • separator: Platform-specific character (\//).
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;
};

Comparison with Node's path

This library intentionally mirrors many of Node's path APIs but provides small ergonomic differences and extra features where useful:

  • join(...paths): wraps path.join. Returns '' instead of . when the joined result is the current directory.
  • isAbsolute(path), normalize(path), relative(from, to): direct mappings to path.isAbsolute, path.normalize, and path.relative respectively.
  • parentPath(file): wraps path.dirname. Returns '' instead of . for consistency with join.
  • resolve(...paths): wraps path.resolve but adds tilde expansion (~ and ~username) before resolving.
  • delimiter: same value as path.delimiter on the current platform.
  • separator: same value as path.sep on the current platform.

Contributions

Contributions are most welcome. Please:

  • Open issues and feature requests under the repository discussions.
  • Follow the CONTRIBUTING.md.

Donations

Support this project by becoming a financial contributor.

Ko-fi ko-fi.com/daiyam
Liberapay liberapay.com/daiyam/donate
PayPal paypal.me/daiyam99

License

Copyright © 2026-present Baptiste Augrain

Licensed under the MIT license.

版本与发布记录

@zokugun/fs-path 共发布 3 个版本,首次发布于 2026/05/30;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.1latest2026/11/03今天
0.2.02026/05/315 个月前
0.1.02026/05/305 个月前

关于 @zokugun/fs-path 的常见问题

@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 和提交记录判断。