-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cljc
86 lines (74 loc) · 2.23 KB
/
utils.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(ns deercreeklabs.tube.utils
"Common code and utilities. Parts from https://github.com/farbetter/utils."
(:refer-clojure :exclude [byte-array send])
(:require
[#?(:clj clj-time.format :cljs cljs-time.format) :as f]
[#?(:clj clj-time.core :cljs cljs-time.core) :as t]
[#?(:clj clojure.core.async.impl.protocols
:cljs cljs.core.async.impl.protocols) :as cap]
[#?(:clj clojure.test :cljs cljs.test) :as test :include-macros true]
[deercreeklabs.baracus :as ba]
[deercreeklabs.log-utils :as lu :refer [debugs]]
[schema.core :as s]
[taoensso.timbre :as timbre :refer [debugf errorf infof]])
#?(:clj
(:import
(com.google.common.primitives Bytes)
(java.io ByteArrayInputStream ByteArrayOutputStream)
(java.util Arrays)
(java.util.zip DeflaterOutputStream InflaterOutputStream))
:cljs
(:require-macros
[deercreeklabs.tube.utils :refer [sym-map]])))
#?(:cljs
(set! *warn-on-infer* true))
;;;;;;;;;;;;;;;;;;;; Macros ;;;;;;;;;;;;;;;;;;;;
(defmacro sym-map
"Builds a map from symbols.
Symbol names are turned into keywords and become the map's keys.
Symbol values become the map's values.
(let [a 1
b 2]
(sym-map a b)) => {:a 1 :b 2}"
[& syms]
(zipmap (map keyword syms) syms))
;;;;;;;;;;;;;;;;;;;; Utility fns ;;;;;;;;;;;;;;;;;;;;
(defn compress-smart [data]
(if (<= (count data) 30)
[0 data]
(let [deflated (ba/deflate data)]
(if (<= (count data) (count deflated))
[0 data]
[1 deflated]))))
;;;;;;;;;;;;;;;;;;;; Platform detection ;;;;;;;;;;;;;;
(s/defn jvm? :- s/Bool
[]
#?(:clj true
:cljs false))
(s/defn browser? :- s/Bool
[]
#?(:clj false
:cljs
(exists? js/navigator)))
(s/defn node? :- s/Bool
[]
#?(:clj false
:cljs (boolean (= "nodejs" cljs.core/*target*))))
(s/defn get-platform-kw :- s/Keyword
[]
(cond
(jvm?) :jvm
(node?) :node
(browser?) :browser
:else :unknown))
(defn configure-logging []
(timbre/merge-config!
{:level :debug
:output-fn lu/short-log-output-fn
:appenders
{:println {:ns-blacklist
["org.eclipse.jetty.*"]}}}))
(s/defn get-current-time-ms :- s/Num
[]
#?(:clj (System/currentTimeMillis)
:cljs (.getTime (js/Date.))))