Skip to content

API Path

Liu.Yandong.Hanks edited this page Aug 19, 2026 · 3 revisions

Path

支持协议的路径值

Protocol-aware path value

脚本 API 目录

Script API Directory

概述

Overview

Path 只处理路径文本:它规范化 /、点路径和协议路径,例如 mem://app/main.as,但不读取文件系统。静态方法通常返回字符串;实例方法会修改并返回同一个 Path

Path manipulates path text only: it normalizes /, dot segments, and protocol paths such as mem://app/main.as, but never reads the file system. Static methods usually return strings; instance methods mutate and return the same Path.

new Path(root, ...segments) / Path.of(root, ...segments)

Parameters:

  • root
    根路径或 Path

    Root path or Path.

  • segments
    零个或多个后续路径片段。

    Zero or more subsequent path segments.

Returns

已规范化的可变 Path

Normalized mutable Path.

var first = new Path("mem://app/scripts", "..", "main.as");
var second = Path.of("assets", "ui", "icon.png");
return first.toString() + ":" + second.toString();

Path.isPath(value)

Parameters:

  • value
    要检查的值。

    Value to inspect.

Returns

是否为 Path 对象。

Whether it is a Path object.

return Path.isPath(new Path("scripts")); // true

Path.join(root, ...segments)

Parameters:

  • root
    起始路径。

    Starting path.

  • segments
    要拼接的路径片段。

    Path segments to join.

Returns

规范化路径字符串。

Normalized path string.

return Path.join("scripts", "shared", "main.as");

Path.baseModule(...segments)

Parameters:

  • segments
    相对当前模块目录的路径片段。

    Path segments relative to the current module directory.

Returns

模块目录下的规范化路径;模块外为 null

Normalized path below the module directory, or null outside a module context.

@module(MAIN);

export func assetPath() {
    return Path.baseModule("../assets", "config.json");
}

Path.normalize(path)

Parameters:

  • path
    路径文本或 Path

    Path text or Path.

Returns

统一 / 和点片段后的文本。

Text with normalized / and dot segments.

return Path.normalize("assets/../scripts/main.as"); // scripts/main.as

Path.directoryName(path) / Path.fileName(path) / Path.extName(path)

Parameters:

  • path
    路径文本或 Path

    Path text or Path.

Returns

父目录、最后片段或扩展名(含 .;没有扩展名时为空字符串)。

Parent directory, final segment, or extension (including .; empty when absent).

var path = "assets/ui/icon.png";
return Path.directoryName(path) + ":" + Path.fileName(path) + ":" + Path.extName(path);

Path.protocol(path)

Parameters:

  • path
    路径文本或 Path

    Path text or Path.

Returns

协议名(不带分隔符);普通路径为空字符串。

Protocol name without its separator; empty string for ordinary paths.

return Path.protocol("mem://app/main.as"); // mem

Path.changeExt(path, extension)

Parameters:

  • path
    原路径。

    Original path.

  • extension
    新扩展名,可包含或省略 .

    New extension, with or without ..

Returns

更改扩展名后的规范化文本。

Normalized text with changed extension.

return Path.changeExt("scripts/main", "as"); // scripts/main.as

Path.isRooted(path)

Parameters:

  • path
    路径文本或 Path

    Path text or Path.

Returns

是否为根路径或带协议路径。

Whether it is rooted or protocol-qualified.

return Path.isRooted("mem://app/main.as"); // true

Path.isUnderRoot(root, path)

Parameters:

  • root
    根路径。

    Root path.

  • path
    待检查路径。

    Path to check.

Returns

规范化后是否位于根下。

Whether the normalized path is under the root.

return Path.isUnderRoot("scripts", "scripts/shared/main.as"); // true

Path.currentFile() / Path.currentDirectory()

Parameters:

  • 无。

    None.

Returns

当前模块的完整路径或目录;模块外为 null

Current module full path or directory, or null outside a module context.

@module(MAIN);

export func currentLocation() {
    return Path.currentFile() + ":" + Path.currentDirectory();
}

path.append(...segments)

Parameters:

  • segments
    要追加的路径片段。

    Path segments to append.

Returns

当前 Path

This Path.

副作用

原地修改。

Mutates in place.

var path = new Path("assets");
return path.append("ui", "icon.png").toString();

path.reset(root, ...segments)

Parameters:

  • root
    新根路径。

    New root path.

  • segments
    后续路径片段。

    Subsequent path segments.

Returns

当前 Path

This Path.

副作用

用新路径替换旧路径。

Replaces the current path.

var path = new Path("old");
return path.reset("new", "main.as").toString();

path.changeExt(extension)

Parameters:

  • extension
    新扩展名。

    New extension.

Returns

当前 Path

This Path.

副作用

原地更改扩展名。

Changes the extension in place.

var path = new Path("scripts/main");
return path.changeExt("as").toString();

path.directoryName() / path.fileName() / path.extName() / path.protocol()

Parameters:

  • 无。

    None.

Returns

当前路径的父目录、最后片段、扩展名或协议。

Current path's parent directory, final segment, extension, or protocol.

var path = new Path("mem://app/main.as");
return path.directoryName() + ":" + path.fileName() + ":" + path.extName() + ":" + path.protocol();

path.clone()

Parameters:

  • 无。

    None.

Returns

相同文本的新 Path

New Path with the same text.

var original = new Path("scripts/main.as");
var copy = original.clone();
copy.changeExt("json");
return original.toString(); // scripts/main.as

path.toString()

Parameters:

  • 无。

    None.

Returns

规范化路径文本。

Normalized path text.

return new Path("assets", "..", "scripts/main.as").toString();

Clone this wiki locally