Fork me on GitHub
#malli
<
2021-07-24
>
nivekuil22:07:58

is there a way to work with bigints in cljs? trying to coerce a string (reitit route) to one

nivekuil04:07:28

bigints are a headache with transit, just ended up using Long

(defn -string->bigint [x]
  (if (string? x)
    (try
      #?(:clj  (Long/parseLong x)
         :cljs (let [x' (js/parseInt x)]
                 (if (> x' js/Number.MAX_SAFE_INTEGER)
                   (goog.math.Long/fromString x 10)
                   x')))
      (catch #?(:clj Exception, :cljs js/Error) _ x))
    x))

(defn string-transformer []
  (mt/transformer
   {:name     :string
    :decoders (assoc (mt/-string-decoders)
                     :i64 -string->bigint)
    :encoders (assoc (mt/-string-encoders)
                     :i64 mt/-any->string)}))