Skip to content

Instantly share code, notes, and snippets.

@spacepluk
Last active August 29, 2015 14:24
Show Gist options
  • Save spacepluk/f172a990e11d0589e3c9 to your computer and use it in GitHub Desktop.
Save spacepluk/f172a990e11d0589e3c9 to your computer and use it in GitHub Desktop.
(defn- generate-uuid []
(let [pattern "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
time-api (or js/performance js/Date)
now (atom (.now time-api))]
(replace pattern #"[xy]" (fn [char]
(let [r (-> (rand)
(* 16)
(+ @now)
(rem 16)
(int))
digit (if (= char "y")
(-> r
(bit-and 0x3)
(bit-or 0x8))
r)]
(reset! now (.floor js/Math (/ @now 16)))
(.toString digit 16))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment