Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 Commits
nujel / stdlib / string / path.nuj
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Some functions for working with paths and file extensions

(defn path/ext?! (ext)
      "Return a predicate that checks if a path ends on EXT"
      (case (:type-name ext)
            (:string (fn (path)
                         (= ext (lower-case (path/extension path)))))
            (:pair (fn (path)
                       (def cext (lower-case (path/extension path)))
                     (reduce ext (fn (α β) (or α (= β cext))))))
            (otherwise (exception :type-error "Expected a :string or :list" ext))))


(defn path/extension (path)
      "Return the extension of PATH"
      (def last-period (:last-index-of path "."))
      (if (>= last-period 0)
          (:cut path (+ 1 last-period) (:length path))
          path))

(defn path/without-extension (path)
      "Return PATH, but without the extension part"
      (def last-period (:last-index-of path "."))
      (if (>= last-period 0)
          (:cut path 0 last-period)
          path))

(defn path/dirname (path)
      "Return the directory part of a PATH"
      (def last-slash (:last-index-of path "/"))
      (if (>= last-slash 0)
          (:cut path 0 last-slash)
          ""))

(defn path/basename (path)
      "Return the path without the directory part"
      (def last-slash (:last-index-of path "/"))
      (if (>= last-slash 0)
          (:cut path (inc last-slash))
          path))