Skip to content

getBaseName

The getBaseName() function returns the last segment of a path (after the final slash), with optional extension removal.

WARNING

Works only with POSIX paths (not Windows paths).

Interactive example

Syntax

typescript
function getBaseName<
	GenericPath extends string
>(
	path: GenericPath,
	params?: {
		removeExtension?: boolean;
	}
): string | null;

Parameters

  • path : The path to analyze.
  • params.removeExtension : When true, removes the extension from the final segment.

Return value

The last path segment (after the final /), with the extension removed if requested, or null when no segment is found (including when the path has no /).

See also

Released under the MIT license.