The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Remove unused metabase.util.formatting.numbers and metabase.util.formatting.time (#63791)
Oleksandr Yakushev5880342225e2
diff --git a/shadow-cljs.edn b/shadow-cljs.ednindex 892a5c2e4d1..8e0a3b16950 100644--- a/shadow-cljs.edn+++ b/shadow-cljs.edn@@ -31,8 +31,6 @@metabase.util.devtoolsmetabase.util.formatting.constantsmetabase.util.formatting.date- metabase.util.formatting.numbers- metabase.util.formatting.timemetabase.util.timemetabase.xrays.domain-entities.queries.util]}diff --git a/src/metabase/lib/binning.cljc b/src/metabase/lib/binning.cljcindex d4f0b000c1b..4b1a75733b5 100644--- a/src/metabase/lib/binning.cljc+++ b/src/metabase/lib/binning.cljc@@ -13,7 +13,6 @@[metabase.lib.schema.metadata :as lib.schema.metadata][metabase.lib.util :as lib.util][metabase.util :as u]- [metabase.util.formatting.numbers :as fmt.num][metabase.util.i18n :as i18n][metabase.util.malli :as mu][metabase.util.performance :refer [mapv select-keys]]))@@ -116,6 +115,14 @@{:display-name (i18n/tru "Bin every 0.01 degrees") :mbql {:strategy :bin-width :bin-width 0.01}}{:display-name (i18n/tru "Bin every 0.005 degrees") :mbql {:strategy :bin-width :bin-width 0.005}}]))+(defn- format-number-simple+ "Format a number for display in binning display names. Returns integers as whole numbers (no decimal point), and+ decimals as-is. The limited configurability is sufficient for the usecase where this function is invoked."+ [n]+ (str (if (zero? (rem n 1))+ (int n)+ n)))+(mu/defn binning-display-name :- ::lib.schema.common/non-blank-string"This is implemented outside of [[lib.metadata.calculation/display-name]] because it needs access to the field type.It's called directly by `:field` or `:metadata/column`'s [[lib.metadata.calculation/display-name]]."@@ -129,7 +136,7 @@(and (map? x) (= :metadata/column (:lib/type x))) :semantic-type)](case strategy:num-bins (i18n/trun "{0} bin" "{0} bins" num-bins)- :bin-width (str (fmt.num/format-number bin-width {})+ :bin-width (str (format-number-simple bin-width)(when (isa? semantic-type :type/Coordinate)"°")):default (i18n/tru "Auto binned")))))diff --git a/src/metabase/util/formatting/numbers.cljc b/src/metabase/util/formatting/numbers.cljcdeleted file mode 100644index 268ccc84742..00000000000--- a/src/metabase/util/formatting/numbers.cljc+++ /dev/null@@ -1,120 +0,0 @@-(ns metabase.util.formatting.numbers- (:require- [metabase.util :as u]- [metabase.util.formatting.internal.numbers :as internal]- [metabase.util.formatting.internal.numbers-core :as core]))--(declare format-number)--(def compact-currency-options- "Extra defaults that are mixed in when formatted a currency value in compact mode."- {:currency-style "symbol"})--#?(:cljs- (def ^:export compact-currency-options-js- "Extra defaults that are mixed in when formatted a currency value in compact mode."- (clj->js compact-currency-options)))--;; Compact form ===================================================================================================-(def ^:private display-compact-decimals-cutoff 1000)--(def ^:private humanized-powers- [[1000000000000 "T"]- [1000000000 "B"]- [1000000 "M"]- [1000 "k"]])--(defn- format-number-compact-basic [number options]- (let [options (dissoc options :compact :number-style)- abs-value (abs number)]- (cond- (zero? number) "0"- (< abs-value display-compact-decimals-cutoff) (format-number number options)- :else (let [[power suffix] (first (filter #(>= abs-value (first %)) humanized-powers))]- (str (format-number (/ number power)- (merge options {:minimum-fraction-digits 1 :maximum-fraction-digits 1}))- suffix)))))--(defmulti ^:private format-number-compact*- {:arglists '([number options])}- (fn [_ {:keys [number-style]}] number-style))--(defmethod format-number-compact* :default [number options]- (format-number-compact-basic number options))--(defmethod format-number-compact* "percent" [number options]- (str (format-number-compact-basic (* 100 number) options) "%"))--(defmethod format-number-compact* "currency" [number options]- (let [options (merge options compact-currency-options)- formatter (internal/number-formatter-for-options options)]- (if (< (abs number) display-compact-decimals-cutoff)- (core/format-number-basic formatter number)- (core/wrap-currency formatter (format-number-compact-basic number options)))))--(defmethod format-number-compact* "scientific" [number options]- (internal/format-number-scientific number (merge options {:maximum-fraction-digits 1 :minimum-fraction-digits 1})))--(defn- format-number-compact [number options]- (format-number-compact* number (-> options- (dissoc :compact)- core/prep-options)))--;; High-level =====================================================================================================-(defn- format-number-standard [number options]- (let [options (core/prep-options options)- nf (cond- (:number-formatter options) (:number-formatter options)-- ;; Hacky special case inherited from the TS version - to match classic behavior for small numbers,- ;; treat maximum-fraction-digits as maximum-significant-digits instead.- ;; "Small" means |x| < 1, or < 1% for percentages.- (and (not (:decimals options))- (not (:minimum-fraction-digits options))- (not= (:number-style options) "currency")- (< (abs number)- (if (= (:number-style options) "percent")- 0.01- 1)))- (-> options- (dissoc :maximum-fraction-digits)- (assoc :maximum-significant-digits (max 2 (:minimum-significant-digits options 0)))- internal/number-formatter-for-options)-- :else (internal/number-formatter-for-options options))]- (core/format-number-basic nf number)))--(defn ^:export format-number- "Formats a number according to a map of options.- The options:- - `:compact` boolean: Set true for human-readable contractions like $2.4M rather than $2,413,326.98.- - `:currency` string: The ISO currency code, eg. USD, RMB, EUR. **Required** when `:number-style \"currency\"`.- - `:currency-style` \"symbol\" | \"code\" | \"name\": Sets how the currency unit is displayed. Default is \"symbol\".- - `:maximum-fraction-digits` number: Show at most this many decimal places. Default 2.- - `:minimum-fraction-digits` number: Show at least this many decimal places. Default 0, or 2 for currencies.- - `:minimum-integer-digits` number: Show at least this many integer digits. Default 1.- - `:maximum-significant-digits` number: Show at most this many significant figures. Default not set; no extra rounding.- - `:minimum-significant-digits` number: Show at least this many significant figures. Default not set; no padding.- - `:negative-in-parentheses` boolean: True wraps negative values in parentheses; false (the default) uses minus signs.- - `:number-serpators` string: A two-character string \"ab\" where `a` is the decimal symbol and `b` is the grouping.- Default is American-style \".,\".- - `:number-style` \"currency\" | \"decimal\" | \"scientific\" | \"percent\": The fundamental type to display.- - \"currency\" renders as eg. \"$123.45\" based on the `:currency` value.- - \"percent\" renders eg. 0.432 as \"43.2%\".- - \"scientific\" renders in scientific notation with 1 integer digit: eg. 0.00432 as \"4.32e-3\".- - \"decimal\" (the default) is basic numeric notation.- - `:scale` number: Gives a factor by which to multiply the value before rendering it."- [number options]- (let [{:keys [compact negative-in-parentheses number-style scale] :as options} (u/normalize-map options)]- (cond- (and scale (not (NaN? scale))) (format-number (* scale number) (dissoc options :scale))-- (and (neg? number)- negative-in-parentheses) (str "("- (format-number (- number) (assoc options :negative-in-parentheses false))- ")")-- compact (format-number-compact number options)- (= (keyword number-style)- :scientific) (internal/format-number-scientific number options)- :else (format-number-standard number options))))diff --git a/src/metabase/util/formatting/time.cljc b/src/metabase/util/formatting/time.cljcdeleted file mode 100644index 79aac08ce2e..00000000000--- a/src/metabase/util/formatting/time.cljc+++ /dev/null@@ -1,29 +0,0 @@-(ns metabase.util.formatting.time- "Formatters for time values without date information."- (:require- [metabase.util.formatting.date :as date]- [metabase.util.formatting.internal.date-options :as options]- [metabase.util.time :as u.time])- #?(:clj- (:import- [java.time.format DateTimeFormatter FormatStyle])))--;;; ------------------------------------------------- Format Time ----------------------------------------------------(defn ^:export format-time- "Formats a give time (an hour number, a local time string, or a platform-specific local time object) in the- idiomatic style for this locale.-- For example, `\"7:45 PM\"` in English, `\"19h45\"` in French."- [value]- (let [t (u.time/coerce-to-time value)]- ;; Uses localized time formatting.- (when (u.time/valid? t)- #?(:cljs (.format t "LT")- :clj (.format (DateTimeFormatter/ofLocalizedTime FormatStyle/SHORT) t)))))--(defn ^:export format-time-with-unit- "Formats the given time (as a string or platform-specific local time or datetime object) based on the `:unit` and- other options, as is done for date formatting."- [value options]- (let [options (-> options options/prepare-options (assoc :date-enabled false))]- (date/format-datetime-with-unit value options)))diff --git a/test/metabase/util/formatting/numbers_test.cljc b/test/metabase/util/formatting/numbers_test.cljcdeleted file mode 100644index 152e53f5775..00000000000--- a/test/metabase/util/formatting/numbers_test.cljc+++ /dev/null@@ -1,275 +0,0 @@-(ns metabase.util.formatting.numbers-test- (:require- [clojure.test :refer [are deftest is testing]]- [metabase.util.formatting.numbers :as numbers]))--(deftest ^:parallel basics-test- (testing "format-number basics"- (are [s n] (= s (numbers/format-number n {}))- "0" 0- "1" 1- "-1" -1))- (testing "large positive and negative integers"- (are [s n] (= s (numbers/format-number n {}))- "10" 10- "99,999,999" 99999999- "-10" -10- "-99,999,999" -99999999)-- (testing "with non-default number separators"- (are [s n] (= s (numbers/format-number n {:number-separators ",."}))- "10,1" 10.1- "99.999.999,9" 99999999.9- "-10,1" -10.1- "-99.999.999,9" -99999999.9)- (are [s n] (= s (numbers/format-number n {:number-separators ". "}))- "9 001" 9001- "-9 001" -9001- "123 456.79" 123456.789)- (are [s n] (= s (numbers/format-number n {:number-separators "."}))- "9001" 9001- "-9001" -9001- "123456.79" 123456.789)))-- (testing "defaults to two significant figures for |n| < 1"- (are [s n] (= s (numbers/format-number n {}))- "0.33" (/ 1 3)- "-0.33" (- (/ 1 3))- "0.000033" (/ 0.0001 3)- "0.1" 0.1- "0.11" 0.11- "0.11" 0.111- "0.01" 0.01- "0.011" 0.011- "0.011" 0.0111- "1.1" 1.1- "1.11" 1.11- "1.11" 1.111- "111.11" 111.11))-- (testing "defaults to two decimal places for |n| >= 1"- (are [s n] (= s (numbers/format-number n {}))- "1.01" 1.01- "1.01" 1.011- "1.01" 1.0111)))--(deftest ^:parallel negative-in-parentheses-test- (are [s n] (= s (numbers/format-number n {:negative-in-parentheses true}))- "7" 7- "(4)" -4- "0" 0))--(deftest ^:parallel compact-mode-test- (testing "zero"- (is (= "0" (numbers/format-number 0 {:compact true}))))-- (testing "small numbers are not truncated to 0"- (are [s n] (= s (numbers/format-number n {:compact true}))- "0.1" 0.1- "-0.1" -0.1- "0.01" 0.01- "-0.01" -0.01))-- (testing "should round up and down"- (are [s n] (= s (numbers/format-number n {:compact true}))- "0.12" 0.123- "0.13" 0.127- "-0.12" -0.123- "-0.13" -0.127))-- (testing "large numbers get metric suffixes and exactly 1 decimal place"- (are [s n] (= s (numbers/format-number n {:compact true}))- "2.0k" 2000- "2.1k" 2100- "2.0k" 2049- "2.1k" 2050- "2.1k" 2051- "2.1k" 2100- "2.1k" 2149- "2.2k" 2150- "-2.6M" -2614519- "19.2M" 19247821- "-2.6B" -2614519000- "19.2B" 19247821000- "-2.6T" -2614519000000- "19.2T" 19247821000000))-- (testing "percentages"- (are [s n] (= s (numbers/format-number n {:compact true :number-style "percent"}))- "86.7%" 0.867- "123.45%" 1.2345- "123.46%" 1.234567- "0%" 0- "0.1%" 0.001- "0.01%" 0.0001- "0.12%" 0.001234- "10%" 0.1- "12.34%" 0.1234- "1.9%" 0.019- "2.1%" 0.021- "1.1k%" 11.11- "-22%" -0.22))-- (testing "scientific notation"- (are [s n] (= s (numbers/format-number n {:compact true :number-style "scientific"}))- "0.0e+0" 0- "1.0e-4" 0.0001- "1.0e-2" 0.01- "5.0e-1" 0.5- "1.2e+5" 123456.78- "-1.2e+5" -123456.78)-- (testing "with specified decimal places"- (are [s n mn mx] (= s (numbers/format-number n {:number-style "scientific"- :minimum-fraction-digits mn- :maximum-fraction-digits mx}))- "0.0e+0" 0 1 1- "1.00e-4" 0.0001 2 4- "1e-2" 0.01 0 1- "5.440e-1" 0.544 3 3 ; extra- "5.44e-1" 0.544 2 2 ; exact- "5.44e-1" 0.544 1 2 ; in range- "5.4e-1" 0.544 1 1 ; short, rounds down- "5.5e-1" 0.545 1 1 ; short, rounds up- "-1.235e+5" -123456.78 0 3 ; halves are rounded "up", away from 0.- "-1.2e+5" -123456.78 0 1))-- (testing "with custom separators"- (are [s n] (= s (numbers/format-number n {:compact true- :number-style "scientific"- :number-separators ",."}))- "0,0e+0" 0- "1,0e-4" 0.0001- "1,0e-2" 0.01- "5,0e-1" 0.5- "1,2e+5" 123456.78- "-1,2e+5" -123456.78)))-- (testing "currency values"- (are [s n c] (= s (numbers/format-number n {:compact true- :number-style "currency"- :currency c- :locale "en"}))- "$0.00" 0 "USD"- "$0.00" 0.001 "USD"- "$0.01" 0.007 "USD"- "$7.24" 7.24 "USD"- "$7.25" 7.249 "USD"- "$724.90" 724.90 "USD"- "$1.2k" 1234.56 "USD"- "$1.2M" 1234567.89 "USD"- "$-1.2M" -1234567.89 "USD"- "CN¥1.2M" 1234567.89 "CNY")))--(deftest ^:parallel currency-test- (are [s n c] (= s (numbers/format-number n {:number-style "currency" :currency c :locale "en"}))- "$1.23" 1.23 "USD"- "-$1.23" -1.23 "USD"- "$0.00" 0 "USD"- "$0.00" 0.001 "USD"- "$0.01" 0.007 "USD"- "$7.24" 7.24 "USD"- "$7.25" 7.245 "USD"- "$7.25" 7.249 "USD"- "$7.26" 7.255 "USD"- "$724.90" 724.90 "USD"- "$1,234.56" 1234.56 "USD"- "$1,234,567.89" 1234567.89 "USD"- "-$1,234,567.89" -1234567.89 "USD"- "₿6.34527873" 6.345278729 "BTC" ;; BTC is not natively supported, but we fix the symbols. 8 decimal places!- "¥1,234,568" 1234567.89 "JPY" ;; 0 decimal places for JPY.- "CN¥1,234,567.89" 1234567.89 "CNY")-- (testing "by name"- (let [labels {"USD" "US dollars"- ;; TODO Override this in the JS side? This is the only spot where the names from Intl.NumberFormat and- ;; metabase.util.currency differ.- "BTC" #?(:clj "Bitcoins" :cljs "BTC")- "CNY" "Chinese yuan"- "JPY" "Japanese yen"}]- (are [s n c] (= (str s " " (get labels c))- (numbers/format-number n {:number-style "currency" :currency c :currency-style "name"}))- "1.23" 1.23 "USD"- "-1.23" -1.23 "USD"- "0.00" 0 "USD"- "0.00" 0.001 "USD"- "0.01" 0.007 "USD"- "7.24" 7.24 "USD"- "7.25" 7.245 "USD"- "7.25" 7.249 "USD"- "7.26" 7.255 "USD"- "724.90" 724.90 "USD"- "1,234.56" 1234.56 "USD"- "1,234,567.89" 1234567.89 "USD"- "-1,234,567.89" -1234567.89 "USD"- "6.34527298" 6.345272982 "BTC"- "1,234,567.89" 1234567.89 "CNY"- "1,234,568" 1234567.89 "JPY")))-- (testing "by code"- (are [s n c] (= s (numbers/format-number n {:number-style "currency" :currency c :currency-style "code"}))- ;; NB After the currency symbol is a UTF-8 $00a0, a non-breaking space.- "USD\u00a01.23" 1.23 "USD"- "-USD\u00a01.23" -1.23 "USD"- "USD\u00a00.00" 0 "USD"- "USD\u00a00.00" 0.001 "USD"- "USD\u00a00.01" 0.007 "USD"- "USD\u00a07.24" 7.24 "USD"- "USD\u00a07.25" 7.245 "USD"- "USD\u00a07.25" 7.249 "USD"- "USD\u00a07.26" 7.255 "USD"- "USD\u00a0724.90" 724.90 "USD"- "USD\u00a01,234.56" 1234.56 "USD"- "USD\u00a01,234,567.89" 1234567.89 "USD"- "-USD\u00a01,234,567.89" -1234567.89 "USD"- "BTC\u00a06.34527192" 6.345271916 "BTC"- "CNY\u00a01,234,567.89" 1234567.89 "CNY"- "JPY\u00a01,234,568" 1234567.89 "JPY")))--(deftest ^:parallel scientific-test- (testing "defaults to 0-2 decimal places if not specified"- (are [s n] (= s (numbers/format-number n {:number-style "scientific"}))- "0e+0" 0- "1e-4" 0.0001- "1e-2" 0.01- "5e-1" 0.5- "5.4e-1" 0.54- "5.5e-1" 0.55- "1.23e+5" 123456.78- "-1.23e+5" -123456.78))-- (testing "with specified decimal places"- (are [s n mn mx] (= s (numbers/format-number n {:number-style "scientific"- :minimum-fraction-digits mn- :maximum-fraction-digits mx}))- "0.0e+0" 0 1 1- "1.00e-4" 0.0001 2 4- "1e-2" 0.01 0 1- "5.440e-1" 0.544 3 3 ; extra- "5.44e-1" 0.544 2 2 ; exact- "5.44e-1" 0.544 1 2 ; in range- "5.4e-1" 0.544 1 1 ; short, rounds down- "5.5e-1" 0.545 1 1 ; short, rounds up- "-1.235e+5" -123456.78 0 3 ; halves are rounded "up", away from 0.- "-1.2e+5" -123456.78 0 1))-- (testing "with custom separators"- (are [s n mn mx] (= s (numbers/format-number n {:number-style "scientific"- :number-separators ",."- :minimum-fraction-digits mn- :maximum-fraction-digits mx}))- "0,0e+0" 0 1 1- "1,00e-4" 0.0001 2 4- "1e-2" 0.01 0 1- "5,440e-1" 0.544 3 3 ; extra- "5,44e-1" 0.544 2 2 ; exact- "5,44e-1" 0.544 1 2 ; in range- "5,4e-1" 0.544 1 1 ; short, rounds down- "5,5e-1" 0.545 1 1 ; short, rounds up- "-1,235e+5" -123456.78 0 3 ; halves are rounded "up", away from 0.- "-1,2e+5" -123456.78 0 1)))--;; TODO(braden) Compact currency shows negatives as $-xM; regular currency as -$12.34. Do we care?-;; TODO(braden) Rounding is set to half-up, not half-even. Do we care? Hopefully not, since JavaScript's-;; Intl.NumberFormat has a roundingMode option but it's only lately proposed and not supported anywhere.-;; JS defaults to half-up/half-expand, so we force Java to do the same.diff --git a/test/metabase/util/formatting/time_test.cljc b/test/metabase/util/formatting/time_test.cljcdeleted file mode 100644index 825dcd9c5a4..00000000000--- a/test/metabase/util/formatting/time_test.cljc+++ /dev/null@@ -1,28 +0,0 @@-(ns metabase.util.formatting.time-test- (:require- [clojure.string :as str]- [clojure.test :refer [are deftest]]- [metabase.util.formatting.time :as time]))--(deftest format-time-test- ;; some JVMs use non-breaking space (nbsp) in their formatted strings- ;; which can cause confusing looking test failures.- ;; A string replace normalizes on the ascii space char- (are [exp input] (= exp (str/replace (time/format-time input) \u202f \space))- "1:02 AM" "01:02:03.456+07:00"- "1:02 AM" "01:02"- "10:29 PM" "22:29:59.26816+01:00"- "10:29 PM" "22:29:59.412459+01:00"- "7:14 PM" "19:14:42.926221+01:00"- "7:14 PM" "19:14:42.13202+01:00"- "1:38 PM" "13:38:58.987352+01:00"- "1:38 PM" "13:38:58.001001+01:00"- "5:01 PM" "17:01:23+01:00"))--(deftest format-time-with-unit- (are [exp input opts] (= exp (time/format-time-with-unit input (merge {:unit "hour-of-day"} opts)))- "8:00 AM" 8 nil- ;; 24-hour- "08:00" 8 {:time-style "HH:mm"}- "18:00" 18 {:time-style "HH:mm"}- "18:00:00" 18 {:time-style "HH:mm" :time-enabled "seconds"}))