query
On this page

urlToName

lib.sources.urlToName

Docs pulled from | This Revision | about 1 hour ago


Transform a URL (or path, or string) into a clean package name.


Noogle detected

Implementation

The following is the current implementation of this function.

urlToName =
    url:
    let
      inherit (lib.strings) stringLength;
      base = baseNameOf (lib.removeSuffix "/" (lib.last (lib.splitString ":" (toString url))));
      # chop away one git or archive-related extension
      removeExt =
        name:
        let
          matchExt = match "(.*)\\.(git|tar|zip|gz|tgz|bz|tbz|bz2|tbz2|lzma|txz|xz|zstd)$" name;
        in
        if matchExt != null then lib.head matchExt else name;
      # apply function f to string x while the result shrinks
      shrink =
        f: x:
        let
          v = f x;
        in
        if stringLength v < stringLength x then shrink f v else x;
    in
    shrink removeExt base;