From b73cdd15e0e386bfd7da218520973993faad11d9 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Thu, 4 Jul 2024 11:28:15 +0200 Subject: [PATCH] Add helper to remove attributes from applied-tokens --- frontend/src/app/main/ui/workspace/tokens/token.cljs | 10 ++++++++++ frontend/test/token_tests/token_test.cljs | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/frontend/src/app/main/ui/workspace/tokens/token.cljs b/frontend/src/app/main/ui/workspace/tokens/token.cljs index 2db7c24cf..79fbb0e2d 100644 --- a/frontend/src/app/main/ui/workspace/tokens/token.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/token.cljs @@ -8,6 +8,16 @@ (->> (map (fn [attr] {attr id}) attributes) (into {}))) +(defn remove-attributes-for-token-id + "Removes applied tokens with `token-id` for the given `attributes` set from `applied-tokens`." + [attributes token-id applied-tokens] + (let [attr? (set attributes)] + (->> (remove (fn [[k v]] + (and (attr? k) + (= v token-id))) + applied-tokens) + (into {})))) + (defn token-applied? "Test if `token` is applied to a `shape` with the given `token-attributes`." [token shape token-attributes] diff --git a/frontend/test/token_tests/token_test.cljs b/frontend/test/token_tests/token_test.cljs index c999c74ca..255adf28e 100644 --- a/frontend/test/token_tests/token_test.cljs +++ b/frontend/test/token_tests/token_test.cljs @@ -9,6 +9,12 @@ [app.main.ui.workspace.tokens.token :as wtt] [cljs.test :as t :include-macros true])) +(t/deftest remove-attributes-for-token-id + (t/testing "removes attributes matching the `token-id`, keeps other attributes" + (t/is (= {:ry :b} + (wtt/remove-attributes-for-token-id + #{:rx :ry} :a {:rx :a :ry :b}))))) + (t/deftest token-applied-test (t/testing "matches passed token with `:token-attributes`" (t/is (true? (wtt/token-applied? {:id :a} {:applied-tokens {:x :a}} #{:x}))))