Skip to content

Commit

Permalink
router - fix setting a route key to itself doesn't drop the key anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
ggeoffrey committed Jan 23, 2024
1 parent 8535999 commit bf9d504
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/hyperfiddle/router.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,11 @@
The corresponding value is preserved."
;; Useful for HFQL and `demo-colors`
[m k f]
(-> (assoc m (f k) (get m k))
(dissoc k)))
(let [k' (f k)
m' (assoc m k' (get m k))]
(if (= k' k)
m'
(dissoc m' k))))

(defn set-key "Replaces `old-key` in map `m` by `new-key` without altering the corresponding value.
Similar to `clojure.set/rename-keys`, but faster and for a single key."
Expand All @@ -571,6 +574,7 @@
(tests
(set-key nil :a :b) := {:b nil}
(set-key {} :a :b) := {:b nil}
(set-key nil :a :a) := {:a nil}
(set-key {:a 1} :a :b) := {:b 1}
(set-key {:a 1, :b 2} :a :c) := {:c 1, :b 2}
)
Expand Down

0 comments on commit bf9d504

Please sign in to comment.