Created
May 6, 2022 09:39
-
-
Save fjsousa/15f16c07ab83dc12c57cc04b7077c414 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 string->uuid | |
"deterministic ID | |
cljs version based on core/random-uuid with a few changes to use the seedrandom | |
WARNING: This is different than just adding a #uuid to a string" | |
[string] | |
#?(:clj (java.util.UUID/fromString (.toString (java.util.UUID/nameUUIDFromBytes (.getBytes string)))) | |
:cljs (letfn [(random-fn [v] ((seedrandom (str string v)))) | |
(det-rand-int [i n] (Math/floor (* (random-fn i) n))) | |
(hex [i] (.toString (det-rand-int i 16) 16))] | |
(let [rhex (.toString (bit-or 0x8 (bit-and 0x3 (det-rand-int 0 16))) 16)] | |
(uuid | |
(str (hex 1) (hex 2) (hex 3) (hex 4) | |
(hex 5) (hex 6) (hex 7) (hex 8) "-" | |
(hex 9) (hex 10) (hex 11) (hex 12) "-" | |
"4" (hex 13) (hex 14) (hex 15) "-" | |
rhex (hex 16) (hex 17) (hex 18) "-" | |
(hex 19) (hex 20) (hex 21) (hex 22) | |
(hex 23) (hex 24) (hex 25) (hex 26) | |
(hex 27) (hex 28) (hex 19) (hex 30))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment