Skip to content

Instantly share code, notes, and snippets.

Avatar

Simon Gray simongray

View GitHub Profile
@simongray
simongray / dataurl.clj
Created Jun 17, 2019
Helper functions for converting a DataURL to an image and saving to a file
View dataurl.clj
(defn dataurl->mime-type
"Extract the mime-type from a data URL."
[dataurl]
(->> (str/split dataurl #",")
(first)
(re-find #":(.*?);")
(second)))
(defn dataurl->image
"Extract the image data from a data URL."
@simongray
simongray / dataurl.cljs
Created Jun 17, 2019
Conversion functions between DataURL and Blob type
View dataurl.cljs
;; Based on the implementations found here: https://gist.github.com/wuchengwei/b7e1820d39445f431aeaa9c786753d8e
(defn dataurl->blob
[dataurl]
(let [arr (str/split dataurl #",")
mime (second (re-find #":(.*?);" (first arr)))
bstr (js/atob (second arr))
n (count bstr)
u8arr (-> (map (fn [i] (.charCodeAt bstr i)) (range n))
(into-array)
(js/Uint8Array.from))]
View gist:6fd9717026b644db7030b2986ddbf5a5
(defn enumerated
[xs]
(apply str (concat (interleave (drop-last 2 xs) (repeat ", "))
(interpose " og " (take-last 2 xs)))))
(do
(println (enumerated ["a"]))
(println (enumerated ["a" "b"]))
(println (enumerated ["a" "b" "c"]))
(println (enumerated ["a" "b" "c" "d"])))