Преглед на файлове

feat: support locale sensitive string comparisons for table sorting (#6892)

* feat: support locale sensitive string comparisons for table sorting

* refac: code review

* refac: add tests
Seth Yuan преди 3 години
родител
ревизия
bc7d37d43e
променени са 2 файла, в които са добавени 17 реда и са изтрити 2 реда
  1. 8 1
      src/main/frontend/components/query_table.cljs
  2. 9 1
      src/test/frontend/components/query_table_test.cljs

+ 8 - 1
src/main/frontend/components/query_table.cljs

@@ -39,9 +39,16 @@
     (:block/name item)
     (get-in item [:block/properties sort-by-column])))
 
+(defn- locale-compare
+  "Use locale specific comparison for strings and general comparison for others."
+  [x y]
+  (if (and (string? x) (string? y))
+    (.localeCompare x y (.language js/navigator))
+    (< x y)))
+
 (defn- sort-result [result {:keys [sort-by-column sort-desc?]}]
   (if (some? sort-by-column)
-    (let [comp (if sort-desc? > <)]
+    (let [comp (if sort-desc? #(locale-compare %2 %1) locale-compare)]
       (sort-by (fn [item]
                  (block/normalize-block (sort-by-fn sort-by-column item)))
                comp

+ 9 - 1
src/test/frontend/components/query_table_test.cljs

@@ -49,4 +49,12 @@
 
          {:sort-desc? false :sort-by-column :title}
          [{:title "abc"} {:title "cde"}]
-         [{:title "abc"} {:title "cde"}])))
+         [{:title "abc"} {:title "cde"}]
+
+         {:sort-desc? true :sort-by-column :title}
+         [{:title "意志"} {:title "圆圈"}]
+         [{:title "圆圈"} {:title "意志"}]
+
+         {:sort-desc? false :sort-by-column :title}
+         [{:title "圆圈"} {:title "意志"}]
+         [{:title "意志"} {:title "圆圈"}])))