Code coverage report for js/utils.tsx

# All files / js utils.tsx

30.77% Statements 16/52
23.91% Branches 11/46
43.75% Functions 7/16
30.77% Lines 16/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    15x

    3x
    3x

    7x
    7x

    4x
    4x

    5x
    5x

    4x
    2x

    2x

    3x
    3x

    3x
    3x

     export function isValidUrl(url: string) {
      return !!url.match(/^([a-z0-9+.-]+):(\/\/)?[^/]\S+$/);
    }

    /**
     * Trim leading slashes from a string.
     */
    export function stripLeadingSlashes(string: string) {
      const match = /^\/*(.*?)$/.exec(string);
      return match ? match[1] : "";
    }

    /**
     * Trim trailing slashes from a string.
     */
    export function stripTrailingSlashes(string: string) {
      const match = /^(.*?)\/*$/.exec(string);
      return match ? match[1] : "";
    }

    /**
     * Trim both leading and trailing slashes from a string.
     */
    export function trimSlashes(string: string) {
      const match = /^\/*(.*?)\/*$/.exec(string);
      return match ? match[1] : "";
    }

    /**
     * Trim both leading and trailing colons from a string.
     */
    export function trimColons(string: string) {
      const match = /^:*(.*?):*$/.exec(string);
      return match ? match[1] : "";
    }

    export function getCanonicalUrl(localPath: string) {
      const base = stripTrailingSlashes($LEKTOR_CONFIG.site_root);
      return `${base}/${stripLeadingSlashes(localPath)}`;
    }

    export function urlPathsConsideredEqual(a: string | null, b: string | null) {
      if (a == null || b == null) {
        return false;
      }
      return stripTrailingSlashes(a) === stripTrailingSlashes(b);
    }

    export function getApiUrl(url: string) {
      return `${$LEKTOR_CONFIG.admin_root}/api${url}`;
    }

    export function getPlatform() {
      if (navigator.appVersion.indexOf("Win") !== -1) {
        return "windows";
      } else if (navigator.appVersion.indexOf("Mac") !== -1) {
        return "mac";
      } else if (
        navigator.appVersion.indexOf("X11") !== -1 ||
        navigator.appVersion.indexOf("Linux") !== -1
      ) {
        return "linux";
      }
      return "other";
    }

    export interface KeyboardShortcut {
      key: string;
      mac?: string;
      preventDefault?: boolean;
    }

    export function getKey(shortcut: KeyboardShortcut): string {
      return getPlatform() === "mac" && shortcut.mac ? shortcut.mac : shortcut.key;
    }

    export function keyboardShortcutHandler(
      shortcut: KeyboardShortcut,
      action: (ev: KeyboardEvent) => void
    ): (ev: KeyboardEvent) => void {
      const key = getKey(shortcut);
      return (ev) => {
        let eventKey = ev.key;
        if (ev.altKey) {
          eventKey = `Alt+${eventKey}`;
        }
        if (ev.ctrlKey) {
          eventKey = `Control+${eventKey}`;
        }
        if (ev.metaKey) {
          eventKey = `Meta+${eventKey}`;
        }
        if (eventKey === key) {
          if (shortcut.preventDefault) {
            ev.preventDefault();
          }
          action(ev);
        }
      };
    }

    export function getParentFsPath(fsPath: string) {
      const match = /^(.*?)\/([^/]*)$/.exec(fsPath);
      return match ? match[1] : null;
    }

    export function fsToUrlPath(fsPath: string) {
      let segments = trimSlashes(fsPath).split("/");
      if (segments.length === 1 && segments[0] === "") {
        segments = [];
      }
      segments.unshift("root");
      return segments.join(":");
    }

    export function urlToFsPath(urlPath: string) {
      const segments = trimColons(urlPath).split(":");
      Iif (segments.length < 1 || segments[0] !== "root") {
        return null;
      }
      segments[0] = "";
      return segments.join("/");
    }

    export function fsPathFromAdminObservedPath(adminPath: string) {
      const base = stripTrailingSlashes($LEKTOR_CONFIG.site_root);
      if (adminPath.substr(0, base.length) !== base) {
        return null;
      }
      return "/" + trimSlashes(adminPath.substr(base.length));
    }