application/octet-stream
•
1.52 KB
•
43 lines
;;; 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-of 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 [throw [list :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]
[string/cut path [+ 1 last-period] [buffer/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]
[string/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]
[string/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]
[string/cut path [inc last-slash]]
path]]