From ed08d8878a2b4986547a2fb8c028f68292d5d87a Mon Sep 17 00:00:00 2001 From: David Nolen Date: Tue, 21 Jul 2026 15:42:17 -0400 Subject: [PATCH 1/2] port CLJ-2949: add req! (throwing get) and tests Co-authored-by: Fogus --- src/main/cljs/cljs/core.cljs | 10 ++++++++++ src/test/cljs/cljs/core_test.cljs | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index 993a5d5fe..2d1e9c599 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -2072,6 +2072,16 @@ reduces them without incurring seq initialization" :else not-found) not-found))) +(def REQ_NOT_FOUND #js {}) + +(defn req! + "Like arity-2 'get', but throws if key not present." + [m k] + (let [v (get m k REQ_NOT_FOUND)] + (if (identical? REQ_NOT_FOUND v) + (throw (js/Error. (str_ "Missing required key: " k))) + v))) + (declare PersistentHashMap PersistentArrayMap MapEntry) (defn assoc diff --git a/src/test/cljs/cljs/core_test.cljs b/src/test/cljs/cljs/core_test.cljs index 8bff60d3b..933f87781 100644 --- a/src/test/cljs/cljs/core_test.cljs +++ b/src/test/cljs/cljs/core_test.cljs @@ -2012,4 +2012,13 @@ (deftest test-cljs-3415 (let [a 1 b 1] - (is (thrown? js/Error #{a b})))) \ No newline at end of file + (is (thrown? js/Error #{a b})))) + +(deftest test-req! + (let [m {:a 1, :b 2, :f nil, :g false, nil "nil"}] + (is (thrown? js/Error (req! m :e))) + (are [x y] (= x y) + (req! m :a) 1 + (req! m nil) "nil" + (req! m :b) 2 + (req! m :f) nil))) From a24845975eed4c2d5c80cfb9532dec6f3777d3d9 Mon Sep 17 00:00:00 2001 From: David Nolen Date: Tue, 21 Jul 2026 15:50:34 -0400 Subject: [PATCH 2/2] make REQ_NOT_FOUND private --- src/main/cljs/cljs/core.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index 2d1e9c599..f97d4c7bc 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -2072,7 +2072,7 @@ reduces them without incurring seq initialization" :else not-found) not-found))) -(def REQ_NOT_FOUND #js {}) +(def ^:private REQ_NOT_FOUND #js {}) (defn req! "Like arity-2 'get', but throws if key not present."