summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatus Goljer <dota.keys@gmail.com>2014-08-28 14:18:22 +0200
committerMatus Goljer <dota.keys@gmail.com>2014-08-28 15:49:28 +0200
commit2e85dede0b7bebe34e0d079c1e58d32f2a139d2a (patch)
tree8619385ebc001a56613386bc82ce258944305a9b
parentf9238c3bfd8ce29edc8f42d4949e8630916ef05b (diff)
Fix f-common-parent if input list has just one element
-rw-r--r--f.el20
-rw-r--r--test/f-paths-test.el7
2 files changed, 19 insertions, 8 deletions
diff --git a/f.el b/f.el
index 1d2599f..6d7a23c 100644
--- a/f.el
+++ b/f.el
@@ -93,14 +93,18 @@ If PATH is not allowed to be modified, throw error."
(defun f-common-parent (paths)
"Return the deepest common parent directory of PATHS."
- (let* ((paths (-map 'f-split paths))
- (common (caar paths))
- (re nil))
- (while (--all? (equal (car it) common) paths)
- (setq paths (-map 'cdr paths))
- (push common re)
- (setq common (caar paths)))
- (if re (concat (apply 'f-join (nreverse re)) "/") "")))
+ (cond
+ ((not paths) nil)
+ ((not (cdr paths)) (f-parent (car paths)))
+ (:otherwise
+ (let* ((paths (-map 'f-split paths))
+ (common (caar paths))
+ (re nil))
+ (while (--all? (equal (car it) common) paths)
+ (setq paths (-map 'cdr paths))
+ (push common re)
+ (setq common (caar paths)))
+ (if re (concat (apply 'f-join (nreverse re)) "/") "")))))
(defun f-ext (path)
"Return the file extension of PATH."
diff --git a/test/f-paths-test.el b/test/f-paths-test.el
index f20b98c..dc93728 100644
--- a/test/f-paths-test.el
+++ b/test/f-paths-test.el
@@ -152,6 +152,13 @@
(ert-deftest f-common-parent/no-common-parent ()
(should (equal (f-common-parent '("foo/bar/baz" "foo/bar/qux" "fo/bar/mux")) "")))
+(ert-deftest f-common-parent/single-file ()
+ (should (equal (f-common-parent '("foo/bar/baz")) "foo/bar/"))
+ (should (equal (f-common-parent '("baz")) "./")))
+
+(ert-deftest f-common-parent/empty-list ()
+ (should (equal (f-common-parent nil) nil)))
+
;;;; f-ext