-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.cljc
79 lines (66 loc) · 2.01 KB
/
helper.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
(ns pinkgorilla.encoding.helper
(:require
[clojure.string :as str]
[cognitect.transit :as t]))
;; Cognitec TRANSIT WRITER
;; make json encoding independent of clj/cljs
#?(:clj (import '[java.io ByteArrayInputStream ByteArrayOutputStream]))
(defn create-writer []
#?(:clj (let [out (ByteArrayOutputStream. (* 4096 1024))
writer (t/writer out :json {:transform t/write-meta})]
{:writer writer :out out})
:cljs (t/writer :json {:transform t/write-meta})))
(defn to-json [writer o]
#?(:clj (do (t/write (:writer writer) o)
(String. (.toByteArray (:out writer))))
:cljs (do ;(println "encoding: " o)
(try
(t/write writer o)
(catch js/Object ex
(println "encode exception object: " o " ex: " ex)
"{}")))))
(defn from-json [s]
#?(:clj
(try
(do
(def in (ByteArrayInputStream. (.getBytes s "UTF-8")))
(def reader (t/reader in :json))
(t/read reader))
(catch Exception ex
(println "decode json exception: " s)
nil))
:cljs (try
(t/read (t/reader :json) s)
(catch js/Object ex
(println "decode json exception: " s)
nil))))
;; COMMENT ENCODING / DECODING
(defn make-clojure-comment
[code]
#_(println "make-clojure-comment for: " code)
(if (nil? code)
";;; "
(->> (str/split-lines code)
(map #(str ";;; " %))
(str/join "\n"))))
(defn unmake-line [l]
(if (nil? l)
(do (println "unmake-line called with nil.")
"")
(let [c (count l)]
(if (< c 4)
(do (println "unmake-line called with less than 4 characters: " l)
l)
(subs l 4 c)))))
(defn unmake-clojure-comment
[code]
(if (nil? code)
""
(->> (str/split-lines code)
(map unmake-line)
(str/join "\n"))))
;(defn unmake-clojure-comment
; [code]
; (try
; (unmake-clojure-comment-x code)
; (catch Exception e code)))