-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cljc
37 lines (30 loc) · 930 Bytes
/
util.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(ns ^{:author "Daniel Leong"
:doc "spade.util"}
spade.util
(:require [clojure.string :as str]))
(defn factory->name [factory]
(-> (.-name factory)
(str/replace "_factory$" "")
(str/replace #"[_$]" "-")))
(defn sanitize [s]
(-> s
str
(str/replace #"[^A-Za-z0-9-_]" "-")))
(defn params->key [p]
(try
(hash p)
(catch #?(:cljs :default
:clj Throwable) _
nil)))
(defn build-style-name [base style-key params]
(cond
; easy case: a key was provided
style-key (str base "_" (sanitize style-key))
(seq params) (if-let [pkey (params->key params)]
(str base "_" pkey)
(let [msg (str "WARNING: no key provided for " base)]
#?(:cljs (js/console.warn msg)
:clj (throw (Exception. msg)))
base))
; easiest case: no key is necessary
:else base))