Skip to content

Commit

Permalink
[fix] [#81] [Experimental] Improve React Native oget support
Browse files Browse the repository at this point in the history
Apparently it seems that RN may throw on `(gobj/get nil "key")`?
I'm not familiar with RN, so not certain why that might be.

This commit adds a small change to protect the two `oget` fns against
possible nils.

Hopefully this resolves the issue, though I'll need confirmation from
someone using React Native.
  • Loading branch information
ptaoussanis committed Dec 30, 2024
1 parent efc16c3 commit 4f36472
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/taoensso/encore.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2344,9 +2344,9 @@

#?(:cljs
(defn oget "Like `get` for JS objects."
([ k ] (gobj/get js-?window (name k)))
([o k ] (gobj/get o (name k) nil))
([o k not-found] (gobj/get o (name k) not-found))))
([ k ] (when-let [o js-?window] (gobj/get o (name k))))
([o k ] (when o (gobj/get o (name k) nil)))
([o k not-found] (if o (gobj/get o (name k) not-found) not-found))))

#?(:cljs
(let [sentinel (js-obj)]
Expand All @@ -2355,13 +2355,15 @@
([ ks ] (oget-in js-?window ks nil))
([o ks ] (oget-in o ks nil))
([o ks not-found]
(loop [o o, ks (seq ks)]
(if ks
(let [o (gobj/get o (name (first ks)) sentinel)]
(if (identical? o sentinel)
not-found
(recur o (next ks))))
o))))))
(if o
(loop [o o, ks (seq ks)]
(if ks
(let [o (gobj/get o (name (first ks)) sentinel)]
(if (identical? o sentinel)
not-found
(recur o (next ks))))
o))
not-found)))))

(defn get1
"Like `get` but returns val for first key that exists in map.
Expand Down

0 comments on commit 4f36472

Please sign in to comment.