Interpolate nil values to empty string

This commit is contained in:
Julien Eluard 2019-04-12 12:11:29 +02:00
parent 0f167bbf69
commit 21e48f29d5
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
2 changed files with 6 additions and 5 deletions

View File

@ -73,5 +73,5 @@
(if (seq v)
(apply #?(:clj format-en :cljs gstring/format)
(string/replace s placeholder-pattern
(fn [[_ pattern]] (format-pattern pattern))) (map values names))
(fn [[_ pattern]] (format-pattern pattern))) (map #(or (get values %) "") names))
s)})))

View File

@ -9,15 +9,16 @@
(clojure.core/slurp file)))
(deftest interpolate
#_
(is (= {:errors [(error/syntax ::error/invalid-placeholders #{'id1})]}
(is (= {:errors [(error/syntax ::error/invalid {:type :placeholders} {:data #{'id}})]}
(utils/interpolate nil "test-${id}")))
(is (= {:errors [(error/syntax ::error/invalid {:type :placeholders} {:data #{'id1}})]}
(utils/interpolate {'id 3} "${id1}")))
(is (= {:data "test-"} (utils/interpolate {'id nil} "test-${id}")))
(is (= {:data "test-3"} (utils/interpolate {'id 3} "test-${id}")))
(is (= {:data "test- 3"} (utils/interpolate {'id "3"} "test-${id:2s}")))
(is (= {:data "2.369"} (utils/interpolate {'f 2.369} "${f}")))
(is (= {:data "12345"} (utils/interpolate {'d 12345} "${d:2d}")))
(is (= {:data " 12345"} (utils/interpolate {'d 12345} "${d:6d}")))
(is (= {:data "2.37"} (utils/interpolate {'f 2.369} "${f:1.2f}")))
#_
(is (= {:errors [(error/syntax ::error/invalid-placeholders #{'f})]}
(is (= {:errors [(error/syntax ::error/invalid {:type :placeholders} {:data #{'f}})]}
(utils/interpolate {'e 2.369} "${f:1.2f}"))))