Created
February 6, 2014 16:56
-
-
Save dcmoore/8848185 to your computer and use it in GitHub Desktop.
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
;;spec | |
(def hiccup [:key {} | |
[:nesting {} | |
[:match "first match"]] | |
[:nesting | |
[:match {} "second match"]] | |
[:miss ""]]) | |
(describe "get-hiccup-nodes" | |
(it "gets all of the matching nodes" | |
(should== | |
[[:match "first match"] [:match {} "second match"]] | |
(get-hiccup-nodes hiccup [:key :nesting :match])))) | |
;;src | |
(defn- body-extractor [hiccup-node] | |
(if (map? (second hiccup-node)) | |
(rest (rest hiccup-node)) | |
(rest hiccup-node))) | |
(defn- get-body-of-each-node [hiccup-nodes] | |
(apply concat (map body-extractor hiccup-nodes))) | |
(defn- filter-nodes-on-key [hiccup-nodes key-to-filter-on] | |
(filter #(= key-to-filter-on (first %)) hiccup-nodes)) | |
(defn get-hiccup-nodes [hiccup key-path] | |
(loop [hiccup-nodes [hiccup] | |
key-path key-path] | |
(if (or (empty? hiccup-nodes) (empty? key-path)) | |
hiccup-nodes | |
(let [filtered-nodes (filter-nodes-on-key hiccup-nodes (first key-path))] | |
(recur | |
(if (empty? (rest key-path)) | |
filtered-nodes | |
(get-body-of-each-node filtered-nodes)) | |
(rest key-path)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/skatenerd/homework/blob/3a05741384d7a05a5f1716f7431fd43d417da566/src/homework/core.clj