-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.cljc
41 lines (34 loc) · 1.12 KB
/
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
38
39
40
41
(ns graphql-builder.util
(:require [clojure.string :as str]
[clojure.walk :as walk]
[camel-snake-kebab.core :refer [->camelCase]]))
(defn nl-join [coll]
(when (seq coll)
(str/join "\n" (vec (remove nil? coll)))))
(defn indent [level line]
(str (str/join "" (repeat (* 2 level) " ")) line))
(defn combine-children [children]
(reduce (fn [acc c]
(let [children (or (:children acc) [])
deps (or (:deps acc) [])
c-deps (:deps c)]
(assoc acc
:children (conj children (:children c))
:deps (if c-deps (into deps c-deps) deps))))
{} children))
(defn transform-keys
"Recursively transforms all map keys in coll with t."
[t coll]
(let [f (fn [[k v]] [(t k) v])]
(walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) coll)))
;; added
(defn- key->graphql [k]
(if (string? k)
k
(->camelCase (name k))))
(defn variables->graphql [vars]
(transform-keys key->graphql vars))
(defn reverse-map
"Reverse the keys/values of a map"
[m]
(into {} (map (fn [[k v]] [v k]) m)))