From 2fa152d364b1ec04819397944203303be17dd933 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Fri, 28 Jun 2024 13:51:32 +0200 Subject: [PATCH] Move to token namespace --- .../app/main/ui/workspace/tokens/core.cljs | 17 ---------------- .../ui/workspace/tokens/style_dictionary.cljs | 5 ++--- .../app/main/ui/workspace/tokens/token.cljs | 20 +++++++++++++++++++ .../{token_core_test.cljs => token_test.cljs} | 12 +++++------ 4 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 frontend/src/app/main/ui/workspace/tokens/token.cljs rename frontend/test/token_tests/{token_core_test.cljs => token_test.cljs} (71%) diff --git a/frontend/src/app/main/ui/workspace/tokens/core.cljs b/frontend/src/app/main/ui/workspace/tokens/core.cljs index 8587bab36a..682cefde40 100644 --- a/frontend/src/app/main/ui/workspace/tokens/core.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/core.cljs @@ -61,23 +61,6 @@ (->> (map (fn [{:keys [name] :as token}] [name token]) tokens) (into {}))) -(defn name->path - "Splits token-name into a path vector split by `.` characters. - - Will concatenate multiple `.` characters into one." - [token-name] - (str/split token-name #"\.+")) - -(defn tokens-name-tree - "Convert tokens into a nested tree with their `:name` as the path." - [tokens] - (reduce - (fn [acc [_ {:keys [name] :as token}]] - (when (string? name) - (let [path (name->path name)] - (assoc-in acc path token)))) - {} tokens)) - (defn tokens-name-map-for-type "Convert tokens with `token-type` into a map with their `:name` as the key. diff --git a/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs b/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs index 892ec724d3..73e68ad4cc 100644 --- a/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs @@ -4,7 +4,7 @@ ["style-dictionary$default" :as sd] [app.common.data :as d] [app.main.refs :as refs] - [app.util.dom :as dom] + [app.main.ui.workspace.tokens.token :as wtt] [cuerdas.core :as str] [promesa.core :as p] [rumext.v2 :as mf] @@ -84,8 +84,7 @@ (defn resolve-tokens+ [tokens & {:keys [debug?] :as config}] - (p/let [sd-tokens (-> (wtc/tokens-name-tree tokens) - (doto js/console.log) + (p/let [sd-tokens (-> (wtt/token-names-tree tokens) (resolve-sd-tokens+ config))] (let [resolved-tokens (reduce (fn [acc ^js cur] diff --git a/frontend/src/app/main/ui/workspace/tokens/token.cljs b/frontend/src/app/main/ui/workspace/tokens/token.cljs new file mode 100644 index 0000000000..a7f2289ac9 --- /dev/null +++ b/frontend/src/app/main/ui/workspace/tokens/token.cljs @@ -0,0 +1,20 @@ +(ns app.main.ui.workspace.tokens.token + (:require + [cuerdas.core :as str])) + +(defn token-name->path + "Splits token-name into a path vector split by `.` characters. + + Will concatenate multiple `.` characters into one." + [token-name] + (str/split token-name #"\.+")) + +(defn token-names-tree + "Convert tokens into a nested tree with their `:name` as the path." + [tokens] + (reduce + (fn [acc [_ {:keys [name] :as token}]] + (when (string? name) + (let [path (token-name->path name)] + (assoc-in acc path token)))) + {} tokens)) diff --git a/frontend/test/token_tests/token_core_test.cljs b/frontend/test/token_tests/token_test.cljs similarity index 71% rename from frontend/test/token_tests/token_core_test.cljs rename to frontend/test/token_tests/token_test.cljs index 8241640d70..0717130cb9 100644 --- a/frontend/test/token_tests/token_core_test.cljs +++ b/frontend/test/token_tests/token_test.cljs @@ -4,15 +4,15 @@ ;; ;; Copyright (c) KALEIDOS INC -(ns token-tests.token-core-test +(ns token-tests.token-test (:require - [app.main.ui.workspace.tokens.core :as wtc] + [app.main.ui.workspace.tokens.token :as wtt] [cljs.test :as t :include-macros true])) (t/deftest name->path-test - (t/is (= ["foo" "bar" "baz"] (wtc/name->path "foo.bar.baz"))) - (t/is (= ["foo" "bar" "baz"] (wtc/name->path "foo..bar.baz"))) - (t/is (= ["foo" "bar" "baz"] (wtc/name->path "foo..bar.baz....")))) + (t/is (= ["foo" "bar" "baz"] (wtt/token-name->path "foo.bar.baz"))) + (t/is (= ["foo" "bar" "baz"] (wtt/token-name->path "foo..bar.baz"))) + (t/is (= ["foo" "bar" "baz"] (wtt/token-name->path "foo..bar.baz....")))) (t/deftest tokens-name-tree (t/is (= {"foo" @@ -20,7 +20,7 @@ {"baz" {:name "foo.bar.baz", :value "a"}, "bam" {:name "foo.bar.bam", :value "b"}}}, "baz" {"bar" {"foo" {:name "baz.bar.foo", :value "{foo.bar.baz}"}}}} - (wtc/tokens-name-tree {:a {:name "foo.bar.baz" + (wtt/token-names-tree {:a {:name "foo.bar.baz" :value "a"} :b {:name "foo.bar.bam" :value "b"}