LCOV - code coverage report
Current view: top level - pkg/lib - cockpit-path.ts Coverage Total Hit
Test: cockpit Lines: 75.0 % 24 18
Test Date: 2026-06-25 09:20:42

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2024 Red Hat, Inc.
       3              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       4              :  */
       5              : 
       6            4 : function drop_slashes(path : string): string {
       7              :     // Drop all trailing slashes, but never drop the first character.
       8            4 :     let pos = path.length;
       9            4 :     while (pos > 1 && path[pos - 1] == "/")
      10            0 :         pos -= 1;
      11            0 :     return pos == path.length ? path : path.substring(0, pos);
      12            4 : }
      13              : 
      14            2 : export function dirname(path : string): string {
      15            2 :     const norm = drop_slashes(path);
      16            2 :     const pos = norm.lastIndexOf("/");
      17            2 :     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            2 : }
      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 : }
        

Generated by: LCOV version 2.0-1