minigame-std
Version:
Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.
66 lines (64 loc) • 1.99 kB
TypeScript
/**
* POSIX 路径工具模块,提供 basename、dirname、normalize 等常用路径操作。
* 仅处理 string 路径,不涉及 URL,确保小游戏平台兼容。
* @module path
*/
/**
* 路径分隔符,始终为 '/'。
* @since 2.4.0
* @example
* ```ts
* import { path } from 'minigame-std';
*
* console.log(path.SEPARATOR); // '/'
* ```
*/
declare const SEPARATOR = "/";
/**
* 提取路径的最后一个片段(文件名)。
* @param path - 要处理的路径字符串。
* @param suffix - 可选的后缀,如果文件名以此结尾则去除。
* @returns 路径中的文件名部分。
* @since 2.4.0
* @example
* ```ts
* import { path } from 'minigame-std';
*
* path.basename('/usr/local/file.txt'); // 'file.txt'
* path.basename('/usr/local/file.txt', '.txt'); // 'file'
* path.basename('/usr/local/'); // 'local'
* ```
*/
declare function basename(path: string, suffix?: string): string;
/**
* 提取路径的目录部分。
* @param path - 要处理的路径字符串。
* @returns 路径中的目录部分。
* @since 2.4.0
* @example
* ```ts
* import { path } from 'minigame-std';
*
* path.dirname('/usr/local/file.txt'); // '/usr/local'
* path.dirname('/usr/local/'); // '/usr'
* path.dirname('file.txt'); // '.'
* path.dirname('/'); // '/'
* ```
*/
declare function dirname(path: string): string;
/**
* 规范化路径,解析 '.' 和 '..' 片段,合并多余斜杠。
* @param path - 要规范化的路径字符串。
* @returns 规范化后的路径。
* @since 2.4.0
* @example
* ```ts
* import { path } from 'minigame-std';
*
* path.normalize('/foo/bar//baz/asdf/quux/..'); // '/foo/bar/baz/asdf'
* path.normalize('./foo/../bar/baz'); // 'bar/baz'
* path.normalize('/foo/bar///baz'); // '/foo/bar/baz'
* ```
*/
declare function normalize(path: string): string;
export { SEPARATOR, basename, dirname, normalize };