Skip to main content

@std/url@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Published2 years ago (0.215.0)
function basename
basename(
url: string | URL,
suffix?: string
): string

Return the last portion of a URL, or the host name if there is no path. Trailing /s are ignored, and optional suffix is removed.

Examples

Example 1

import { basename } from "@std/url/basename";

// basename accepts a string or URL
console.log(basename("https://deno.land/std/assert/mod.ts"));  // "mod.ts"
console.log(basename(new URL("https://deno.land/std/assert/mod.ts"))); // "mod.ts"

// basename accepts an optional suffix to remove
console.log(basename(new URL("https://deno.land/std/assert/mod.ts"), ".ts")); // "mod"

// basename does not include query parameters or hash fragments
console.log(basename(new URL("https://deno.land/std/assert/mod.ts?a=b"))); // "mod.ts"
console.log(basename(new URL("https://deno.land/std/assert/mod.ts#header"))); // "mod.ts"

// If no path is present, the host name is returned
console.log(basename(new URL("https://deno.land/"))); // "deno.land"

Parameters

  • url to extract the final path segment from.
optional
suffix: string
  • optional suffix to remove from extracted name.

Return Type

the last portion of the URL path, or the URL origin if there is no path.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/url

Import symbol

import { basename } from "@std/url";
or

Import directly with a jsr specifier

import { basename } from "jsr:@std/url";