basename(): string
Deprecated
Use
@std/path/basename
instead. @std/url
will be removed in the future.
Returns the base name of a URL or URL string, optionally removing a suffix.
Trailing /
s are ignored. If no path is present, the host name is returned.
If a suffix is provided, it will be removed from the base name. URL queries
and hashes are ignored.
Basic usage
Basic usage
import { basename } from "@std/url/basename"; import { assertEquals } from "@std/assert"; assertEquals(basename("https://deno.land/std/assert/mod.ts"), "mod.ts"); assertEquals(basename(new URL("https://deno.land/std/assert/mod.ts")), "mod.ts"); assertEquals(basename("https://deno.land/std/assert/mod.ts?a=b"), "mod.ts"); assertEquals(basename("https://deno.land/std/assert/mod.ts#header"), "mod.ts"); assertEquals(basename("https://deno.land/"), "deno.land");
Removing a suffix
Removing a suffix
Defining a suffix will remove it from the base name.
import { basename } from "@std/url/basename"; import { assertEquals } from "@std/assert"; assertEquals(basename("https://deno.land/std/assert/mod.ts", ".ts"), "mod"); assertEquals(basename(new URL("https://deno.land/std/assert/mod.ts"), ".ts"), "mod"); assertEquals(basename("https://deno.land/std/assert/mod.ts?a=b", ".ts"), "mod"); assertEquals(basename("https://deno.land/std/assert/mod.ts#header", ".ts"), "mod");
The base name of the URL.