deno.land / std@0.201.0 / path / _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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { CHAR_COLON } from "./_constants.ts";import { assertPath, isPathSeparator, isPosixPathSeparator, isWindowsDeviceRoot, stripTrailingSeparators,} from "./_util.ts";
function stripSuffix(name: string, suffix: string): string { if (suffix.length >= name.length) { return name; }
const lenDiff = name.length - suffix.length;
for (let i = suffix.length - 1; i >= 0; --i) { if (name.charCodeAt(lenDiff + i) !== suffix.charCodeAt(i)) { return name; } }
return name.slice(0, -suffix.length);}
function lastPathSegment( path: string, isSep: (char: number) => boolean, start = 0,): string { let matchedNonSeparator = false; let end = path.length;
for (let i = path.length - 1; i >= start; --i) { if (isSep(path.charCodeAt(i))) { if (matchedNonSeparator) { start = i + 1; break; } } else if (!matchedNonSeparator) { matchedNonSeparator = true; end = i + 1; } }
return path.slice(start, end);}
function assertArgs(path: string, suffix: string) { assertPath(path); if (path.length === 0) return path; if (typeof suffix !== "string") { throw new TypeError( `Suffix must be a string. Received ${JSON.stringify(suffix)}`, ); }}
/** * Return the last portion of a `path`. * Trailing directory separators are ignored, and optional suffix is removed. * * @param path - path to extract the name from. * @param [suffix] - suffix to remove from extracted name. */export function posixBasename(path: string, suffix = ""): string { assertArgs(path, suffix);
const lastSegment = lastPathSegment(path, isPosixPathSeparator); const strippedSegment = stripTrailingSeparators( lastSegment, isPosixPathSeparator, ); return suffix ? stripSuffix(strippedSegment, suffix) : strippedSegment;}
/** * Return the last portion of a `path`. * Trailing directory separators are ignored, and optional suffix is removed. * * @param path - path to extract the name from. * @param [suffix] - suffix to remove from extracted name. */export function windowsBasename(path: string, suffix = ""): string { assertArgs(path, suffix);
// Check for a drive letter prefix so as not to mistake the following // path separator as an extra separator at the end of the path that can be // disregarded let start = 0; if (path.length >= 2) { const drive = path.charCodeAt(0); if (isWindowsDeviceRoot(drive)) { if (path.charCodeAt(1) === CHAR_COLON) start = 2; } }
const lastSegment = lastPathSegment(path, isPathSeparator, start); const strippedSegment = stripTrailingSeparators(lastSegment, isPathSeparator); return suffix ? stripSuffix(strippedSegment, suffix) : strippedSegment;}
std

Version Info

Tagged at
a year ago