mirror of
https://github.com/penpot/penpot.git
synced 2025-12-12 06:24:17 +01:00
Add function to check if a token can be placed under a name path
This commit is contained in:
@@ -18,3 +18,31 @@
|
||||
(let [path (token-name->path name)]
|
||||
(assoc-in acc path token))))
|
||||
{} tokens))
|
||||
|
||||
(defn token-name-path-exists?
|
||||
"Traverses the path from `token-name` down a `token-tree` and checks if a token at that path exists.
|
||||
|
||||
It's not allowed to create a token inside a token. E.g.:
|
||||
Creating a token with
|
||||
|
||||
{:name \"foo.bar\"}
|
||||
|
||||
in the tokens tree:
|
||||
|
||||
{\"foo\" {:name \"other\"}}"
|
||||
[token-name token-names-tree]
|
||||
(let [name-path (token-name->path token-name)
|
||||
result (reduce
|
||||
(fn [acc cur]
|
||||
(let [target (get acc cur)]
|
||||
(cond
|
||||
;; Path segment doesn't exist yet
|
||||
(nil? target) (reduced false)
|
||||
;; A token exists at this path
|
||||
(:name target) (reduced true)
|
||||
;; Continue traversing the true
|
||||
:else target)))
|
||||
token-names-tree name-path)]
|
||||
(if (map? result)
|
||||
(some? (:name result))
|
||||
result)))
|
||||
|
||||
Reference in New Issue
Block a user