diff --git a/src/clj/com/rpl/specter.cljc b/src/clj/com/rpl/specter.cljc index 5a27480..618ec01 100644 --- a/src/clj/com/rpl/specter.cljc +++ b/src/clj/com/rpl/specter.cljc @@ -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."} diff --git a/test/com/rpl/specter/core_test.cljc b/test/com/rpl/specter/core_test.cljc index 9633ad6..ff51446 100644 --- a/test/com/rpl/specter/core_test.cljc +++ b/test/com/rpl/specter/core_test.cljc @@ -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)) + )