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

BREAKING(path/unstable): move unstable overload of extname to unstable-extname #5962

Merged
merged 2 commits into from
Sep 12, 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
2 changes: 2 additions & 0 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ const ENTRY_POINTS = [
"../net/mod.ts",
"../net/unstable_get_network_address.ts",
"../path/mod.ts",
"../path/unstable_basename.ts",
"../path/unstable_dirname.ts",
"../path/unstable_extname.ts",
"../path/posix/mod.ts",
"../path/windows/mod.ts",
"../random/mod.ts",
Expand Down
5 changes: 4 additions & 1 deletion path/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
"./posix/to-namespaced-path": "./posix/to_namespaced_path.ts",
"./posix/unstable-basename": "./posix/unstable_basename.ts",
"./posix/unstable-dirname": "./posix/unstable_dirname.ts",
"./posix/unstable-extname": "./posix/unstable_extname.ts",
"./relative": "./relative.ts",
"./resolve": "./resolve.ts",
"./to-file-url": "./to_file_url.ts",
"./to-namespaced-path": "./to_namespaced_path.ts",
"./types": "./types.ts",
"./unstable-basename": "./unstable_basename.ts",
"./unstable-dirname": "./unstable_dirname.ts",
"./unstable-extname": "./unstable_extname.ts",
"./windows": "./windows/mod.ts",
"./windows/basename": "./windows/basename.ts",
"./windows/common": "./windows/common.ts",
Expand All @@ -68,6 +70,7 @@
"./windows/to-file-url": "./windows/to_file_url.ts",
"./windows/to-namespaced-path": "./windows/to_namespaced_path.ts",
"./windows/unstable-basename": "./windows/unstable_basename.ts",
"./windows/unstable-dirname": "./windows/unstable_dirname.ts"
"./windows/unstable-dirname": "./windows/unstable_dirname.ts",
"./windows/unstable-extname": "./windows/unstable_extname.ts"
}
}
27 changes: 3 additions & 24 deletions path/extname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,12 @@ 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 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("C:\\home\\user\\Documents\\image.png"), ".png");
* assertEquals(extname(new URL("file:///C:/home/user/Documents/image.png")), ".png");
* } else {
* assertEquals(extname("/home/user/Documents/image.png"), ".png");
* assertEquals(extname(new URL("file:///home/user/Documents/image.png")), ".png");
* }
* ```
* Note: If you are working with file URLs,
* use the new version of `extname` from `@std/path/unstable-extname`.
*
* @param path Path with extension.
* @returns The file extension. E.g. returns `.ts` for `file.ts`.
*/
export function extname(path: string | URL): string;
export function extname(path: string | URL): string {
export function extname(path: string): string {
return isWindows ? windowsExtname(path) : posixExtname(path);
}
10 changes: 6 additions & 4 deletions path/extname_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { assertEquals } from "@std/assert";
import * as posix from "./posix/mod.ts";
import * as windows from "./windows/mod.ts";
import { extname as posixUnstableExtname } from "./posix/unstable_extname.ts";
import { extname as windowsUnstableExtname } from "./windows/unstable_extname.ts";

const slashRE = /\//g;

Expand Down Expand Up @@ -70,11 +72,11 @@ Deno.test("posix.extname()", function () {
assertEquals(posix.extname("file.\\\\"), ".\\\\");

assertEquals(
posix.extname(new URL("file:///home/user/Documents/image.png")),
posixUnstableExtname(new URL("file:///home/user/Documents/image.png")),
".png",
);
assertEquals(
posix.extname(new URL("file:///home/user/Documents")),
posixUnstableExtname(new URL("file:///home/user/Documents")),
"",
);
});
Expand All @@ -98,11 +100,11 @@ Deno.test("windows.extname()", function () {
assertEquals(windows.extname("file.\\\\"), ".");

assertEquals(
windows.extname(new URL("file:///C:/home/user/Documents/image.png")),
windowsUnstableExtname(new URL("file:///C:/home/user/Documents/image.png")),
".png",
);
assertEquals(
windows.extname(new URL("file:///C:/home/user/Documents")),
windowsUnstableExtname(new URL("file:///C:/home/user/Documents")),
"",
);
});
34 changes: 3 additions & 31 deletions path/posix/extname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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 @@ -19,40 +18,13 @@ import { fromFileUrl } from "./from_file_url.ts";
* assertEquals(extname("/home/user/Documents/image.png"), ".png");
* ```
*
* @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: 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("/home/user/Documents/file.ts"), ".ts");
* assertEquals(extname("/home/user/Documents/"), "");
* assertEquals(extname("/home/user/Documents/image.png"), ".png");
* 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");
* ```
* Note: If you are working with file URLs,
* use the new version of `extname` from `@std/path/posix/unstable-extname`.
*
* @param path The path to get the extension from.
* @returns The extension (ex. for `file.ts` returns `.ts`).
*/
export function extname(path: string | URL): string;
export function extname(path: string | URL): string {
if (path instanceof URL) {
path = fromFileUrl(path);
}

export function extname(path: string): string {
assertPath(path);

let startDot = -1;
Expand Down
36 changes: 36 additions & 0 deletions path/posix/unstable_extname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { extname as stableExtname } from "./extname.ts";
import { fromFileUrl } from "./from_file_url.ts";

/**
* Return the extension of the `path` with leading period.
*
* 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/unstable-extname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(extname("/home/user/Documents/file.ts"), ".ts");
* assertEquals(extname("/home/user/Documents/"), "");
* assertEquals(extname("/home/user/Documents/image.png"), ".png");
* 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 | URL): string {
if (path instanceof URL) {
path = fromFileUrl(path);
}
return stableExtname(path);
}
32 changes: 32 additions & 0 deletions path/unstable_extname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { isWindows } from "./_os.ts";
import { extname as posixUnstableExtname } from "./posix/unstable_extname.ts";
import { extname as windowsUnstableExtname } from "./windows/unstable_extname.ts";

/**
* 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/unstable-extname";
* import { assertEquals } from "@std/assert";
*
* if (Deno.build.os === "windows") {
* assertEquals(extname("C:\\home\\user\\Documents\\image.png"), ".png");
* assertEquals(extname(new URL("file:///C:/home/user/Documents/image.png")), ".png");
* } else {
* assertEquals(extname("/home/user/Documents/image.png"), ".png");
* 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: string | URL): string {
return isWindows ? windowsUnstableExtname(path) : posixUnstableExtname(path);
}
26 changes: 3 additions & 23 deletions path/windows/extname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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 @@ -18,32 +17,13 @@ import { fromFileUrl } from "./from_file_url.ts";
* assertEquals(ext, ".ts");
* ```
*
* @param path The path to get the extension from.
* @returns The extension of the `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/windows/extname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(extname("file.ts"), ".ts");
* assertEquals(extname(new URL("file:///C:/foo/bar/baz.ext")), ".ext");
* ```
* Note: If you are working with file URLs,
* use the new version of `extname` from `@std/path/windows/unstable-extname`.
*
* @param path The path to get the extension from.
* @returns The extension of the `path`.
*/
export function extname(path: string | URL): string;
export function extname(path: string | URL): string {
if (path instanceof URL) {
path = fromFileUrl(path);
}
export function extname(path: string): string {
assertPath(path);

let start = 0;
Expand Down
29 changes: 29 additions & 0 deletions path/windows/unstable_extname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { extname as stableExtname } from "./extname.ts";
import { fromFileUrl } from "./from_file_url.ts";

/**
* 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/unstable-extname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(extname("file.ts"), ".ts");
* assertEquals(extname(new URL("file:///C:/foo/bar/baz.ext")), ".ext");
* ```
*
* @param path The path to get the extension from.
* @returns The extension of the `path`.
*/
export function extname(path: string | URL): string {
if (path instanceof URL) {
path = fromFileUrl(path);
}
return stableExtname(path);
}