Path Module
The std/path module provides cross-platform utilities for working with file and directory paths.
Import
Section titled “Import”import { path } from "std/path";Joining & Building
Section titled “Joining & Building”path.join(...parts: string): string
Section titled “path.join(...parts: string): string”Joins path segments with the OS path separator and cleans the result.
println(path.join("/usr", "local", "bin")); // "/usr/local/bin"println(path.join("src", "utils", "index.chuks")); // "src/utils/index.chuks"path.abs(p: string): string
Section titled “path.abs(p: string): string”Returns the absolute path.
const full = path.abs("./src/main.chuks");println(full); // e.g. "/home/user/project/src/main.chuks"path.clean(p: string): string
Section titled “path.clean(p: string): string”Cleans a path by resolving . and .. segments.
println(path.clean("/usr/local/../bin/./chuks")); // "/usr/bin/chuks"path.rel(base: string, target: string): string
Section titled “path.rel(base: string, target: string): string”Returns the relative path from base to target.
println(path.rel("/usr/local", "/usr/local/bin/chuks")); // "bin/chuks"Extracting Components
Section titled “Extracting Components”path.base(p: string): string
Section titled “path.base(p: string): string”Returns the last element of the path (the file name).
println(path.base("/usr/local/bin/chuks")); // "chuks"println(path.base("/path/to/file.txt")); // "file.txt"path.dir(p: string): string
Section titled “path.dir(p: string): string”Returns the directory portion of the path.
println(path.dir("/usr/local/bin/chuks")); // "/usr/local/bin"path.ext(p: string): string
Section titled “path.ext(p: string): string”Returns the file extension including the dot.
println(path.ext("app.chuks")); // ".chuks"println(path.ext("archive.tar.gz")); // ".gz"path.stem(p: string): string
Section titled “path.stem(p: string): string”Returns the file name without the extension.
println(path.stem("/path/to/file.txt")); // "file"println(path.stem("app.chuks")); // "app"Testing
Section titled “Testing”path.isAbs(p: string): bool
Section titled “path.isAbs(p: string): bool”Returns true if the path is absolute.
println(path.isAbs("/usr/local")); // trueprintln(path.isAbs("relative/path")); // falsepath.match(pattern: string, name: string): bool
Section titled “path.match(pattern: string, name: string): bool”Returns true if the name matches the glob pattern.
println(path.match("*.chuks", "main.chuks")); // trueprintln(path.match("*.go", "main.chuks")); // falseUtility
Section titled “Utility”path.separator(): string
Section titled “path.separator(): string”Returns the OS path separator (/ on Unix, \ on Windows).
println(path.separator()); // "/" on macOS/LinuxFull Example
Section titled “Full Example”import { path } from "std/path";
// Build paths safelyconst projectDir = path.join(path.cwd(), "src");const mainFile = path.join(projectDir, "main.chuks");
println("Project: " + projectDir);println("Main file: " + mainFile);println("Extension: " + path.ext(mainFile));println("Stem: " + path.stem(mainFile));println("Is absolute: " + string(path.isAbs(mainFile)));
// Resolve relative pathsconst from = "/Users/dev/project";const to = "/Users/dev/project/src/lib/utils.chuks";println("Relative: " + path.rel(from, to)); // "src/lib/utils.chuks"