Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,16 @@ reduces them without incurring seq initialization"
:else not-found)
not-found)))

(def ^:private 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
Expand Down
11 changes: 10 additions & 1 deletion src/test/cljs/cljs/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,4 +2012,13 @@

(deftest test-cljs-3415
(let [a 1 b 1]
(is (thrown? js/Error #{a b}))))
(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)))
Loading