ClojureScript deep for JavaScript objects

I ran into the problem of comparing two JavaScript objects for deep equality in ClojureScript, because (= var1 var2 var3 ...) only works with Clojure / ClojureScript collections and numbers.

(= (js-obj "a" 1) (js-obj "a" 1)) ;; => false

While I was writing this question, I found a solution ... but I will ask and answer, maybe this will help others.

+4
source share
1 answer

Since the "=" function can compare ClojureScript collections, one of the closest solutions should be:

(= (js->clj (js-obj "a" 1)) (js->clj (js-obj "a" 1))) ;; => true

What is ugly and doesn't work with objects like:

(= (js->clj (js/THREE.Vector3. 10 20 30)) (js->clj (js/THREE.Vector3. 10 20 30))) ;; => false

The most reliable solution is the goog.equals method from Google Closing Library .

(ns my.name-space
  (:import goog.object)
  (:require [cljsjs.three]))

(.equals goog.object (js/THREE.Vector3. 10 20 30) (js/THREE.Vector3. 10 20 30))) ;; => true

Google Closure Library JavaScript.

+6

Source: https://habr.com/ru/post/1687680/


All Articles