deno.land / std@0.224.0 / url / basename.ts

basename.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { basename as posixBasename } from "../path/posix/basename.ts";import { strip } from "./_strip.ts";
/** * 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. * * @param url The URL from which to extract the base name. * @param suffix An optional suffix to remove from the base name. * @returns The base name of the URL. * * @example Basic usage * ```ts * import { basename } from "https://deno.land/std@$STD_VERSION/url/basename.ts"; * * basename("https://deno.land/std/assert/mod.ts"); // "mod.ts" * * basename(new URL("https://deno.land/std/assert/mod.ts")); // "mod.ts" * * basename("https://deno.land/std/assert/mod.ts?a=b"); // "mod.ts" * * basename("https://deno.land/std/assert/mod.ts#header"); // "mod.ts" * * basename("https://deno.land/"); // "deno.land" * ``` * * @example Removing a suffix * ```ts * import { basename } from "https://deno.land/std@$STD_VERSION/url/basename.ts"; * * basename("https://deno.land/std/assert/mod.ts", ".ts"); // "mod" * * basename(new URL("https://deno.land/std/assert/mod.ts"), ".ts"); // "mod" * * basename("https://deno.land/std/assert/mod.ts?a=b", ".ts"); // "mod" * * basename("https://deno.land/std/assert/mod.ts#header", ".ts"); // "mod.ts" * ``` * Defining a suffix will remove it from the base name. */export function basename(url: string | URL, suffix?: string): string { url = new URL(url); strip(url); return posixBasename(url.href, suffix);}
std

Version Info

Tagged at
6 months ago