atevm
atevm

Reputation: 821

ClojureScript deep equal for JavaScript objects

I've ran into the problem how to compare two JavaScript objects for deep equality in ClojureScript, because (= var1 var2 var3...) only works on Clojure/ClojureScript collections and numbers.

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

While I wrote this question I found the solution... but I'll ask and answer it maybe help others.

Upvotes: 4

Views: 694

Answers (1)

atevm
atevm

Reputation: 821

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

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

Which is ugly, and does not work on instanced objects like.:

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

The most reliable solution is using the goog.equals method from Google's Closure 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

In general Google's Closure Library has robust solutions for handling plain JavaScript.

Upvotes: 6

Related Questions