generated from PEZ/rn-rf-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.cljs
66 lines (56 loc) · 1.36 KB
/
helpers.cljs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns gurps.utils.helpers
(:require [clojure.string :as str]))
(defn default-to
"Returns the default value if the value is nil or empty"
[v default]
(if (or (and (seqable? v) (empty? v)) (nil? v))
default
v))
(defn ->int
[n]
(let [ret (js/parseInt n)]
(if (js/isNaN ret)
0
ret)))
(defn str->key
[str]
(keyword str))
(defn key->str
[key]
(str (symbol key)))
(defn flatten-key
[key]
(symbol
(if (some? (namespace key))
(str (namespace key) "-" (name key))
(str (name key)))))
(defn pluralize-key
[key]
(symbol
(if (some? (namespace key))
(str (namespace key) "s-" (name key))
(str (name key) "s"))))
(defn singularize
"Not exhaustive, but good enough for now
can use https://www.npmjs.com/package/pluralize for more"
[s]
(str/replace s #"(s)$" ""))
(defn singularize-key
"Returns the singular form of the key.
e.g. :skills -> :skill
:skills/weapon -> :skill/weapon"
[key]
(symbol
(if (some? (namespace key))
(str (singularize (namespace key)) "-" (name key))
(str (singularize (name key))))))
;; NOTE: because 'positions has vanished from clojure.contrib.seq
(defn positions
[pred coll]
(keep-indexed (fn [idx x]
(when (pred x)
idx))
coll))
(defn index-of
[e coll]
(first (keep-indexed #(when (= e %2) %1) coll)))