Skip to content

Instantly share code, notes, and snippets.

@webmonarch
Last active December 3, 2022 06:18
Show Gist options
  • Save webmonarch/99c493f8367e44264e2e3712f72bf18e to your computer and use it in GitHub Desktop.
Save webmonarch/99c493f8367e44264e2e3712f72bf18e to your computer and use it in GitHub Desktop.
;; a compare function that will compare two Logseq block hash maps using "eric" logic
;;
;; sort by:
;; - NOW, DOING, ...
;; - A, B, NO PRIORITY, C
;; - Journal day (a journal page is "lower" than a page that is not a journal)
;;
;; You can run this script which will run through some test cases
(def compare-block
(fn [a b]
(let
[[taskA taskB] (map (comp (fn [a] (or a 0)) {"NOW" -1} :block/marker) [a b])
[priorityA priorityB] (map (comp (fn [a] (or a "BB")) :block/priority) [a b])
[dayA dayB] (map (comp (fn [a] (or a 30000000)) :block/journal-day :block/page) [a b])]
(cond
(not= taskA taskB)
(compare taskA taskB)
(not= priorityA priorityB)
(compare priorityA priorityB)
(every? some? [dayA dayB])
(compare dayA dayB)
:else
0)))
)
;; sort by:
;; - NOW, DOING, ...
;; - A, B, C
;; - Journal day (order is higher)
(defn testCompareBlock [title expected a b]
(let [actual (compare-block a b)]
(println
(if (= actual expected)
(format "%s %s" "✅" title)
(format "%s %s (expected %d, actual %d)" "🛑" title expected actual)))
)
)
(testCompareBlock "Empty" 0 {} {})
(testCompareBlock "NOW < LATER" -1 {:block/marker "NOW"} {:block/marker "LATER"})
(testCompareBlock "nil > NOW" 1 {} {:block/marker "NOW"})
(testCompareBlock "nil = LATER" 0 {} {:block/marker "LATER"})
(testCompareBlock "A < B" -1 {:block/priority "A"} {:block/marker "B"})
(testCompareBlock "A < nil" -1 {:block/priority "A"} {})
(testCompareBlock "B < nil" -1 {:block/priority "B"} {})
(testCompareBlock "C > nil" 1 {:block/priority "C"} {})
(testCompareBlock "20220101 > 20211231" 1 {:block/page {:block/journal-day 20220101}} {:block/page {:block/journal-day 20211231}})
(testCompareBlock "20220101 < nil" -1 {:block/page {:block/journal-day 20220101}} {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment