-
Notifications
You must be signed in to change notification settings - Fork 577
/
basename.ts
28 lines (26 loc) · 975 Bytes
/
basename.ts
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { isWindows } from "./_os.ts";
import { basename as posixBasename } from "./posix/basename.ts";
import { basename as windowsBasename } from "./windows/basename.ts";
/**
* Return the last portion of a `path`.
* Trailing directory separators are ignored, and optional suffix is removed.
*
* @example
* ```ts
* import { basename } from "https://deno.land/std@$STD_VERSION/path/basename.ts";
*
* basename("/home/user/Documents/"); // "Documents"
* basename("C:\\user\\Documents\\image.png"); // "image.png"
* basename("/home/user/Documents/image.png", ".png"); // "image"
* ```
*
* @param path - path to extract the name from.
* @param [suffix] - suffix to remove from extracted name.
*/
export function basename(path: string, suffix = ""): string {
return isWindows
? windowsBasename(path, suffix)
: posixBasename(path, suffix);
}