Added NAME and NAMESPACE navigators

This commit is contained in:
nathanmarz 2017-02-14 08:47:19 -05:00
parent 3dbc775334
commit 48efea55ab
2 changed files with 37 additions and 0 deletions

View file

@ -959,6 +959,32 @@
(transform* [this structure next-fn]
(with-meta structure (next-fn (meta structure)))))
(defnav ^{:doc "Navigates to the name portion of the keyword or symbol"}
NAME
[]
(select* [this structure next-fn]
(next-fn (name structure)))
(transform* [this structure next-fn]
(let [new-name (next-fn (name structure))
ns (namespace structure)]
(cond (keyword? structure) (keyword ns new-name)
(symbol? structure) (symbol ns new-name)
:else (i/throw-illegal "NAME can only be used on symbols or keywords - " structure)
))))
(defnav ^{:doc "Navigates to the name portion of the keyword or symbol"}
NAMESPACE
[]
(select* [this structure next-fn]
(next-fn (name structure)))
(transform* [this structure next-fn]
(let [name (name structure)
new-ns (next-fn (namespace structure))]
(cond (keyword? structure) (keyword new-ns name)
(symbol? structure) (symbol new-ns name)
:else (i/throw-illegal "NAMESPACE can only be used on symbols or keywords - " structure)
))))
(defdynamicnav
^{:doc "Adds the result of running select with the given path on the
current value to the collected vals."}

View file

@ -1408,3 +1408,14 @@
(s/selected? (collected? [n] (even? n))))
[4 2 3])))
)
(deftest name-namespace-test
(= :a (setval s/NAME "a" :e))
(= :a/b (setval s/NAME "b" :a/e))
(= 'a (setval s/NAME "a" 'e))
(= 'a/b (setval s/NAME "b" 'a/e))
(= :a/e (setval s/NAMESPACE "a" :e))
(= :a/e (setval s/NAMESPACE "a" :f/e))
(= 'a/e (setval s/NAMESPACE "a" 'e))
(= 'a/e (setval s/NAMESPACE "a" 'f/e))
)