Line data Source code
1 : /*
2 : * Copyright (C) 2024 Red Hat, Inc.
3 : * SPDX-License-Identifier: LGPL-2.1-or-later
4 : */
5 :
6 5 : function drop_slashes(path : string): string {
7 : // Drop all trailing slashes, but never drop the first character.
8 5 : let pos = path.length;
9 5 : while (pos > 1 && path[pos - 1] == "/")
10 0 : pos -= 1;
11 0 : return pos == path.length ? path : path.substring(0, pos);
12 5 : }
13 :
14 3 : export function dirname(path : string): string {
15 3 : const norm = drop_slashes(path);
16 3 : const pos = norm.lastIndexOf("/");
17 3 : if (pos < 0)
18 1 : return ".";
19 2 : else if (pos == 0)
20 0 : return "/";
21 : else
22 2 : return drop_slashes(norm.substring(0, pos));
23 3 : }
24 :
25 3 : export function basename(path : string): string {
26 3 : const norm = drop_slashes(path);
27 3 : const pos = norm.lastIndexOf("/");
28 3 : if (pos < 0)
29 0 : return norm;
30 0 : else if (pos === 0 && norm.length === 1)
31 0 : return "/";
32 : else
33 3 : return norm.substring(pos + 1);
34 3 : }
|