From a6c535684319db5d5d6b95eee8ae28f1ac19d7da Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Tue, 10 Mar 2026 13:00:40 +0100 Subject: [PATCH] :recycle: Use tdd to resolve parsing token --- common/src/app/common/types/token.cljc | 42 ++++++++++++++++++- .../test/common_tests/types/token_test.cljc | 18 ++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/common/src/app/common/types/token.cljc b/common/src/app/common/types/token.cljc index e3e541da33..8781da0b1f 100644 --- a/common/src/app/common/types/token.cljc +++ b/common/src/app/common/types/token.cljc @@ -607,7 +607,8 @@ "400" #{"book" "normal" "buch" "regular"}, "500" #{"kräftig" "medium" "kraeftig"}, "600" #{"demi-bold" "halbfett" "demibold" "demi bold" "semibold" "semi bold" "semi-bold"}, - "700" #{"dreiviertelfett" "bold"}, + "700" #{"dreivi + ertelfett" "bold"}, "800" #{"extrabold" "fett" "extra-bold" "ultrabold" "ultra-bold" "extra bold" "ultra bold"}, "900" #{"heavy" "black" "extrafett"}, "950" #{"extra-black" "extra black" "ultra-black" "ultra black"}}) @@ -637,3 +638,42 @@ (when (font-weight-values weight) (cond-> {:weight weight} italic? (assoc :style "italic"))))) + +(defn inside-ref? + [value position] + (let [left-part (str/slice value 0 position) + last-index-open (str/last-index-of left-part "{") + last-index-close (str/last-index-of left-part "}")] + (cond + (and (nil? last-index-open) (nil? last-index-close)) false + (and (nil? last-index-open) (some? last-index-close)) false + (and (some? last-index-open) (nil? last-index-close)) true + :else + (< last-index-close last-index-open)))) + +(defn start-ref-position + [value position] + (let [left-part (str/slice value 0 position) + last-index-open (str/last-index-of left-part "{")] + last-index-open)) + +(defn end-ref-position + [value position] + (let [right-part (str/slice value position) + next-index-close (str/index-of right-part "}")] + (if (some? next-index-close) + (+ position next-index-close 1) + position))) + +(defn insert-ref + [value position name] + (let [reference (str "{" name "}")] + (if (inside-ref? value position) + (let [first-part (str/slice value 0 (start-ref-position value position)) + second-part (str/slice value (end-ref-position value position))] + {:result (str first-part reference second-part) + :position (+ (count first-part) (count reference))}) + (let [first-part (str/slice value 0 position) + second-part (str/slice value position)] + {:result (str first-part reference second-part) + :position (+ position (count reference))})))) diff --git a/common/test/common_tests/types/token_test.cljc b/common/test/common_tests/types/token_test.cljc index fb93bd2541..08b06d99db 100644 --- a/common/test/common_tests/types/token_test.cljc +++ b/common/test/common_tests/types/token_test.cljc @@ -24,3 +24,21 @@ (t/is (false? (sm/validate cto/schema:token-name "Hey Foo.Bar"))) (t/is (false? (sm/validate cto/schema:token-name "Hey😈Foo.Bar"))) (t/is (false? (sm/validate cto/schema:token-name "Hey%Foo.Bar")))) + + +(t/deftest token-value-with-refs + (t/testing "empty value" + (t/is (= (cto/insert-ref "" 0 "token1") + {:result "{token1}" :position 8}))) + (t/testing "value without references" + (t/is (= (cto/insert-ref "ABC" 0 "token1") + {:result "{token1}ABC" :position 8})) + (t/is (= (cto/insert-ref "23 + " 5 "token1") + {:result "23 + {token1}" :position 13})) + (t/is (= (cto/insert-ref "23 + " 5 "token1") + {:result "23 + {token1}" :position 13}))) + (t/testing "value with closed references" + (t/is (= (cto/insert-ref "{token2}" 8 "token1") + {:result "{token2}{token1}" :position 16})) + (t/is (= (cto/insert-ref "{token2}" 6 "token1") + {:result "{token1}" :position 8})))) \ No newline at end of file