Number helper functions that abstract differences between CLJ and CLJS.

(ns metabase.util.number
  (:refer-clojure :exclude [bigint integer?])
  (:require
   [clojure.core :as core]))

Coerce a number or a string to BigInt. Throws if coercion cannot be made.

(defn bigint
  [x]
  #?(:clj (core/bigint x)
     :cljs (js/BigInt x)))

Checks if the passed value is a BigInt instance.

(defn bigint?
  [x]
  #?(:clj (instance? clojure.lang.BigInt x)
     :cljs (= (type x) js/BigInt)))

Checks if the passed value is an integer. [[clojure.core/integer?]] does not take BigInt into account on CLJS.

(defn integer?
  [x]
  #?(:clj (core/integer? x)
     :cljs (core/or (core/integer? x) (bigint? x))))

Parses a string as a BigInt. If the string cannot be parsed, returns nil.

(defn parse-bigint
  [s]
  (when (re-matches #"[+-]?\d+" s)
    (bigint s)))