Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(path/unstable): support URL in extname() #5818

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions path/extname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ import { extname as windowsExtname } from "./windows/extname.ts";
* @param path Path with extension.
* @returns The file extension. E.g. returns `.ts` for `file.ts`.
*/
export function extname(path: string): string {
return isWindows ? windowsExtname(path) : posixExtname(path);
export function extname(path: string): string;
/**
* Return the extension of the path with leading period (".").
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { extname } from "@std/path/extname";
* import { assertEquals } from "@std/assert";
*
* if (Deno.build.os === "windows") {
* assertEquals(extname(new URL("file:///C:/home/user/Documents/image.png")), ".png");
* } else {
* assertEquals(extname(new URL("file:///home/user/Documents/image.png")), ".png");
* }
* ```
*
* @param path Path with extension.
* @returns The file extension. E.g. returns `.ts` for `file.ts`.
*/
export function extname(path: URL): string;
export function extname(path: string | URL): string {
// deno-lint-ignore no-explicit-any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't any be avoided with toString() or String(path)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We like to do path instanceof URL check in each implementation. String(path) makes that check not working.

return isWindows ? windowsExtname(path as any) : posixExtname(path as any);
}
18 changes: 18 additions & 0 deletions path/extname_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ Deno.test("posix.extname()", function () {
assertEquals(posix.extname("file\\\\"), "");
assertEquals(posix.extname("file.\\"), ".\\");
assertEquals(posix.extname("file.\\\\"), ".\\\\");

assertEquals(
posix.extname(new URL("file:///home/user/Documents/image.png")),
".png",
);
assertEquals(
posix.extname(new URL("file:///home/user/Documents")),
"",
);
});

Deno.test("windows.extname()", function () {
Expand All @@ -87,4 +96,13 @@ Deno.test("windows.extname()", function () {
assertEquals(windows.extname("file\\\\"), "");
assertEquals(windows.extname("file.\\"), ".");
assertEquals(windows.extname("file.\\\\"), ".");

assertEquals(
windows.extname(new URL("file:///C:/home/user/Documents/image.png")),
".png",
);
assertEquals(
windows.extname(new URL("file:///C:/home/user/Documents")),
"",
);
});
30 changes: 21 additions & 9 deletions path/posix/extname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { CHAR_DOT } from "../_common/constants.ts";
import { assertPath } from "../_common/assert_path.ts";
import { isPosixPathSeparator } from "./_util.ts";
import { fromFileUrl } from "./from_file_url.ts";

/**
* Return the extension of the `path` with leading period.
Expand All @@ -18,26 +19,37 @@ import { isPosixPathSeparator } from "./_util.ts";
* assertEquals(extname("/home/user/Documents/image.png"), ".png");
* ```
*
* @example Working with URLs
* @param path The path to get the extension from.
* @returns The extension (ex. for `file.ts` returns `.ts`).
*/
export function extname(path: string): string;
/**
* Return the extension of the `path` with leading period.
*
* Note: This function doesn't automatically strip hash and query parts from
* URLs. If your URL contains a hash or query, remove them before passing the
* URL to the function. This can be done by passing the URL to `new URL(url)`,
* and setting the `hash` and `search` properties to empty strings.
* Note: Hashes and query parameters are ignore when constructing a URL.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
*
* ```ts
* import { extname } from "@std/path/posix/extname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(extname("https://deno.land/std/path/mod.ts"), ".ts");
* assertEquals(extname("https://deno.land/std/path/mod.ts?a=b"), ".ts?a=b");
* assertEquals(extname("https://deno.land/std/path/mod.ts#header"), ".ts#header");
* assertEquals(extname(new URL("file:///home/user/Documents/file.ts")), ".ts");
* assertEquals(extname(new URL("file:///home/user/Documents/file.ts?a=b")), ".ts");
* assertEquals(extname(new URL("file:///home/user/Documents/file.ts#header")), ".ts");
* ```
*
* @param path The path to get the extension from.
* @returns The extension (ex. for `file.ts` returns `.ts`).
*/
export function extname(path: string): string {
export function extname(path: URL): string;
export function extname(path: string | URL): string {
if (path instanceof URL) {
path = fromFileUrl(path);
}

assertPath(path);

let startDot = -1;
Expand Down
25 changes: 24 additions & 1 deletion path/windows/extname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { CHAR_COLON, CHAR_DOT } from "../_common/constants.ts";
import { assertPath } from "../_common/assert_path.ts";
import { isPathSeparator, isWindowsDeviceRoot } from "./_util.ts";
import { fromFileUrl } from "./from_file_url.ts";

/**
* Return the extension of the `path` with leading period.
Expand All @@ -20,7 +21,29 @@ import { isPathSeparator, isWindowsDeviceRoot } from "./_util.ts";
* @param path The path to get the extension from.
* @returns The extension of the `path`.
*/
export function extname(path: string): string {
export function extname(path: string): string;
/**
* Return the extension of the `path` with leading period.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { extname } from "@std/path/windows/extname";
* import { assertEquals } from "@std/assert";
*
* const ext = extname(new URL("file:///C:/foo/bar/baz.ext"));
* assertEquals(ext, ".ext");
* ```
*
* @param path The path to get the extension from.
* @returns The extension of the `path`.
*/
export function extname(path: URL): string;
export function extname(path: string | URL): string {
if (path instanceof URL) {
path = fromFileUrl(path);
}
assertPath(path);

let start = 0;
Expand Down