Skip to content

Instantly share code, notes, and snippets.

@dcmoore
Created February 6, 2014 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcmoore/8848185 to your computer and use it in GitHub Desktop.
Save dcmoore/8848185 to your computer and use it in GitHub Desktop.
;;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