Last active
August 29, 2015 14:24
-
-
Save spacepluk/f172a990e11d0589e3c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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