Skip to content

Commit 82ba695

Browse files
committed
docs(path): add many more examples to the main mod.ts
1 parent b5a5fe4 commit 82ba695

File tree

1 file changed

+161
-2
lines changed

1 file changed

+161
-2
lines changed

Diff for: path/mod.ts

+161-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,166 @@
1313
* To use functions for a specific path style regardless of the current OS
1414
* import the modules from the platform sub directory instead.
1515
*
16-
* Example, for POSIX:
16+
* ## Basic Path Operations
17+
*
18+
* ```ts
19+
* import * as path from "@std/path";
20+
* import { assertEquals } from "@std/assert";
21+
*
22+
* // Get components of a path
23+
* if (Deno.build.os === "windows") {
24+
* assertEquals(path.basename("C:\\Users\\user\\file.txt"), "file.txt");
25+
* assertEquals(path.dirname("C:\\Users\\user\\file.txt"), "C:\\Users\\user");
26+
* assertEquals(path.extname("C:\\Users\\user\\file.txt"), ".txt");
27+
* } else {
28+
* assertEquals(path.basename("/home/user/file.txt"), "file.txt");
29+
* assertEquals(path.dirname("/home/user/file.txt"), "/home/user");
30+
* assertEquals(path.extname("/home/user/file.txt"), ".txt");
31+
* }
32+
*
33+
* // Join path segments
34+
* if (Deno.build.os === "windows") {
35+
* assertEquals(path.join("C:\\", "Users", "docs", "file.txt"), "C:\\Users\\docs\\file.txt");
36+
* } else {
37+
* assertEquals(path.join("/home", "user", "docs", "file.txt"), "/home/user/docs/file.txt");
38+
* }
39+
*
40+
* // Normalize a path
41+
* if (Deno.build.os === "windows") {
42+
* assertEquals(path.normalize("C:\\Users\\user\\..\\temp\\.\\file.txt"), "C:\\Users\\temp\\file.txt");
43+
* } else {
44+
* assertEquals(path.normalize("/home/user/../temp/./file.txt"), "/home/temp/file.txt");
45+
* }
46+
*
47+
* // Resolve absolute path
48+
* if (Deno.build.os === "windows") {
49+
* const resolved = path.resolve("C:\\foo", "docs", "file.txt");
50+
* assertEquals(resolved, "C:\\foo\\docs\\file.txt");
51+
* assertEquals(path.isAbsolute(resolved), true);
52+
* } else {
53+
* const resolved = path.resolve("/foo", "docs", "file.txt");
54+
* assertEquals(resolved, "/foo/docs/file.txt");
55+
* assertEquals(path.isAbsolute(resolved), true);
56+
* }
57+
*
58+
* // Get relative path
59+
* if (Deno.build.os === "windows") {
60+
* assertEquals(path.relative("C:\\Users", "C:\\Users\\docs\\file.txt"), "docs\\file.txt");
61+
* assertEquals(path.relative("C:\\Users", "D:\\Programs"), "D:\\Programs");
62+
* } else {
63+
* assertEquals(path.relative("/home/user", "/home/user/docs/file.txt"), "docs/file.txt");
64+
* assertEquals(path.relative("/home/user", "/var/data"), "../../var/data");
65+
* }
66+
* ```
67+
*
68+
* ## Path Parsing and Formatting
69+
*
70+
* ```ts
71+
* import * as path from "@std/path";
72+
* import { assertEquals } from "@std/assert";
73+
*
74+
* if (Deno.build.os === "windows") {
75+
* const parsedWindows = path.parse("C:\\Users\\user\\file.txt");
76+
* assertEquals(parsedWindows.root, "C:\\");
77+
* assertEquals(parsedWindows.dir, "C:\\Users\\user");
78+
* assertEquals(parsedWindows.base, "file.txt");
79+
* assertEquals(parsedWindows.ext, ".txt");
80+
* assertEquals(parsedWindows.name, "file");
81+
*
82+
* // Format path from components (Windows)
83+
* assertEquals(
84+
* path.format({ dir: "C:\\Users\\user", base: "file.txt" }),
85+
* "C:\\Users\\user\\file.txt"
86+
* );
87+
* } else {
88+
* const parsedPosix = path.parse("/home/user/file.txt");
89+
* assertEquals(parsedPosix.root, "/");
90+
* assertEquals(parsedPosix.dir, "/home/user");
91+
* assertEquals(parsedPosix.base, "file.txt");
92+
* assertEquals(parsedPosix.ext, ".txt");
93+
* assertEquals(parsedPosix.name, "file");
94+
*
95+
* // Format path from components (POSIX)
96+
* assertEquals(
97+
* path.format({ dir: "/home/user", base: "file.txt" }),
98+
* "/home/user/file.txt"
99+
* );
100+
* }
101+
* ```
102+
*
103+
* ## URL Conversion
104+
*
105+
* ```ts
106+
* import * as path from "@std/path";
107+
* import { assertEquals } from "@std/assert";
108+
*
109+
* // Convert between file URLs and paths
110+
* if (Deno.build.os === "windows") {
111+
* assertEquals(path.fromFileUrl("file:///C:/Users/user/file.txt"), "C:\\Users\\user\\file.txt");
112+
* assertEquals(path.toFileUrl("C:\\Users\\user\\file.txt").href, "file:///C:/Users/user/file.txt");
113+
* } else {
114+
* assertEquals(path.fromFileUrl("file:///home/user/file.txt"), "/home/user/file.txt");
115+
* assertEquals(path.toFileUrl("/home/user/file.txt").href, "file:///home/user/file.txt");
116+
* }
117+
* ```
118+
*
119+
* ## Path Properties
120+
*
121+
* ```ts
122+
* import * as path from "@std/path";
123+
* import { assertEquals } from "@std/assert";
124+
*
125+
* // Check if path is absolute
126+
* if (Deno.build.os === "windows") {
127+
* assertEquals(path.isAbsolute("C:\\Users"), true);
128+
* assertEquals(path.isAbsolute("\\\\Server\\share"), true);
129+
* assertEquals(path.isAbsolute("C:relative\\path"), false);
130+
* assertEquals(path.isAbsolute("..\\relative\\path"), false);
131+
* } else {
132+
* assertEquals(path.isAbsolute("/home/user"), true);
133+
* assertEquals(path.isAbsolute("./relative/path"), false);
134+
* assertEquals(path.isAbsolute("../relative/path"), false);
135+
* }
136+
*
137+
* // Convert to namespaced path (Windows-specific)
138+
* if (Deno.build.os === "windows") {
139+
* assertEquals(path.toNamespacedPath("C:\\Users\\file.txt"), "\\\\?\\C:\\Users\\file.txt");
140+
* assertEquals(path.toNamespacedPath("\\\\server\\share\\file.txt"), "\\\\?\\UNC\\server\\share\\file.txt");
141+
* } else {
142+
* // On POSIX, toNamespacedPath returns the path unchanged
143+
* assertEquals(path.toNamespacedPath("/home/user/file.txt"), "/home/user/file.txt");
144+
* }
145+
* ```
146+
*
147+
* ## Glob Pattern Utilities
148+
*
149+
* ```ts
150+
* import * as path from "@std/path";
151+
* import { assertEquals } from "@std/assert";
152+
*
153+
* // Check if a string is a glob pattern
154+
* assertEquals(path.isGlob("*.txt"), true);
155+
*
156+
* // Convert glob pattern to RegExp
157+
* const pattern = path.globToRegExp("*.txt");
158+
* assertEquals(pattern.test("file.txt"), true);
159+
*
160+
* // Join multiple glob patterns
161+
* if (Deno.build.os === "windows") {
162+
* assertEquals(path.joinGlobs(["src", "**\\*.ts"]), "src\\**\\*.ts");
163+
* } else {
164+
* assertEquals(path.joinGlobs(["src", "**\/*.ts"]), "src/**\/*.ts");
165+
* }
166+
*
167+
* // Normalize a glob pattern
168+
* if (Deno.build.os === "windows") {
169+
* assertEquals(path.normalizeGlob("src\\..\\**\\*.ts"), "**\\*.ts");
170+
* } else {
171+
* assertEquals(path.normalizeGlob("src/../**\/*.ts"), "**\/*.ts");
172+
* }
173+
* ```
174+
*
175+
* For POSIX-specific functions:
17176
*
18177
* ```ts
19178
* import { fromFileUrl } from "@std/path/posix/from-file-url";
@@ -22,7 +181,7 @@
22181
* assertEquals(fromFileUrl("file:///home/foo"), "/home/foo");
23182
* ```
24183
*
25-
* Or, for Windows:
184+
* For Windows-specific functions:
26185
*
27186
* ```ts
28187
* import { fromFileUrl } from "@std/path/windows/from-file-url";

0 commit comments

Comments
 (0)