about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorajs124 <git@ajs124.de>2022-06-12 14:09:42 +0200
committerDaniel Olsen <daniel.olsen99@gmail.com>2022-10-20 20:12:15 +0200
commit4284ac9dfb118097eb9e2d5f27e11208f2e715e4 (patch)
tree2abceb7064a3fe65cfd444a5c1cb0658f571b247 /lib
parent4c420ee4854d8b0a9d0d224588f0cd033f6b4381 (diff)
lib.strings: Add normalizePath
dedupes extranous /'s in filepaths

Co-authored-by: Daniel Olsen <daniel.olsen99@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/strings.nix10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index b12dac54f4ff3..be217cb064697 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -185,6 +185,16 @@ rec {
   */
   makeBinPath = makeSearchPathOutput "bin" "bin";
 
+  /* Normalize path, removing extranous /s
+
+     Type: normalizePath :: string -> string
+
+     Example:
+       normalizePath "/a//b///c/"
+       => "/a/b/c/"
+  */
+  normalizePath = s: (builtins.foldl' (x: y: if y == "/" && hasSuffix "/" x then x else x+y) "" (splitString "" s));
+
   /* Depending on the boolean `cond', return either the given string
      or the empty string. Useful to concatenate against a bigger string.