Skip to main content
This package has been archived, and as such it is read-only.

@std/url@0.225.1
Built and signed on GitHub Actions

DEPRECATED: now use path APIs directly

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
6 months ago (0.225.1)
function basename
basename(
url: string | URL,
suffix?: string,
): 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.

Examples

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

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");

Parameters

url: string | URL

The URL from which to extract the base name.

optional
suffix: string

An optional suffix to remove from the base name.

Return Type

The base name of the URL.

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";

Add Package

npx jsr add @std/url

Import symbol

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

Add Package

yarn dlx jsr add @std/url

Import symbol

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

Add Package

pnpm dlx jsr add @std/url

Import symbol

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

Add Package

bunx jsr add @std/url

Import symbol

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