File tree 2 files changed +13
-0
lines changed
2 files changed +13
-0
lines changed Original file line number Diff line number Diff line change 53
53
m
54
54
(persistent! acc))))))
55
55
56
+ (defn assoc-in-some
57
+ " Associates a value v in a nested associative structure provided that v is not nil."
58
+ [m k v]
59
+ (cond-> m (some? v) (assoc-in k v)))
60
+
56
61
(defn update-existing
57
62
" Updates a value in a map given a key and a function, if and only if the key
58
63
exists in the map. See: `clojure.core/update`."
Original file line number Diff line number Diff line change 33
33
(is (nil? (m/assoc-some nil :a nil )))
34
34
(is (nil? (m/assoc-some nil :a nil :b nil ))))
35
35
36
+ (deftest test-assoc-in-some
37
+ (is (= (m/assoc-in-some {:a 1 :b {:c 2 }} [:b ] 3 ) {:a 1 :b 3 }))
38
+ (is (= (m/assoc-in-some [{:a 1 } {:a 2 }] [1 :a ] 3 ) [{:a 1 } {:a 3 }]))
39
+ (is (= (m/assoc-in-some [{:a 1 } {:a 2 }] [1 :a ] false ) [{:a 1 } {:a false }]))
40
+ (is (= (m/assoc-in-some [{:a 1 } {:a 2 }] [1 :a ] nil ) [{:a 1 } {:a 2 }]))
41
+ (is (nil? (m/assoc-in-some nil :a nil )))
42
+ (is (nil? (m/assoc-in-some nil [:a :b ] nil ))))
43
+
36
44
(deftest test-update-existing
37
45
(is (= (m/update-existing {:a 1 } :a inc) {:a 2 }))
38
46
(is (= (m/update-existing {:a 1 :b 2 } :a inc) {:a 2 :b 2 }))
You can’t perform that action at this time.
0 commit comments