Created
August 19, 2011 12:19
-
-
Save maxweber/1156682 to your computer and use it in GitHub Desktop.
pathWithComponents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns path.core | |
(:use clojure.test) | |
(:require [clojure.string :as s])) | |
(defn clean-component [component] | |
(s/replace component #"/+" "")) | |
(defn path-prefix [components] | |
(if (re-find #"/+" (first components)) | |
"/" | |
"")) | |
(defn pathWithComponents [components] | |
(apply str (path-prefix components) | |
(interpose "/" | |
(remove empty? | |
(map clean-component components))))) | |
(testing "pathWithComponents" | |
(is (= "Apple/Local/Library/Frameworks" (pathWithComponents ["Apple" "Local" "Library" "Frameworks"]))) | |
(is (= "/Apple/Local/Library/Frameworks" (pathWithComponents ["/" "Apple" "Local" "Library" "Frameworks"]))) | |
(is (= "/Apple/Local/Library/Frameworks" (pathWithComponents ["/" "Apple" "Local" "Library" "Frameworks/" "/"]))) | |
(is (= "/Apple/Local/Library/Frameworks" (pathWithComponents ["/" "Apple" "Local" "///////" "Library" "Frameworks" "/"])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment