mirror of
https://github.com/logseq/logseq.git
synced 2026-05-05 19:36:35 +00:00
* fix: Date sorting nonsensical * simplify normalize-block * adjusted handling of sets and added tests Co-authored-by: Junyi Du <junyidu.cn@gmail.com>
This commit is contained in:
@@ -110,7 +110,7 @@
|
|||||||
keys))
|
keys))
|
||||||
desc? (desc? *desc? p-desc?)
|
desc? (desc? *desc? p-desc?)
|
||||||
result (sort-result-by (fn [item]
|
result (sort-result-by (fn [item]
|
||||||
(sort-by-fn sort-by-item item))
|
(block/normalize-block (sort-by-fn sort-by-item item)))
|
||||||
desc?
|
desc?
|
||||||
result)]
|
result)]
|
||||||
[:div.overflow-x-auto {:on-mouse-down (fn [e] (.stopPropagation e))
|
[:div.overflow-x-auto {:on-mouse-down (fn [e] (.stopPropagation e))
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
(ns frontend.format.block
|
(ns frontend.format.block
|
||||||
"Block code needed by app but not graph-parser"
|
"Block code needed by app but not graph-parser"
|
||||||
(:require [clojure.string :as string]
|
(:require ["@sentry/react" :as Sentry]
|
||||||
[logseq.graph-parser.block :as gp-block]
|
[cljs-time.format :as tf]
|
||||||
|
[clojure.string :as string]
|
||||||
[frontend.config :as config]
|
[frontend.config :as config]
|
||||||
|
[frontend.date :as date]
|
||||||
[frontend.db :as db]
|
[frontend.db :as db]
|
||||||
[frontend.format :as format]
|
[frontend.format :as format]
|
||||||
[frontend.state :as state]
|
|
||||||
[frontend.handler.notification :as notification]
|
[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.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
|
(defn extract-blocks
|
||||||
"Wrapper around logseq.graph-parser.block/extract-blocks that adds in system state
|
"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?]
|
([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))))
|
(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
|
(defn parse-block
|
||||||
([block]
|
([block]
|
||||||
(parse-block block nil))
|
(parse-block block nil))
|
||||||
|
|||||||
42
src/test/frontend/format/block_test.cljs
Normal file
42
src/test/frontend/format/block_test.cljs
Normal file
@@ -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%"
|
||||||
|
|
||||||
|
"%"
|
||||||
|
"%"
|
||||||
|
|
||||||
|
"-%"
|
||||||
|
"-%")))
|
||||||
Reference in New Issue
Block a user