Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix `u/slugify` function's max-length option (#64256)

John Swanson4614a3797290
diff --git a/src/metabase/util.cljc b/src/metabase/util.cljc
index adcde25ee18..340bf7adbf0 100644
--- a/src/metabase/util.cljc
+++ b/src/metabase/util.cljc
@@ -496,11 +496,18 @@
(slugify s {}))
(^String [s {:keys [max-length unicode?]}]
(when (seq s)
- (let [slug (str/join (for [c (remove-diacritical-marks (lower-case-en s))]
- (slugify-char c (not unicode?))))]
- (if max-length
- (str/join (take max-length slug))
- slug)))))
+ (cond->> (remove-diacritical-marks (lower-case-en s))
+ true (map #(slugify-char % (not unicode?)))
+ max-length (reduce (fn [cur-slug next-slug]
+ (if (<= (+ (count cur-slug)
+ (if (char? next-slug)
+ 1
+ (count next-slug)))
+ max-length)
+ (str cur-slug next-slug)
+ (reduced cur-slug)))
+ "")
+ true str/join))))
(defn id
"If passed an integer ID, returns it. If passed a map containing an `:id` key, returns the value if it is an integer.
diff --git a/test/metabase/upload/impl_test.clj b/test/metabase/upload/impl_test.clj
index 0190ee7d81a..7fa55ab729f 100644
--- a/test/metabase/upload/impl_test.clj
+++ b/test/metabase/upload/impl_test.clj
@@ -2584,7 +2584,7 @@
(deftest unique-long-column-names-test
(let [original ["αbcdεf_αbcdεf" "αbcdεfg_αbcdεf" "αbc_2_etc_αbcdεf" "αbc_3_xyz_αbcdεf"]
- expected [:%ce%b1bcd% :%_b59bccce :%ce%b1bc_2 :%ce%b1bc_3]
+ expected [:%ce%b1bcd :%_a2ba0330 :%ce%b1bc_2 :%ce%b1bc_3]
displays ["αbcdεf" "αbcdεfg" "αbc 2 etc" "αbc 3 xyz"]]
(is (= expected (#'upload/derive-column-names ::short-column-test-driver original)))
(mt/with-dynamic-fn-redefs [upload/max-bytes (constantly 10)]
diff --git a/test/metabase/util_test.cljc b/test/metabase/util_test.cljc
index 15b6e73a5c0..2f01318071a 100644
--- a/test/metabase/util_test.cljc
+++ b/test/metabase/util_test.cljc
@@ -133,7 +133,9 @@
(doseq [[s expected] s->expected]
(testing (list 'u/slugify s)
(is (= expected
- (u/slugify s))))))))
+ (u/slugify s)))))))
+ (testing "non-ASCII characters are not truncated in the middle"
+ (is (= "%E5%8B%87" (u/slugify "勇士" {:max-length 10})))))
(deftest ^:parallel slugify-unicode-test
(doseq [[group s->expected]