diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e3d1c2..216b277 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.0.next in progress + * Fixes #340 by making hyphen to space logic more general so _operators_ containing `-` should retain the hyphen without special cases. * Documentation improvements: `:fetch`, `:lift`, `:limit`, `:offset`, `:param`, `:select`; also around JSON/PostgreSQL. * 2.0.0-rc5 (for testing; 2021-07-17) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index d912375..69b6c72 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -146,9 +146,15 @@ #?(:clj (fn [^String s] (.. s toString (toUpperCase (java.util.Locale/US)))) :cljs str/upper-case)) -(def ^:private keep-hyphen - "The set of symbols that should not have `-` replaced by space." - #{"-" "<->"}) +(defn- dehyphen + "The loop/recur is because we might need to account for x-y-z in + a string where the second - won't get replaced because the regex + already matched y. I'm sure there's a more efficent solution!" + [s] + (loop [s s prev nil] + (if (= s prev) + s + (recur (str/replace s #"(\w)-(\w)" "$1 $2") s)))) (defn sql-kw "Given a keyword, return a SQL representation of it as a string. @@ -158,8 +164,7 @@ Any namespace qualifier is ignored." [k] - (-> k (name) (upper-case) - (as-> s (if (keep-hyphen s) s (str/replace s "-" " "))))) + (-> k (name) (dehyphen) (upper-case))) (defn- sym->kw "Given a symbol, produce a keyword, retaining the namespace