std/path/unstable_extname.ts

33 lines
1.2 KiB
TypeScript

// 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);
}