Skip to content

Commit d8592d1

Browse files
authored
docs(path): add examples to @std/path/posix examples (denoland#5371)
* docs(path): add examples to `@std/path/posix` examples * work
1 parent 2e542ff commit d8592d1

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

path/mod.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
* assertEquals(fromFileUrl("file:///home/foo"), "\\home\\foo");
3232
* ```
3333
*
34+
* Functions for working with URLs can be found in
35+
* {@link ./doc/posix/~ | @std/path/posix}.
36+
*
3437
* @module
3538
*/
3639
export * from "./basename.ts";

path/posix/basename.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ import { isPosixPathSeparator } from "./_util.ts";
2323
* assertEquals(basename("/home/user/Documents/image.png", ".png"), "image");
2424
* ```
2525
*
26+
* @example Working with URLs
27+
*
28+
* Note: This function doesn't automatically strip hash and query parts from
29+
* URLs. If your URL contains a hash or query, remove them before passing the
30+
* URL to the function. This can be done by passing the URL to `new URL(url)`,
31+
* and setting the `hash` and `search` properties to empty strings.
32+
*
33+
* ```ts
34+
* import { basename } from "@std/path/posix/basename";
35+
* import { assertEquals } from "@std/assert";
36+
*
37+
* assertEquals(basename("https://deno.land/std/path/mod.ts"), "mod.ts");
38+
* assertEquals(basename("https://deno.land/std/path/mod.ts", ".ts"), "mod");
39+
* assertEquals(basename("https://deno.land/std/path/mod.ts?a=b"), "mod.ts?a=b");
40+
* assertEquals(basename("https://deno.land/std/path/mod.ts#header"), "mod.ts#header");
41+
* ```
42+
*
2643
* @param path The path to extract the name from.
2744
* @param suffix The suffix to remove from extracted name.
2845
* @returns The extracted name.

path/posix/dirname.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ import { isPosixPathSeparator } from "./_util.ts";
1515
*
1616
* assertEquals(dirname("/home/user/Documents/"), "/home/user");
1717
* assertEquals(dirname("/home/user/Documents/image.png"), "/home/user/Documents");
18+
* assertEquals(dirname("https://deno.land/std/path/mod.ts"), "https://deno.land/std/path");
19+
* ```
20+
*
21+
* @example Working with URLs
22+
*
23+
* ```ts
24+
* import { dirname } from "@std/path/posix/dirname";
25+
* import { assertEquals } from "@std/assert";
26+
*
27+
* assertEquals(dirname("https://deno.land/std/path/mod.ts"), "https://deno.land/std/path");
28+
* assertEquals(dirname("https://deno.land/std/path/mod.ts?a=b"), "https://deno.land/std/path");
29+
* assertEquals(dirname("https://deno.land/std/path/mod.ts#header"), "https://deno.land/std/path");
1830
* ```
1931
*
2032
* @param path The path to get the directory from.

path/posix/extname.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ import { isPosixPathSeparator } from "./_util.ts";
1818
* assertEquals(extname("/home/user/Documents/image.png"), ".png");
1919
* ```
2020
*
21+
* @example Working with URLs
22+
*
23+
* Note: This function doesn't automatically strip hash and query parts from
24+
* URLs. If your URL contains a hash or query, remove them before passing the
25+
* URL to the function. This can be done by passing the URL to `new URL(url)`,
26+
* and setting the `hash` and `search` properties to empty strings.
27+
*
28+
* ```ts
29+
* import { extname } from "@std/path/posix/extname";
30+
* import { assertEquals } from "@std/assert";
31+
*
32+
* assertEquals(extname("https://deno.land/std/path/mod.ts"), ".ts");
33+
* assertEquals(extname("https://deno.land/std/path/mod.ts?a=b"), ".ts?a=b");
34+
* assertEquals(extname("https://deno.land/std/path/mod.ts#header"), ".ts#header");
35+
* ```
36+
*
2137
* @param path The path to get the extension from.
2238
* @returns The extension (ex. for `file.ts` returns `.ts`).
2339
*/

path/posix/join.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ import { normalize } from "./normalize.ts";
1616
* assertEquals(path, "/foo/bar/baz/asdf");
1717
* ```
1818
*
19+
* @example Working with URLs
20+
* ```ts
21+
* import { join } from "@std/path/posix/join";
22+
* import { assertEquals } from "@std/assert";
23+
*
24+
* const url = new URL("https://deno.land");
25+
* url.pathname = join("std", "path", "mod.ts");
26+
* assertEquals(url.href, "https://deno.land/std/path/mod.ts");
27+
*
28+
* url.pathname = join("//std", "path/", "/mod.ts");
29+
* assertEquals(url.href, "https://deno.land/std/path/mod.ts");
30+
* ```
31+
*
1932
* @param paths The paths to join.
2033
* @returns The joined path.
2134
*/

path/posix/mod.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
/**
77
* Utilities for working with OS-specific file paths.
88
*
9+
* This module also provides some functions that help when working with URLs.
10+
* See the documentation for examples.
11+
*
912
* Codes in the examples uses POSIX path but it automatically use Windows path
1013
* on Windows. Use methods under `posix` or `win32` object instead to handle non
1114
* platform specific path like:

path/posix/normalize.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ import { isPosixPathSeparator } from "./_util.ts";
1919
* assertEquals(path, "/foo/bar/baz/asdf");
2020
* ```
2121
*
22+
* @example Working with URLs
23+
*
24+
* Note: This function will remove the double slashes from a URL's scheme.
25+
* Hence, do not pass a full URL to this function. Instead, pass the pathname of
26+
* the URL.
27+
*
28+
* ```ts
29+
* import { normalize } from "@std/path/posix/normalize";
30+
* import { assertEquals } from "@std/assert";
31+
*
32+
* const url = new URL("https://deno.land");
33+
* url.pathname = normalize("//std//assert//.//mod.ts");
34+
* assertEquals(url.href, "https://deno.land/std/assert/mod.ts");
35+
*
36+
* url.pathname = normalize("std/assert/../async/retry.ts");
37+
* assertEquals(url.href, "https://deno.land/std/async/retry.ts");
38+
* ```
39+
*
2240
* @param path The path to normalize.
2341
* @returns The normalized path.
2442
*/

0 commit comments

Comments
 (0)