Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exists? in CLJS #626

Closed
borkdude opened this issue Oct 14, 2021 · 0 comments
Closed

Add exists? in CLJS #626

borkdude opened this issue Oct 14, 2021 · 0 comments

Comments

@borkdude
Copy link
Collaborator

Rudimentary version, cljs.analyzer should be replaced with SCI specifics.

(defn ^:macro exists?*
  "Return true if argument exists, analogous to usage of typeof operator
   in JavaScript."
  [_ _ x]
  (if (symbol? x)
    (let [x     (cond-> (str x) #_(:name (cljs.analyzer/resolve-var &env x))
                  (= "js" (namespace x)) name)
          segs  (str/split (str (str/replace (str x) "/" ".")) #"\.")
          n     (count segs)
          syms  (map
                 #(vary-meta (symbol "js" (str/join "." %))
                             assoc :cljs.analyzer/no-resolve true)
                 (reverse (take n (iterate butlast segs))))
          js    (str/join " && " (repeat n "(typeof ~{} !== 'undefined')"))]
      (list 'boolean (concat (list 'js* js) syms)))
    `(some? ~x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant