diff --git a/src/main/frontend/components/query_table.cljs b/src/main/frontend/components/query_table.cljs index 127e790c0b..7e750cab81 100644 --- a/src/main/frontend/components/query_table.cljs +++ b/src/main/frontend/components/query_table.cljs @@ -110,7 +110,7 @@ keys)) desc? (desc? *desc? p-desc?) result (sort-result-by (fn [item] - (sort-by-fn sort-by-item item)) + (block/normalize-block (sort-by-fn sort-by-item item))) desc? result)] [:div.overflow-x-auto {:on-mouse-down (fn [e] (.stopPropagation e)) diff --git a/src/main/frontend/format/block.cljs b/src/main/frontend/format/block.cljs index 0ddc451973..78635c199f 100644 --- a/src/main/frontend/format/block.cljs +++ b/src/main/frontend/format/block.cljs @@ -1,16 +1,18 @@ (ns frontend.format.block "Block code needed by app but not graph-parser" - (:require [clojure.string :as string] - [logseq.graph-parser.block :as gp-block] + (:require ["@sentry/react" :as Sentry] + [cljs-time.format :as tf] + [clojure.string :as string] [frontend.config :as config] + [frontend.date :as date] [frontend.db :as db] [frontend.format :as format] - [frontend.state :as state] [frontend.handler.notification :as notification] - ["@sentry/react" :as Sentry] + [frontend.state :as state] + [logseq.graph-parser.block :as gp-block] [logseq.graph-parser.config :as gp-config] - [logseq.graph-parser.property :as gp-property] - [logseq.graph-parser.mldoc :as gp-mldoc])) + [logseq.graph-parser.mldoc :as gp-mldoc] + [logseq.graph-parser.property :as gp-property])) (defn extract-blocks "Wrapper around logseq.graph-parser.block/extract-blocks that adds in system state @@ -36,6 +38,29 @@ and handles unexpected failure." ([original-page-name with-id? with-timestamp?] (gp-block/page-name->map original-page-name with-id? (db/get-db (state/get-current-repo)) with-timestamp? (state/get-date-formatter)))) +(defn- normalize-as-percentage + ([block] + (some->> block + str + (re-matches #"(-?\d+\.?\d*)%") + second + (#(/ % 100))))) + +(defn- normalize-as-date + ([block] + (some->> block + str + date/valid? + (tf/unparse date/custom-formatter)))) + +(defn normalize-block + "Normalizes supported formats such as dates and percentages." + ([block] + (->> [normalize-as-percentage normalize-as-date identity] + (map #(% block)) + (remove nil?) + (first)))) + (defn parse-block ([block] (parse-block block nil)) diff --git a/src/test/frontend/format/block_test.cljs b/src/test/frontend/format/block_test.cljs new file mode 100644 index 0000000000..b1b48ce364 --- /dev/null +++ b/src/test/frontend/format/block_test.cljs @@ -0,0 +1,42 @@ +(ns frontend.format.block-test + (:require [cljs.test :refer [deftest testing are]] + [frontend.format.block :as block])) + +(deftest test-normalize-date + (testing "normalize date values" + (are [x y] (= (block/normalize-block x) y) + "Aug 12th, 2022" + "2022-08-12T00:00:00Z" + + "2022-08-12T00:00:00Z" + "2022-08-12T00:00:00Z"))) + +(deftest test-normalize-percentage + (testing "normalize percentages" + (are [x y] (= (block/normalize-block x) y) + "50%" + 0.5 + + "0%" + 0 + + "-5%" + -0.05))) + +(deftest test-random-values + (testing "random values should not be processed" + (are [x y] (= (block/normalize-block x) y) + "anreanre" + "anreanre" + + "" + "" + + "a.0%" + "a.0%" + + "%" + "%" + + "-%" + "-%")))